Ask Your Question
1

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

asked 2013-06-20 09:58:39 -0600

prerna1 gravatar image

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-06-20 11:44:58 -0600

Nesbit gravatar image

updated 2013-06-20 11:47:57 -0600

Normalize and then convert cv::Math src to QImage qim:

int w=src.cols;
int h=src.rows;
QImage qim(w,h,QImage::Format_RGB32);
QRgb pixel;
Mat im;
normalize(src.clone(),im,0.0,255.0,CV_MINMAX,CV_8UC1);
for(int i=0;i<w;i++)
{
    for(int j=0;j<h;j++)
    {
        int gray = (int)im.at<unsigned char>(j, i);
        pixel = qRgb(gray,gray,gray);
        qim.setPixel(i,j,pixel);
    }
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-20 09:58:39 -0600

Seen: 3,992 times

Last updated: Jun 20 '13