0

I know there were already questions like this in the past but all the answers seem not to work anymore since some functions are deprecated so I hope you can help me.

That's what I try to do :

IplImage* image = 0;
Mat frame; 
image = cvCloneImage(&(IplImage)frame); 

Basically I get an error because of the last line :

E0312 no suitable user-defined conversion from "cv::Mat" to "IplImage" exists

By the way if instead there is a manual way to convert Mat to IplImage it's also great for me.

PS : I'm trying to use an old code so it doesn't bother me if Mat is the new standard in OpenCV and not advised using IplImage.

PS2 : If there is an old version of OpenCV that can do this without errors it's also OK.

Thank you!

EigenMan
  • 3
  • 3
  • conversion shouldnt be a problem. IplImage should provide a constructor that accepts data pointers and step values. I worked with IplImages and IPP about 12 years ago. But I dot know whether cloning an image (the cvCloneImag call) is still available in your environment?!? – Micka Jan 17 '22 at 14:43
  • This looks like an answer that applies https://stackoverflow.com/a/4664671/4117728. Manually doing the conversion is unlikely deprecated – 463035818_is_not_an_ai Jan 17 '22 at 14:44
  • 1
    or how about this https://stackoverflow.com/a/13683381/4117728 ? I don't understand that you say you are using an old version hence `IplImage` is fine, but at the same time you say solutions you found are outdated. The question would be more clear if you show the solutions you found and why they didnt work – 463035818_is_not_an_ai Jan 17 '22 at 14:46
  • I tried all of the solutions above and suprisingly they don't work since they get the same error ( no conversion from cv::Mat to IplImage ), btw if this can help I'm using OpenCV 4.5.5 (latest) – EigenMan Jan 17 '22 at 14:48
  • 1
    In my opencv 4.5.x environment I dont even have access to the IplImage type anymore. Can you tell a bit about your environment? I think you only have to set the `ipl.imageData = mat.data; ipl.width=img.cols; ipl.height=img.rows; ipl.widthStep=img.step;` but you might need to deep-copy the iplImage afterwards, before the cv::Mat leaves scope. – Micka Jan 17 '22 at 15:04
  • Not sure that I understand the question but I use OpenCV 4.5.5 in VS2019 (sorry I'm a total rookie) – EigenMan Jan 17 '22 at 15:10
  • What do you #include to get access to the IplImage type? – Micka Jan 17 '22 at 16:52

2 Answers2

0

I try to make hand-crafted conversion. In my enviroment it works well (OpenCV 2.4.9). Be carefull with pix_type variable value.

Mat frame_gray = imread("img.bmp");
int pix_type = IPL_DEPTH_8U;
IplImage* img = cvCreateImage(frame_gray.size(), pix_type , frame_gray.channels());
img->imageData = reinterpret_cast<char*>(frame_gray.data);
img->widthStep = frame_gray.step;
Prog_rez
  • 26
  • 2
  • but as Micka says, you should think about deep-copy. I think. Micka: https://stackoverflow.com/questions/70743095/problem-with-deprecated-conversion-from-cvmat-to-iplimage/70751337#comment125062825_70743095 – Prog_rez Jan 18 '22 at 11:36
  • The OP says they're [using OpenCV 4.5.5](https://stackoverflow.com/questions/70743095/problem-with-deprecated-conversion-from-cvmat-to-iplimage#comment125062480_70743095). Does your approach work in 4.5.5? – beaker Jan 18 '22 at 17:42
  • In my OpenCV 4.5.5 version does not defined IplImage type. Presented solution is redundant in memory (extra memory allocation in `cvCreateImage(frame_gray.size(), pix_type , frame_gray.channels())`). The better way is: `Mat frame_gray = imread("img.bmp"); int pix_type = IPL_DEPTH_8U; IplImage img; img.height = frame_gray.rows; img.width = frame_gray.cols; img.nChannels = frame_gray.channels(); img.depth = pix_type; img. imageSize = frame_gray.rows * frame_gray.step; img.imageData = reinterpret_cast(frame_gray.data); img.widthStep = frame_gray.step;` – Prog_rez Jan 19 '22 at 08:05
  • To avoid problems with deep-copy and `cv::Mat` scope, just always contain your data in `cv::Mat` type and use conveter in places, only before using `IplImage` type – Prog_rez Jan 19 '22 at 08:15
0

On modern versions of OpenCV (3.9 and later) you can use function cvIplImage:

Mat matImage;
IplImage iplImage = cvIplImage(matImage);
Elmir
  • 110
  • 11