Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can I convert cv::Mat of type CV_8UC1 to a QImage in Qt?

I am trying to fetch the depth frame from kinect and convert that into a cv::Mat & subsequently a QImage. I used the following code to convert a depth frame to an OpenCV Mat object:

VideoFrameRef depthFrame = depthListener.getFrame();
cv::Mat depthDisp (depthFrame.getVideoMode().getResolutionY(), depthFrame.getVideoMode().getResolutionX(), CV_16UC1, (unsigned char*)depthFrame.getData());
cv::normalize(depthDisp, depthDisp, 0, 255, CV_MINMAX, CV_8UC1);
imshow("d", depthDisp);

THE IMAGE DISPLAYS WELL! So, now I have to convert from an OpenCV Mat of type CV_8UC1 to a QImage. I tried the following:

QImage dispQDepth = new Qimage((uchar*)depthDisp.data, depthDisp.size().width, depthDisp.size().height, QImage::Format_Indexed8);

And then displayed the QImage using a QLabel:

QLabel *imgDispLabel = new QLabel("");
imgDispLabel->setPixmap(QPixmap::fromImage(dispQDepth));

But, THIS DOES NOT WORK! What can I do to get it to work?