简体   繁体   中英

How to display contours (openCV) on an image in a HMI (pyQt) - Python

I am developping a tool for a project of my engineering school. This is my first time making a HMI and using openCV and qtpy .

My goal is to apply a first treatment on the image (blur and stuff) to make it easier to detect the contours of a plum. Then I want to display the contours of the plum found using the openCV findContour method. For the time being i managed to display the contours through the openCV method drawContours however it opens a new window through matplot instead of displaying the contours on top of the image that is already displayed in my MHI.

How can I do to not display the contours in a new matplot window but on top of my already displayed image in my HMI?

Here is what my HMI looks like.

在此处输入图像描述

Here is my code concerning the contours display which uses drawContours method.

def allContours(img_originale, masque, toggleMode) :
    if toggleMode :
        #trouve les contours sur l'image
        image_copy = img_originale.copy()
        contours, hier = cv2.findContours(image=masque, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(image=image_copy, contours=contours, contourIdx=-1, color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
        cv2.imshow('Contours', image_copy)
        cv2.waitKey(0)
    else :
        pass

And here is the code part that displays my image in the HMI:

 # Redimensionnement et affichage Image
        image = cv2.resize(image, (int(self.ratio*self.image_w),int(self.ratio*self.image_h)))
        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
        height, width, channels = image.shape
        step =  channels*width
        qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
        self.imageLabel.setPixmap(QPixmap.fromImage(qImg))

I would like to obtain the following result but have it in the HMI instead of a new window. Can anyone tell me how to do? Thank you ! 在此处输入图像描述

It would be easier if we could see more of your code but basically you can just return the image with the drawn contours:

def allContours(img_originale, masque, toggleMode) :
    if toggleMode :
        #trouve les contours sur l'image
        image_copy = img_originale.copy()
        contours, hier = cv2.findContours(image=masque, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(image=image_copy, contours=contours, contourIdx=-1, color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
        return image_copy
    else :
        return None

Also tweak your "blur and stuff" preprocessing a bit. It should be possible to get even better results. Maybe try Canny edge detection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM