Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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];, length);

    image = cv::imdecode(std::vector< int >(buffer, buffer + length), cv::IMREAD_UNCHANGED);
    return -2;
if (image.empty())
    std::cout << "Cannot read image" << std::endl;
    return -1;
cv::imshow("image", image);

How to fix the problem and make the imdecode working as imread?