Convert a byte arry to OpenCV image in C++

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I have a byte array that represents a .jpg file that I want to convert directly to an OpenCV Mat object.

I have something like

byte* data; // Represents a JPG that I don't want to disk and then read. // What goes here to end up with the following line? cv::Mat* image_representing_the_data;

回答1:

the previously mentioned method will work fine, if it's PIXEL data.

if instead, you have a whole jpg FILE in memory, headers, compression, and all, it won't work.

in that case you want:

Mat img = imdecode(data);

which will do the same as imread(), only from memory, not from a filename



回答2:

as @bob mention, this can be done using the Mat constructor.

byte *data; cv::Mat imageWithData = cv::Mat(sizeOfData, 1, CV_8U, data).clone();

After you have created this matrix, call the reshape function with the number of rows in the image.

Mat reshapedImage = imageWithData.reshape(0, numberOfRows);


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!