How to convert QImage to opencv Mat

Viewed 27964

I have searched on the internet but I cannot find a method of converting a QImage(or QPixmap) to a OpenCV Mat. How would I do this?.

Any help is appreciated.

5 Answers

The answer to this with Qt 5.11 (and probably some earlier versions):

    cv::Mat mat(image.height(), image.width(),CV_8UC3, image.bits());  
    // image.scanline() does not exist, 
    //and height/width is interchanged for a matrix

Again the QImage is assumed to be RGB888 (ie QImage::Format_RGB888)

Related