imread succeeds to read image, but imdecode cannot decode the buffer

asked 2015-03-18 05:38:33 -0600

thdrksdfthmn gravatar image

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.

image

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?

edit retag flag offensive close merge delete

Comments

1

may be you should try (http://stackoverflow.com/questions/42...) cv::imdecode(std::vector (buffer, buffer + length), cv::IMREAD_UNCHANGED);

LBerger gravatar imageLBerger ( 2015-03-18 05:54:42 -0600 )edit

you can say that vector< char > fill fix the problem...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-18 05:57:39 -0600 )edit
1

vector<uchar> , not int

berak gravatar imageberak ( 2015-03-18 05:58:14 -0600 )edit

I am using cpprest and there is some problem of vector< unsigned char > changed to vector< unsigned int >... IMHO, it seems that it has a bug...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-18 06:06:09 -0600 )edit

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?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-18 08:38:02 -0600 )edit