PyQt/PySide: How do I convert QImage into OpenCV's MAT format
Asked Answered
A

2

3

I'm looking to create a function for converting a QImage into OpenCV's (CV2) Mat format from within the PyQt.

How do I do this? My input images I've been working with so far are PNGs (either RGB or RGBA) that were loaded in as a QImage.

Ultimately, I want to take two QImages and use the matchTemplate function to find one image in the other, so if there is a better way to do that than I'm finding now, I'm open to that as well. But being able to convert back and forth between the two easily would be ideal.

Thanks for your help,

Argentum answered 23/8, 2013 at 15:6 Comment(1)
the images in cv2 are just numpy arrays. so if you can convert from QImage to that, you're done.Wham
A
2

After much searching on here, I found a gem that got me a working solution. I derived much of my code from this answer to another question: https://mcmap.net/q/1021149/-how-can-access-to-pixel-data-with-pyqt-39-qimage-scanline

The key challenge I had was in how to correctly use the pointer. The big thing I think I was missing was the setsize function.

Here's my imports:

import cv2
import numpy as np

Here's my function:

def convertQImageToMat(incomingImage):
    '''  Converts a QImage into an opencv MAT format  '''

    incomingImage = incomingImage.convertToFormat(4)

    width = incomingImage.width()
    height = incomingImage.height()

    ptr = incomingImage.bits()
    ptr.setsize(incomingImage.byteCount())
    arr = np.array(ptr).reshape(height, width, 4)  #  Copies the data
    return arr
Argentum answered 26/8, 2013 at 17:50 Comment(1)
How to remove alpha channel from QImage to use it as 3 channel numpy array?Compel
J
0

I tried the answer given above, but couldn't get the expected thing. I tried this crude method where i saved the image using the save() method of the QImage class and then used the image file to read it in cv2

Here is a sample code

def qimg2cv(q_img):
    q_img.save('temp.png', 'png')
    mat = cv2.imread('temp.png')
    return mat

You could delete the temporary image file generated once you are done with the file. This may not be the right method to do the work, but still does the required job.

Jennijennica answered 15/4, 2019 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.