Question: import sys import cv 2 import numpy as np from PySide 6 . QtWidgets import ( QApplication , QMainWindow , QLabel, QPushButton, QFileDialog ) from

import sys
import cv2
import numpy as np
from PySide6.QtWidgets import (QApplication,QMainWindow, QLabel, QPushButton, QFileDialog)
from PySide6.QtGui import QPixmap
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QFile
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('QtAgg')
class CellCounterApp(QMainWindow):
def __init__(self,parent=None):
super().__init__()
# Load form.ui file
self.load_ui()
# Get references to components
self.image_label =self.findChild(QLabel,"image_label")
self.result_label =self.findChild(QLabel,"result_label")
self.load_button =self.findChild(QPushButton,"load_button")
self.count_button =self.findChild(QPushButton,"count_button")
# Connect button signals
self.load_button.clicked.connect(self.load_image)
self.count_button.clicked.connect(self.count_cells)
self.count_button.setEnabled(False)
# Image data attributes
self.image_path =None
self.original_image =None
def load_ui(self):
"""Loads the form.ui file."""
loader =QUiLoader()
ui_file =QFile("form.ui") # Path to the form.ui file
ui_file.open(QFile.ReadOnly)
self.ui =loader.load(ui_file)
self.ui.setParent(self)
ui_file.close()
def load_image(self):
"""Handles loading an image from the file system."""
file_path, _=QFileDialog.getOpenFileName(self,"Select Image", "","Images (*.png *.jpg *.jpeg *.bmp)")
if file_path:
self.image_path =file_path
self.original_image =cv2.imread(self.image_path) # Load image with OpenCV
# Show the image on QLabel
pixmap =QPixmap(self.image_path)
self.image_label.setPixmap(pixmap)
self.result_label.setText("Cell Count: N/A") # Reset the result label
self.count_button.setEnabled(True) # Enable the count button
def count_cells(self):
"""Detects and counts cells in the loaded image."""
if self.original_image is None:
return
# Convert the image to grayscale
gray =cv2.cvtColor(self.original_image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian Blur to reduce noise and smooth the image
blurred =cv2.GaussianBlur(gray,(11,11),0)
# Apply thresholding to create a binary image
_,binary =cv2.threshold(blurred,127,255,cv2.THRESH_BINARY_INV)
# Find contours in the binary image
contours, _=cv2.findContours(binary,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Get the number of detected contours
cell_count =len(contours)
self.result_label.setText(f"Cell Count: {cell_count}")
# Draw contours on the original image
output_image =self.original_image.copy()
cv2.drawContours(output_image, contours, -1,(0,255,0),2)
# Use matplotlib to display the original and processed images side by side
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.title("Original Image")
plt.imshow(cv2.cvtColor(self.original_image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB
plt.subplot(1,2,2)
plt.title("Contours")
plt.imshow(cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB))
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# You can display images directly in the QLabel or another Qt widget during the cell counting process.
if __name__=="__main__":
"""Entry point of the application."""
app =QApplication(sys.argv)
window =CellCounterApp()
window.show()
sys.exit(app.exec())
when I run this code on QTCreator, it shows this error every time:2024-12-3103:06:40.457Python[59090:4762284]+[IMKClient subclass]: chose IMKClient_Modern
2024-12-3103:06:40.457Python[59090:4762284]+[IMKInputSession subclass]: chose IMKInputSession_Modern
QCoreApplication::exec: The event loop is already running

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!