简体   繁体   中英

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

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

  • Using OpenCV version 3 (not version 2)
  • Using Python 3 (not C++)
  • Using PyQt 5 (not PyQt4)

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.

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)

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