imread succeeds to read image, but imdecode cannot decode the buffer
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
?
may be you should try (http://stackoverflow.com/questions/42...) cv::imdecode(std::vector (buffer, buffer + length), cv::IMREAD_UNCHANGED);
you can say that vector< char > fill fix the problem...
vector<uchar>
, not intI am using cpprest and there is some problem of
vector< unsigned char >
changed tovector< unsigned int >
... IMHO, it seems that it has a bug...but if the buffer is incomplete, in fact the image is incomplete (it has a gray part at the end - aka bottom), is there a difference between the imdecode from OpenCV 2.4.9 and OpenCV 2.4.11?