c++linuxopencviplimageximea

Extract and save an image from a IplImage


I'm working with a Ximea Camera, programming in c++ and using Ubuntu 14.04. I have a XI_IMG image and with the next conversion I'm creating an OpenCV image, copying data from xiAPI buffer to OpenCV buffer.

stat = xiGetImage(xiH, 5000, &image);
HandleResult(stat,"xiGetImage");    
XI_IMG* imagen = ℑ

IplImage * Ima = NULL;
char fname_jpg[MAX_PATH] = "";
Ima = cvCreateImage(cvSize(imagen->width, imagen->height), IPL_DEPTH_8U, 1); 
memcpy(Ima->imageData, imagen->bp, imagen->width * imagen->height);

imwrite("image1", Ima);

After doing that I should be able to save or show the image, but the next error is shown:

program.cpp:76:24:error:invalid initialization of reference of type 'cv::InputArray {aka const cv::_InputArray&}' from expression of type 'IplImage* {aka IplImage*}'

Is there any other way to obtain or save the image? What else can I do to save a jpg image?


Solution

  • You are mixing old (and obsolete) C syntax like IplImage*, cv<SomeFunction>(), etc... with current C++ syntax. To make it work be consistent and use only one style.

    Using IplImage

    int main()
    {
        IplImage* img = NULL;
        img = cvCreateImage(...);
    
        // Save 
        cvSaveImage("myimage.png", img);
    
        // Show
        cvShowImage("Image", img);
        cvWaitKey();
    
        return 0;
    }
    

    Or using new syntax (much better):

    int main()
    {
        Mat img(...);
    
        // Save 
        imwrite("myimage.png", img);
    
        // Show
        imshow("Image", img);
        waitKey();
    
        return 0;
    }
    

    Note that you don't need to memcpy the data after you initialize your Mat, but you can call one of these constructors:

    C++: Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)
    C++: Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)
    C++: Mat::Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0)
    

    Last trick, you can wrap your IplImage in a Mat and then use imwrite:

    Mat mat(Ima);
    imwrite("name.ext", mat);