25

I searched to convert an IplImage* to Mat, but all answers were about the conversion to cvMat.

How, can I do it? and what is the difference between Mat and cvMat?

Thanks in advance

  • Regarding cvMat and Mat , see the link: http://stackoverflow.com/questions/11037798/difference-between-cvmat-mat-and-ipimage – Barshan Das Apr 10 '13 at 11:58

5 Answers5

44

For the records: taking a look at core/src/matrix.cpp it seems that, indeed, the constructor cv::Mat(IplImage*) has disappeared.

But I found this alternative:

IplImage * ipl = ...;
cv::Mat m = cv::cvarrToMat(ipl);  // default additional arguments: don't copy data.
Moein
  • 1,562
  • 1
  • 15
  • 23
  • Original [source](https://answers.opencv.org/question/23440/any-way-to-convert-iplimage-to-cvmat-in-opencv-300/) for this answer... – M_N1 Jul 20 '20 at 11:30
14

here is a good solution

Mat(const IplImage* img, bool copyData=false);
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
11

The recommended way is the cv::cvarrToMat function

cv::Mat - is base data structure for OpenCV 2.x

CvMat - is old C analog of cv::Mat

Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
5

Check out the Mat documentation.

// converts old-style IplImage to the new matrix; the data is not copied by default
Mat(const IplImage* img, bool copyData=false);
Safir
  • 902
  • 7
  • 9
4
  • cv::Mat or Mat, both are same.

  • Mat has a operator CvMat() so simple assignment works

Convert Mat to CvMat

Mat mat = ---------;
CvMat cvmat = mat;

Convert CVMat to Mat

Mat dst = Mat(cvmat, true);  

Convert Mat to IplImage*

> For Single Channel

IplImage* image = cvCloneImage(&(IplImage)mat); 

> For Three Channel

IplImage* image = cvCreateImage(cvSize(mat.cols, mat.rows), 8, 3);
IplImage ipltemp = mat;
cvCopy(&ipltemp, image);

Hope this helps you. Cheers :)

Abc
  • 824
  • 2
  • 7
  • 20