opencv read jpeg image from buffer

后端 未结 2 623
臣服心动
臣服心动 2020-12-08 07:33

I have an unsigned char* buffer containing data of a jpeg image. I would like to display that image using c++ and opencv. If i do:

Mat img(Size(         


        
相关标签:
2条回答
  • 2020-12-08 08:16

    I have decompressed the JPEG image using libjpeg using the standard procedure described in the libjpeg API documentation under 'Decompression details'.

    After having decompressed the data you can use it to construct the cv::Mat. Mind you, the decompressed image is in RGB format, whereas openCV uses a BGR format so a cvtColor() operation with format CV_RGB2BGR is needed.

    0 讨论(0)
  • 2020-12-08 08:17

    I have seen many responses to this question around on the net saying that you should call libjpeg directly and bypass OpenCV's imread() routine.

    This is NOT necessary! You can use imdecode() to decode a raw image buffer from memory. The way to do it is NOT intuitive, and isn't documented enough to help people trying to do this for the first time.

    If you have a pointer/size for your raw file data (fread() directly from the .jpg, .png, .tif, files, etc...

    int    nSize = ...       // Size of buffer
    uchar* pcBuffer = ...    // Raw buffer data
    
    
    // Create a Size(1, nSize) Mat object of 8-bit, single-byte elements
    Mat rawData( 1, nSize, CV_8UC1, (void*)pcBuffer );
    
    Mat decodedImage  =  imdecode( rawData /*, flags */ );
    if ( decodedImage.data == NULL )   
    {
        // Error reading raw image data
    }
    

    That's IT!

    Hope this helps someone in the future.

    0 讨论(0)
提交回复
热议问题