OpenCV to use in memory buffers or file pointers

33,302

Solution 1

There are a couple of undocumented functions in the SVN version of the libary:

CV_IMPL CvMat* cvEncodeImage( const char* ext, 
                              const CvArr* arr, const int* _params )

CV_IMPL IplImage* cvDecodeImage( const CvMat* _buf, int iscolor )

Latest check in message states that they are for native encoding/decoding for bmp, png, ppm and tiff (encoding only).

Alternatively you could use a standard image encoding library (e.g. libjpeg) and manipulate the data in the IplImage to match the input structure of the encoding library.

Solution 2

This worked for me

// decode jpg (or other image from a pointer)
// imageBuf contains the jpg image
    cv::Mat imgbuf = cv::Mat(480, 640, CV_8U, imageBuf);
    cv::Mat imgMat = cv::imdecode(imgbuf, CV_LOAD_IMAGE_COLOR);
// imgMat is the decoded image

// encode image into jpg
    cv::vector<uchar> buf;
    cv::imencode(".jpg", imgMat, buf, std::vector<int>() );
// encoded image is now in buf (a vector)
    imageBuf = (unsigned char *) realloc(imageBuf, buf.size());
    memcpy(imageBuf, &buf[0], buf.size());
//  size of imageBuf is buf.size();

I was asked about a C version instead of C++:

#include <opencv/cv.h>
#include <opencv/highgui.h>

int
main(int argc, char **argv)
{
    char *cvwin = "camimg";

    cvNamedWindow(cvwin, CV_WINDOW_AUTOSIZE);

    // setup code, initialization, etc ...
    [ ... ]

    while (1) {      
        // getImage was my routine for getting a jpeg from a camera
        char *img = getImage(fp);
        CvMat mat;

   // substitute 640/480 with your image width, height 
        cvInitMatHeader(&mat, 640, 480, CV_8UC3, img, 0);
        IplImage *cvImg = cvDecodeImage(&mat, CV_LOAD_IMAGE_COLOR);
        cvShowImage(cvwin, cvImg);
        cvReleaseImage(&cvImg);
        if (27 == cvWaitKey(1))         // exit when user hits 'ESC' key
        break;
    }

    cvDestroyWindow(cvwin);
}

Solution 3

I'm assuming you're working in linux. From libjpeg.doc:

The rough outline of a JPEG compression operation is:
Allocate and initialize a JPEG compression object
Specify the destination for the compressed data (eg, a file)
Set parameters for compression, including image size & colorspace

jpeg_start_compress(...);
while (scan lines remain to be written)
jpeg_write_scanlines(...);

jpeg_finish_compress(...);
Release the JPEG compression object

The real trick for doing what you want to do is providing a custom "data destination (or source) manager" which is defined in jpeglib.h:

struct jpeg_destination_mgr {
  JOCTET * next_output_byte;    /* => next byte to write in buffer */
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */

  JMETHOD(void, init_destination, (j_compress_ptr cinfo));
  JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
  JMETHOD(void, term_destination, (j_compress_ptr cinfo));
};

Basically set that up so your source and/or destination are the memory buffers you want, and you should be good to go.

As an aside, this post could be a lot better but the libjpeg62 documentation is, quite frankly, superb. Just apt-get libjpeg62-dev and read libjpeg.doc and look at example.c. If you run into problems and can't get something to work, just post again and I'm sure someone will be able to help.

Share:
33,302
The Unknown
Author by

The Unknown

Updated on February 07, 2020

Comments

  • The Unknown
    The Unknown over 4 years

    The two functions in openCV cvLoadImage and cvSaveImage accept file path's as arguments.

    For example, when saving a image it's cvSaveImage("/tmp/output.jpg", dstIpl) and it writes on the disk.

    Is there any way to feed this a buffer already in memory? So instead of a disk write, the output image will be in memory.

    I would also like to know this for both cvSaveImage and cvLoadImage (read and write to memory buffers). Thanks!


    My goal is to store the Encoded (jpeg) version of the file in Memory. Same goes to cvLoadImage, I want to load a jpeg that's in memory in to the IplImage format.

    • mpen
      mpen about 15 years
      You want to write the image to memory? But dstIpl already is in memory, what exactly are you hoping to accomplish? You can access the image data with dstIpl->imageData or ->data or something.
    • mpen
      mpen about 15 years
      Likewise, you could manipulate the iplImage's data buffer to load an image that's already in memory... just has to be in BGR format.
    • M456
      M456 about 15 years
      I think the original poster wants to encode the image but save a disk read/write.
    • Mr Fooz
      Mr Fooz about 15 years
      This kind of thing is especially useful when you want to do things like embed a compressed image into a larger binary. For example, PDF files can have embedded PNG and JPG images. A program that directly creates PDF files would want to avoid having to create temporary PNG or JPG files on disk, then transfer them into the PDF stream.
    • The Unknown
      The Unknown about 15 years
      To clear the confusion: Yes, I want to store the encoded (ex. jpeg) version of the file in Memory instead of writing to disk.
  • Ankit Roy
    Ankit Roy about 15 years
    PLEASE don't rely on undocumented functions, you'll create a maintenance nightmare for yourself. The reason they are not documented is because the designers don't expect them to remain stable over time. They could disappear with the next version!
  • M456
    M456 about 15 years
    I think that warning is implied with any development version. Also, documentation has always been an issue with OpenCV many of the stable functions are still undocumented.
  • Mr Fooz
    Mr Fooz over 10 years
    If you downvote, please leave a comment explaining why this answer is wrong. That way it can be improved.
  • Poul K. Sørensen
    Poul K. Sørensen over 10 years
    is it not the intention that imdecode should decode the image, so why do imageBuf need to be placed in a cv::mat ? I am having problems where i have a unsigned char * containing raw data i loaded over the wire (its a tiff image), but after doign imdecode on the raw data the Mat is having its mat.data == 0 and no errors given.
  • codeDr
    codeDr over 9 years
    @pksorensen imageBuf needs to be put into a Mat so that it can be accepted as a parameter to imdecode() unless there's C++ magic that can turn it into InputArray that imdecode() wants. Reasons for not decoding your TIFF image would be (1) the Mat passed in to imgdecode rows*cols was less than 1, (2) TIFF support was not compiled into your OpenCV library or (3) that the TIFF decoder did not recognize your image buffer as a valid TIFF image.