I have met a strange case: I get an empty Mat if I try to decode an image buffer, but using imread is succeeding. This happens for JFIF buffers.
This is my code:
// cv::Mat image = cv::imread("/home/stefan/Downloads/LCuS14496u60.jpg", cv::IMREAD_UNCHANGED);
std::ifstream f("/home/stefan/Downloads/14266748483487155.jpg", std::ios::binary);
cv::Mat image;
if (f.is_open())
{
f.seekg(0, std::ios::end);
unsigned int length = f.tellg();
f.seekg(0, std::ios::beg);
char* buffer = new char[length];
f.read(buffer, length);
f.close();
image = cv::imdecode(std::vector< int >(buffer, buffer + length), cv::IMREAD_UNCHANGED);
}
else
{
return -2;
}
if (image.empty())
{
std::cout << "Cannot read image" << std::endl;
return -1;
}
cv::imshow("image", image);
cv::waitKey(0);
How to fix the problem and make the imdecode
working as imread
?