Connor
Connor

Reputation: 698

Convert OpenCV 3 (iplImage) to PyQt5 QImage/QPixmap in Python

Before this question is dismissed as a duplicate, please note:

Goal: Trying to stream OpenCV webcam video frames in a PyQt5 GUI window

# For testing, I'm simply reading in a single image instead of a video frame

frame = cv2.imread('Koala.jpg',0)     # this is a numpy.ndarray object
height, width = frame.shape         
my_image = QImage(frame.tostring(), width, height, QImage.Format_RGB888)

# By here, things seem okay. print(mimage) basically says <QImage object at 0x32243>

# my_image.rgbSwapped()               # This line crashes program
pixmap = QPixmap.fromImage(mimage)    # This line crashes program as well

I'm not sure how to debug this anymore. No error tracebacks are printed to console. Any help is appreciated. Thanks in advance.

Upvotes: 4

Views: 3925

Answers (1)

AdityaIntwala
AdityaIntwala

Reputation: 2576

you need to convert the openCv image to QImage first and them use pixmap to display the image as follows this is because pixmap supports QImage format.

   height, width, bytesPerComponent = Img.shape
   bytesPerLine = 3 * width
   cv2.cvtColor(Img, cv2.COLOR_BGR2RGB, Img)                                           
   QImg = QImage(Img.data, width, height, bytesPerLine,QImage.Format_RGB888)

now use this QImg to pixmap

   pixmap = QPixmap.fromImage(QImg)

Upvotes: 3

Related Questions