OpenCV: IplImage versus Mat, which to use?

Opencv

Opencv Problem Overview


I'm pretty new to OpenCV (about 2 months now). I have the book Learning OpenCV by Bradski and Kaehler. My question is, if I want to do everything in a 2.0+ manner, when should I use Matrices (Mat) and when should I use IplImage?

Bradky's book states upfront (Preface) that it's written based on OpenCV 2.0, and it mostly uses IplImage in its sample code, but the more recent online documentation makes it sound like Mat is now a catch-all data type for images, masks, etc, kind of like a basic matrix in Matlab. This leaves me wondering if IplImage should be considered obsolete.

So, should I be totally avoiding IplImages when writing new code? Or is there important stuff IplImages allow me to do that Mats do not?

Thanks.

Opencv Solutions


Solution 1 - Opencv

IplImage has been in OpenCV since the very beginning. It is a part of the C interface for OpenCV. You need to allocate and deallocate memory for IplImage structures yourself. (remember the cvReleaseImage commands?)

The new Mat structure is a part of the C++ structure. So obviously it is object oriented. Also, it manages all memory for you! It keeps a track of references to it. Then the number of references goes to zero, it deallocates automatically. This is one superb feature!

Go for Mat. It should be easy to translate code from the IplImage thingy to the Mat thingy if you are using some IDE that has Intellisense (it drops down a list of possible functions, variables, etc as you type)

Solution 2 - Opencv

I would highly recommend using Mat. I've been using it for a while, and it's great. The member functions and the matrix expressions make things much simpler than dealing with IplImage and as you said it's a catch-all data type.

Go for Mat!

Solution 3 - Opencv

I would say this would actually depend on the platform that you are going to run your application on. If you are developing an application for an embedded system you would be more likely to use C. In that case you would have to use IplImage. Quoting from the tutorial:

> The main downside of the C++ interface is that many embedded > development systems at the moment support only C. Therefore, unless > you are targeting embedded platforms, there’s no point to using the > old methods (unless you’re a masochist programmer and you’re asking > for trouble).

Solution 4 - Opencv

thanks for the help.

I also discovered since posting this question that a function with a Mat as a argument can take an IplImage directly in the place of that Mat argument, which makes it pretty easy to update your code in chunks if it's already broken into convenient functions. Just change the function arguments from IplImage* to Mat, then modify the function to work on a Mat. Other code calling that function should still work fine (it has in my experience).

Solution 5 - Opencv

Quick update:

With the advent of OpenCV 4, the answer to the question will be more straightforward as the IplImage and all of what they now call "the legacy C API" will be progressively removed. In OpenCV 4.0 "alpha", IplImage is already gone - as is CvMat.

So, if you work with OpenCV4.0+, use the Mat Class... because you don't have the choice.

[NB: Of course, if you still use older version of OpenCV, the question is still relevant.]

Solution 6 - Opencv

I'd suggest Mat. The garbage collection is automatic, and thus the application is more reliable and has fewer memory leaks. Also, Mat is a newer way of data storage, so if you are a newbie, just starting off with OpenCV, Mat is newer, and requires less careful coding to make a complete application.

Compatibility is one thing that Mat will be a tad worse in. IplImage has been available longer and thus, has a greater compatibility with most things. I believe you can use IplImage with Mat too, and if not, IplImage>Mat is also quite simple to perform.

Since Iplimage has been available for a much longer period of time, you will probably find a greater selection of samples.

Here are my two cents: As a rookie(still learning tricks) in vision processing with OpenCV, pick one, Mat or IplImage and get really good at it. However, learn at least the basics of the other so you know what to do if you need to use a function that doesn't not compatible with the other.

But to repeat myself, if you are a newbie, try to start with Mat. Since it is a newer implementation, it is easier to learn and get right!

Solution 7 - Opencv

Mat is much easier and easier to use. It represents the image as a matrix. it is faster too. I would recommend Mat over IplImage.

Solution 8 - Opencv

Iplimage is some structure of C interface in Opencv, and Mat is more suitable for C++ program and support some C++ styles like ref parameter and stream operator, etc. Although they are all object oriented programming, Mat includes more methods itself than Iplimage does, like create and release, which Ipliamge would call some cvXXX interface to complete. Moreover, Mat is a new structure from Opencv2, which I think is symbol of development for the old ones. I wish it help.

Solution 9 - Opencv

I believe using cv::Mat is much more convenient. it is more generic. We can see IplImage being a subset of cv::Mat. The default datatype for IplImage is unsigned integer while that for cv::Mat is double. So it is much more easy to use Mat for any kind of mathematical operation.

Solution 10 - Opencv

I began to use opencv about in 2012 or so .So I began with Mat,which is powerful and easy to use.BUT to read or reuse these code that was "old",i had to kown sth about iplimages,it is easy to use,also. But the future is Mat,I think.And dont forget Mat in a CLASS,that means,you dont need to release a Mat.On the other hand,you SHOULD to free a implimage. My English is poor,sorry.

Solution 11 - Opencv

When writing a Blob detection routine I noticed that using

IplImage* img;
uchar* image_src = (uchar*)img->imageData;
image_src[x+y*width] = ...;

Is much faster than using

Mat image;
image.at<uchar>(x,y) = ...;

About 5x faster. Some of this may be because I used a nested X,Y loop for Mat and a single loop for IplImage. But if you have to write any routines that work directly off pixels I would stick with IplImage.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSSilkView Question on Stackoverflow
Solution 1 - OpencvUtkarsh SinhaView Answer on Stackoverflow
Solution 2 - OpencvJacobView Answer on Stackoverflow
Solution 3 - OpencvSunnyView Answer on Stackoverflow
Solution 4 - OpencvSSilkView Answer on Stackoverflow
Solution 5 - OpencvgodotView Answer on Stackoverflow
Solution 6 - Opencvyash101View Answer on Stackoverflow
Solution 7 - OpencvMojo JojoView Answer on Stackoverflow
Solution 8 - Opencvuser2226360View Answer on Stackoverflow
Solution 9 - OpencvUtkarsh DeshmukhView Answer on Stackoverflow
Solution 10 - OpencvjsxyheluView Answer on Stackoverflow
Solution 11 - OpencvMichView Answer on Stackoverflow