Question: import sys import cv 2 import numpy as np from PySide 6 . QtWidgets import ( QApplication , QMainWindow , QLabel, QPushButton, QFileDialog ) from
import sys
import cv
import numpy as np
from PySideQtWidgets import QApplicationQMainWindow QLabel, QPushButton, QFileDialog
from PySideQtGui import QPixmap
from PySideQtUiTools import QUiLoader
from PySideQtCore import QFile
from matplotlib import pyplot as plt
import matplotlib
matplotlib.useQtAgg
class CellCounterAppQMainWindow:
def initselfparentNone:
superinit
# Load form.ui file
self.loadui
# Get references to components
self.imagelabel selffindChildQLabelimagelabel"
self.resultlabel selffindChildQLabelresultlabel"
self.loadbutton selffindChildQPushButtonloadbutton"
self.countbutton selffindChildQPushButtoncountbutton"
# Connect button signals
self.loadbutton.clicked.connectselfloadimage
self.countbutton.clicked.connectselfcountcells
self.countbutton.setEnabledFalse
# Image data attributes
self.imagepath None
self.originalimage None
def loaduiself:
Loads the form.ui file."""
loader QUiLoader
uifile QFileformui # Path to the form.ui file
uifile.openQFileReadOnly
self.ui loaderloaduifile
self.uisetParentself
uifile.close
def loadimageself:
Handles loading an image from the file system."""
filepath, QFileDialoggetOpenFileNameselfSelect Image", Images png jpg jpeg bmp
if filepath:
self.imagepath filepath
self.originalimage cvimreadselfimagepath # Load image with OpenCV
# Show the image on QLabel
pixmap QPixmapselfimagepath
self.imagelabel.setPixmappixmap
self.resultlabel.setTextCell Count: NA # Reset the result label
self.countbutton.setEnabledTrue # Enable the count button
def countcellsself:
Detects and counts cells in the loaded image."""
if self.originalimage is None:
return
# Convert the image to grayscale
gray cvcvtColorselforiginalimage, cvCOLORBGRGRAY
# Apply Gaussian Blur to reduce noise and smooth the image
blurred cvGaussianBlurgray
# Apply thresholding to create a binary image
binary cvthresholdblurredcvTHRESHBINARYINV
# Find contours in the binary image
contours, cvfindContoursbinarycvRETREXTERNAL, cvCHAINAPPROXSIMPLE
# Get the number of detected contours
cellcount lencontours
self.resultlabel.setTextfCell Count: cellcount
# Draw contours on the original image
outputimage selforiginalimage.copy
cvdrawContoursoutputimage, contours,
# Use matplotlib to display the original and processed images side by side
pltfigurefigsize
pltsubplot
plttitleOriginal Image"
pltimshowcvcvtColorselforiginalimage, cvCOLORBGRRGB # Convert BGR to RGB
pltsubplot
plttitleContours
pltimshowcvcvtColoroutputimage, cvCOLORBGRRGB
from matplotlib.backends.backendqtagg import FigureCanvasQTAgg as FigureCanvas
# You can display images directly in the QLabel or another Qt widget during the cell counting process.
if namemain:
Entry point of the application."""
app QApplicationsysargv
window CellCounterApp
window.show
sysexitappexec
when I run this code on QTCreator, it shows this error every time:::Python:IMKClient subclass: chose IMKClientModern
::Python:IMKInputSession subclass: chose IMKInputSessionModern
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
