cv2.cvtColor causes to stop Python

asked 2020-01-24 14:20:35 -0600

hernancrespo gravatar image

I am using PyQT,Python and OpenCV. I want to find contours. So i need to convert image to 1 channel (greyscale). Image is on the HSV color-space. First, i convert it from HSV channel to RGB using

gray = cv2.cvtColor(fg,cv2.COLOR_HSV2RGB)

then i try to convert it to grayscale using

gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)

when i add cv2.COLOR_BGR2GRAY and run the code. Python is not being responsible and it stops working.

this is fg (i want conver it from hsv to grayscale)

and i have been trying to convert it to gray so that i can find contours.

full code:

import cv2
import numpy as np
from PyQt5.QtCore import QThread, QTimer
from PyQt5.QtWidgets import QLabel, QWidget, QPushButton, QVBoxLayout, QApplication, QHBoxLayout, QMessageBox,  QMainWindow
from PyQt5.QtGui import QPixmap, QImage


class Camera:

    def __init__(self, cam_num):

        self.cap = cv2.VideoCapture(cam_num)
        self.cam_num = cam_num

    def open(self, width=640, height=480, fps=30):

        self.cap.set(3, width)   # set width #propID =3 yani 3.property si width. 3.property i 480 yap
        self.cap.set(4, height)  # set height
        return self.cap.isOpened()

    def find_contours(self):

        rval, frame = self.cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        roi = frame
        hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV)
        lower_green = np.array([0, 0, 90])
        upper_green = np.array([255, 255, 255])
        mask = cv2.inRange(hsv, lower_green, upper_green)
        mask_inv = cv2.bitwise_not(mask)
        bg = cv2.bitwise_and(roi, roi, mask=mask)
        fg = cv2.bitwise_and(roi, roi, mask=mask_inv)

        gray = cv2.cvtColor(fg,cv2.COLOR_HSV2RGB)
        gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)
        return gray

    def close_camera(self):
        self.cap.release()


class UI_Window(QWidget):

    def __init__(self, cam_num):
        super().__init__()
        self.cam_num = cam_num
        print('UI')
        self.cam_num.open()

        # Create a timer.
        self.timer = QTimer()
        self.timer.timeout.connect(self.nextFrameSlot)
        self.timer.start(1000 / 24)

        # Create a layout.
        layout = QVBoxLayout()

        button2_layout = QHBoxLayout()

        btn2 = QPushButton("Acquire Frame")
        btn2.clicked.connect(self.take_photo)
        button2_layout.addWidget(btn2)
        layout.addLayout(button2_layout)

        # Add a label
        self.label = QLabel()
        self.label.setFixedSize(640, 640)

        layout.addWidget(self.label)

        # Set the layout
        self.setLayout(layout)
        self.setWindowTitle("First GUI with QT")

    def nextFrameSlot(self):

        frame = self.cam_num.find_contours()
        if frame is not None:
            image = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
            self.pixmap = QPixmap.fromImage(image)

    def take_photo(self):
        self.label.setPixmap(self.pixmap)


if __name__ == '__main__':

    camera = Camera(0)
    app = QApplication([])
    start_window = UI_Window(camera)
    start_window.show()
    app.exit(app.exec_())
    camera.close_camera()
edit retag flag offensive close merge delete

Comments

your input image is invalid

berak gravatar imageberak ( 2020-01-24 14:45:40 -0600 )edit

what do you mean by input image? because fg is valid

hernancrespo gravatar imagehernancrespo ( 2020-01-24 14:55:30 -0600 )edit

and the exact error is ?

berak gravatar imageberak ( 2020-01-24 14:59:41 -0600 )edit

Pyton has stopped working

hernancrespo gravatar imagehernancrespo ( 2020-01-24 15:09:46 -0600 )edit

Does this help cv2.waitKey()? Something is missing.

supra56 gravatar imagesupra56 ( 2020-01-24 21:15:00 -0600 )edit