I have a byte array that represents a .jpg file that I want to convert directly to an OpenCV Mat object.
我有一个字节数组,表示我想直接转换为OpenCV Mat对象的.jpg文件。
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;
2 个解决方案
#1
19
the previously mentioned method will work fine, if it's PIXEL data.
如果是PIXEL数据,前面提到的方法将正常工作。
if instead, you have a whole jpg FILE in memory, headers, compression, and all, it won't work.
如果相反,你在内存,标题,压缩和所有内容中都有一个完整的jpg文件,它将无法正常工作。
in that case you want:
在这种情况下你想要:
Mat img = imdecode(data);
which will do the same as imread()
, only from memory, not from a filename
这将与imread()完全相同,仅来自内存,而不是文件名
#2
13
as @bob mention, this can be done using the Mat constructor.
正如@bob提到的,这可以使用Mat构造函数完成。
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.
创建此矩阵后,使用图像中的行数调用reshape函数。
Mat reshapedImage = imageWithData.reshape(0, numberOfRows);
#1
19
the previously mentioned method will work fine, if it's PIXEL data.
如果是PIXEL数据,前面提到的方法将正常工作。
if instead, you have a whole jpg FILE in memory, headers, compression, and all, it won't work.
如果相反,你在内存,标题,压缩和所有内容中都有一个完整的jpg文件,它将无法正常工作。
in that case you want:
在这种情况下你想要:
Mat img = imdecode(data);
which will do the same as imread()
, only from memory, not from a filename
这将与imread()完全相同,仅来自内存,而不是文件名
#2
13
as @bob mention, this can be done using the Mat constructor.
正如@bob提到的,这可以使用Mat构造函数完成。
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.
创建此矩阵后,使用图像中的行数调用reshape函数。
Mat reshapedImage = imageWithData.reshape(0, numberOfRows);