Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 429
Connor Avatar asked Oct 19 '25 10:10

Connor


1 Answers

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)
like image 126
AdityaIntwala Avatar answered Oct 22 '25 00:10

AdityaIntwala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!