I’ve been working a code that creates a QDialog window using PyQt5 with an image where it lets you find colonies by changing the values of the slides. Here’s the code for reference:
class HoughCircleDialog(QDialog):
def __init__(self, img_ori, img_pro, parent):
super().__init__(parent)
self.setWindowTitle("Hough Circle Transform ")
self.img_ori = img_ori
self.img_pro = img_pro
self.slider_sensitivity = QSlider(Qt.Horizontal)
self.slider_sensitivity.setRange(1,10)
self.slider_sensitivity.setValue(5)
self.slider_neighborhood = QSlider(Qt.Horizontal)
self.slider_neighborhood.setRange(1,30)
self.slider_neighborhood.setValue(15)
self.slider_accumulator = QSlider(Qt.Horizontal)
self.slider_accumulator.setRange(1,50)
self.slider_accumulator.setValue(25)
self.slider_min_radius = QSlider(Qt.Horizontal)
self.slider_min_radius.setRange(1,30)
self.slider_min_radius.setValue(15)
self.slider_max_radius = QSlider(Qt.Horizontal)
self.slider_max_radius.setRange(1,30)
self.slider_max_radius.setValue(15)
self.sen_label = QLabel("Sensitivity: 5")
self.neigh_label = QLabel("Neighborhood: 15")
self.acc_label = QLabel("Accumulator: 25")
self.max_label = QLabel("Min Radius: 15")
self.min_label = QLabel("Max Radius: 15")
self.image_label = QLabel()
self.slider_sensitivity.valueChanged.connect(self.update_image)
self.slider_neighborhood.valueChanged.connect(self.update_image)
self.slider_accumulator.valueChanged.connect(self.update_image)
self.slider_min_radius.valueChanged.connect(self.update_image)
self.slider_max_radius.valueChanged.connect(self.update_image)
# Arrange layout
layout = QVBoxLayout()
layout.addWidget(self.slider_sensitivity)
layout.addWidget(self.sen_label)
layout.addWidget(self.slider_neighborhood)
layout.addWidget(self.neigh_label)
layout.addWidget(self.slider_accumulator)
layout.addWidget(self.acc_label)
layout.addWidget(self.slider_min_radius)
layout.addWidget(self.min_label)
layout.addWidget(self.slider_max_radius)
layout.addWidget(self.max_label)
layout.addWidget(self.image_label)
self.setLayout(layout)
self.update_image() # Initial display
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return:
self.accept()
def update_image(self):
# Get current slider values
circles = 0
sensitivity = self.slider_sensitivity.value()
neighborhood = self.slider_neighborhood.value()
accumulator = self.slider_accumulator.value()
min_radius = self.slider_min_radius.value()
max_radius = self.slider_max_radius.value()
self.sen_label.setText(f"Sensivity: {sensitivity}")
self.neigh_label.setText(f"Neighborhood: {neighborhood}")
self.acc_label.setText(f"Neighborhood: {accumulator}")
self.min_label.setText(f"Min value: {min_radius}")
self.max_label.setText(f"Max value: {max_radius}")
# Perform Hough Circle Transform
circles = cv2.HoughCircles(
self.img_pro, cv2.HOUGH_GRADIENT, sensitivity+1, neighborhood+1,
param1=50, param2=accumulator+1, minRadius=min_radius+1, maxRadius=max_radius+1)
# Draw circles on the original image
img_show = self.img_ori.copy()
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(img_show, (i[0], i[1]), i[2], (0, 0, 255), 2)
self.img_show = img_show
self.circles = circles
img_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
q_img = QImage(img_rgb.data, img_rgb.shape[1], img_rgb.shape[0], QImage.Format_RGB888)
scaled_img = QPixmap.fromImage(q_img).scaled(
self.image_label.width(), self.image_label.height(),
Qt.KeepAspectRatio, Qt.SmoothTransformation
)
self.image_label.setPixmap(scaled_img)
def get_result(self):
return self.img_show, len(self.circles[0])
When I run the code, the qdialog window is created with no problem. Howver, the main issue is how the plate image is shown, like this.
The image should look more like this.
What I’ve tried so far was displaying the processed image (self.img_pro) directly without further modification, displaying the original unprocessed image (self.img_ori) without any transformations and checking that QImage format matches the image type (RGB vs Grayscale) but the issue always persisted.
Here’s the code where the Hough Circle class is called, as I’m not sure if the issue comes from the img_ori used in the CoutnerApp class:
class CounterApp:
def __init__(self, parent, img_path, method):
self.parent = parent
self.img_path = img_path
self.method = method
self.run()
def run(self):
max_size = min(mon.width for mon in get_monitors()) - 100
img = self.load_image(self.img_path, max_size)
start_time = time.time()
img_ori, img_bin = self.preprocess(img)
plate_dialog = PlateIdentification(img_ori, img_bin, self.parent)
if plate_dialog.exec_() == QDialog.Accepted:
plate_mask, circle = plate_dialog.get_result()
processing_dialog = ProcessingDialog(img_ori, img_bin, plate_mask, circle, self.parent)
if processing_dialog.exec_() == QDialog.Accepted:
img_ori, img_pro, success = processing_dialog.process_results()
if not success:
print("Preprocessing window was closed. Exiting.")
return
if self.method.lower() == 'h':
hough_method = HoughCircleDialog(img_ori, img_pro, self.parent)
if hough_method.exec_() == QDialog.Accepted:
output, colonies = hough_method.get_result()
You need to sign in to view this answers