[英]How to use cv::imdecode, if the contents of an image file are in a char array?
I have a jpeg image in buffer jpegBuffer. 我在缓冲区jpegBuffer中有一个jpeg图像。 I'm trying to pass it to cv::imdecode function:
我试图将它传递给cv :: imdecode函数:
Mat matrixJprg = imdecode(Mat(jpegBuffer), 1);
I get this error: 我收到此错误:
/home/richard/Desktop/richard/client/src/main.cc:108: error: no matching function for call to ‘cv::Mat::Mat(char*&)’
This is how I fill jpegBuffer: 这是我填写jpegBuffer的方式:
FILE* pFile;
long lSize;
char * jpegBuffer;
pFile = fopen ("img.jpg", "rb");
if (pFile == NULL)
{
exit (1);
}
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file.
jpegBuffer = (char*) malloc (lSize);
if (jpegBuffer == NULL)
{
exit (2);
}
// copy the file into the buffer.
fread (jpegBuffer, 1, lSize, pFile);
// terminate
fclose (pFile);
Mat has no constructor that takes a char* argument. Mat没有构造函数接受char *参数。 Try this instead:
试试这个:
std::ifstream file("img.jpg");
std::vector<char> data;
file >> std::noskipws;
std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data));
Mat matrixJprg = imdecode(Mat(data), 1);
EDIT: 编辑:
You should also take a look at LoadImageM . 您还应该看看LoadImageM 。
If you have your data already in a char* buffer one way is to copy the data into an std::vector. 如果您的数据已经存在于char *缓冲区中,则一种方法是将数据复制到std :: vector中。
std::vector<char> data(buf, buf + size);
I had to do the-same thing and my image data was already in char
array format and was arriving from a network and a plugin source. 我必须做同样的事情,我的图像数据已经是
char
数组格式,并从网络和插件源到达。 The current answer shows how to do this but it requires copying the data into a vector first which is a waste of time and resources. 当前的答案显示了如何执行此操作, 但它需要先将数据复制到向量中 ,这样会浪费时间和资源。
This is possible to do directly without creating a copy of it. 这可以直接进行而无需创建它的副本。 You were so close with your
imdecode(Mat(jpegBuffer), 1);
你的
imdecode(Mat(jpegBuffer), 1);
非常接近imdecode(Mat(jpegBuffer), 1);
code in your question. 你问题中的代码。
You need to use the constructor overload for the Mat
class below: 您需要为下面的
Mat
类使用构造函数重载:
Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
To create this Mat
, pass 1
to the rows, the size of the array to the cols, CV_8UC1
to the type and the char array itself to the data param. 要创建此
Mat
,请将1
传递给行,将数组的大小传递给cols,将CV_8UC1
传递给类型,将char数组本身传递给数据参数。 Pass this Mat
to the cv::imdecode
function with the mat as the first param and CV_LOAD_IMAGE_UNCHANGED
as the second param. 将此
Mat
传递给cv::imdecode
函数,其中mat为第一个参数, CV_LOAD_IMAGE_UNCHANGED
为第二个参数。
Basic example : 基本示例 :
char *buffer = dataFromNetwork;
int bufferLength = sizeOfDataFromNetwork;
cv::Mat matImg;
matImg = cv::imdecode(cv::Mat(1, bufferLength, CV_8UC1, buffer), CV_LOAD_IMAGE_UNCHANGED);
Complete Example (Reads file named "test.jpg" into char array and uses imdecode
to decode the data from the char array then displays it): 完整示例 (将名为“
imdecode
”的文件读入char数组并使用imdecode
解码char数组中的数据然后显示它):
int main() {
//Open image file to read from
char imgPath[] = "./test.jpg";
ifstream fileImg(imgPath, ios::binary);
fileImg.seekg(0, std::ios::end);
int bufferLength = fileImg.tellg();
fileImg.seekg(0, std::ios::beg);
if (fileImg.fail())
{
cout << "Failed to read image" << endl;
cin.get();
return -1;
}
//Read image data into char array
char *buffer = new char[bufferLength];
fileImg.read(buffer, bufferLength);
//Decode data into Mat
cv::Mat matImg;
matImg = cv::imdecode(cv::Mat(1, bufferLength, CV_8UC1, buffer), CV_LOAD_IMAGE_UNCHANGED);
//Create Window and display it
namedWindow("Image from Char Array", CV_WINDOW_AUTOSIZE);
if (!(matImg.empty()))
{
imshow("Image from Char Array", matImg);
}
waitKey(0);
delete[] buffer;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.