PDA

View Full Version : OpenCV , Mat image from QByteArray



adubaldo
2nd March 2015, 16:42
Hi all :),

I'm trying to show an image with opencv function cv::imshow("windowsName",cv::Mat) starting from QByteArray. The problem is that the image appears duplicated:

Like this:
10983

Here the code:



void Locus::getImages(QFile* file, Mat& matImg)
{
QByteArray imagesData;

if(file->exists())
{
//get byteArray from raw data file
file->open(QFile::ReadOnly);
imagesData = file->readAll();
file->close();

//Create qimage
QImage img((const uchar*)imagesData.data(),WIDTH,HEIGHT,QImage::Form at_RGB16);

//create mat image from qimage
matImg = this->qImage2Mat(img);

imshow("window",matImg);
waitKey();
}
}

Mat Locus::qImage2Mat(const QImage& src)
{

Mat tmp(src.height(),src.width(), CV_8UC3,(uchar*)src.bits(),src.bytesPerLine());
Mat res;
cvtColor(tmp, res,CV_RGB2GRAY);
return res;
}


Any help?
ps: i think the problem is the image format, but i've tried many ways help me xD.

adubaldo
3rd March 2015, 09:13
The problem was the format, it's not CV_8UC3 but CV_16UC1(16 bit - 1 channel ).
This is just my case ,for this specific problem , but when you work on image processing you must keep in your mind that the format convertions are very important.

franco.amato
16th May 2018, 16:12
You can create a Mat image directly from QByteArray without the need of creating a QImage

Mat img = Mat(WIDTH,HEIGHT, CV_8UC1, imagesData.data());

Regards