我有一个字节数组,表示我想直接转换为OpenCV Mat对象的.jpg文件。
我有类似的东西
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;
如果它是PIXEL数据,前面提到的方法将正常工作。
如果相反,你在内存,标题,压缩和所有内容中都有一个完整的jpg文件,它将无法正常工作。
在这种情况下你想要:
Mat img = imdecode(data);
这将与imread()
相同,仅来自内存,而不是文件名
正如@bob提到的,这可以使用Mat构造函数完成。
byte *data;
cv::Mat imageWithData = cv::Mat(sizeOfData, 1, CV_8U, data).clone();
创建此矩阵后,使用图像中的行数调用reshape函数。
cv::Mat reshapedImage = imageWithData.reshape(0, numberOfRows);
cv::Mat GetImageFromMemory(uchar* image, int length, int flag)
{
std::vector<uchar> data = std::vector<uchar>(image, image + length);
cv::Mat ImMat = imdecode(data, flag);
return ImMat;
}
参数“flag”是指图像的类型,灰度,颜色,以及已定义opencv的任何depht ..