Convert Mat to IplImage* in OpenCV and C/C++

12,655

Solution 1

Why do you try to use the C interface with C++ datatypes? Use the C++ interface.

cv::namedWindow(window_title, 1);
cv::imshow(window_title, file);

Solution 2

Try this:

IplImage image = file;
cvShowImage(window_title, &image);

BTW. Maybe it would really be better to use C++ OpenCV functions for showing images, it should be easier and you won't get yourself concerns on whether you have cleaned all allocated memory or not (it is good to take a look at sample code here: http://opencv.willowgarage.com/documentation/cpp/introduction.html).

Share:
12,655
Marcus Barnet
Author by

Marcus Barnet

Updated on June 15, 2022

Comments

  • Marcus Barnet
    Marcus Barnet almost 2 years

    in my application I have a Mat file which i'd like to show in a window with cvShowImage which is defined as:

    void cvShowImage( const char* name, const CvArr* image )
    

    Now, the problem is that if i pass directly the Mat image, it gives me an error conversion:

    cannot convert 'cv::Mat' to 'const CvArr*' for argument '2' to 'void cvShowImage(const char*, const CvArr*)'
    

    I tryed to search in this forum for someone with the same problem and i found this opencv documentation: http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html

    But i didn't understand how to use it.

    Can someone give me an example of how to convert Mat image to IplImage, please?

    This is my code:

    Mat file;
    Mat hogResultFrame = hogStep(temp2);
    file = hogResultFrame;
    
      cvShowImage(window_title, (const CvArr*)(file));
    

    but it gives me an error coversion.

    I hope you can help me,

    thanks a lot!

  • Marcus Barnet
    Marcus Barnet about 13 years
    I tryed also this solution, but i'm using etarion advices because it's a little bit faster.
  • Marcus Barnet
    Marcus Barnet about 13 years
    Thanks a lot! I think it's the better solution for my case
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    even, namedWindow is not really necessary. Some even have problems with it on some platforms.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    @karlphillip the problem must be that many teachers/teaching materials got used to IplImage and there is no good widely known resource on how to change from using IplImage to Mat's.
  • karlphillip
    karlphillip over 11 years
    @BarnabasSzabolcs Maybe. I'm just glad we have Stackoverflow :)