[Qt-interest] OpenCV native image cv::Mat and QImage

frares at gmail.com frares at gmail.com
Mon Sep 26 21:23:09 CEST 2011


Hi, All.

If I understood correctly some code found on the net mixing Qt's QImage and  
cv::Mat, those create a new QImage for each time a cv::Mat is  
altered/created.

I have managed to create, in a quite similar way, a QImage that is allways  
reflecting the contents of a cv::Mat, that is, if the Mat is altered, the  
QImage is automatically updated because (I think) the raw data pointers  
point to the same place.

I have added that QImage as a background brush to the QGraphicsView.

For example:

in the header file:

QGraphicsScene mGS;
QGraphicsView mGV;
cv::Mat mWorkMat;
QImage mWorkQImage;

in the implementation file:

mWorkMat = cv::Mat(480, 640, CV_8UC3);
mWorkQImage = QImage( mWorkMat .data,
mWorkMat .cols,
mWorkMat .rows,
mWorkMat .step,
QImage::Format_RGB888);
mGV->setScene(&mGS);
mGV->setAlignment(Qt::AlignLeft | Qt::AlignTop);
mGV->setMinimumSize(mMatTrabalho.cols, mMatTrabalho.rows);
mGV->setRenderHints( QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::SmoothPixmapTransform |
QPainter::HighQualityAntialiasing );
mGS.setBackgroundBrush(QBrush(mWorkQImage ));

Then I messed a bit with the cv::Mat raw data to create some color gradient  
bars, inserted a cv::circle and then blurred everything with  
cv::GaussianBlur . Everything done on the cv::Mat appeared on that QImage  
on the background of the QGraphicsView.

I am not sure if this is something that could be used as stable, nor if it  
is cross platform. I mean, if the raw data is being altered by something  
else, will this sample code always work correctly?

I would appreciate any comments, hints, suggestions, critics, etc. ;-)

Best regards
Francisco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110926/24c41e07/attachment.html 


More information about the Qt-interest-old mailing list