0
#include <opencv2/opencv.hpp>
...

IplImage* img = cvLoadImage("TestImage.jpg", 1);
Mat mtx(img);

This code should convert the img to Mat format. However, when I implement this I receive an error saying "No matching constructor for initialization of cv::Mat".

Where am I doing wrong? The syntax should be correct so I assume the problem lies somewhere else. I am using OpenCV 3.1.0 and Xcode 7.2.1 in OSX Yosemite.

Thank you in advance

  • 3
    Not sure why it's not working, but is there some reason why you can't use [`cv::imread(...)`](http://docs.opencv.org/3.1.0/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56)? – Dan Mašek Apr 01 '16 at 04:28
  • 2
    Because they removed that constructor in 3.0.0. The C api (and `IplImage`) had been deprecated for 5 years by that time, so there's no reason you should be using it. As @DanMašek says, you should just read the image directly into a `Mat`. – beaker Apr 01 '16 at 16:01
  • @beaker Good point, I seem to have missed that when i looked through the source. Seems there is [another](http://stackoverflow.com/a/30849778/3962537) [option](http://answers.opencv.org/question/23440/any-way-to-convert-iplimage-to-cvmat-in-opencv-300/) ... and that this is most likely a duplicate question. – Dan Mašek Apr 01 '16 at 18:00

2 Answers2

2

Seems the Constructor cv::Mat(IplImages*) has disappeared from core/src/matrix.cpp. By the way, I found an alternative for that.

Mat mtx = cv::cvarrToMat(img);

That should do it.

abhinav dwivedi
  • 188
  • 1
  • 7
1

These could also work as well:

// Method 1
Mat mtx = img;

// Method 2 (`true` stands for whether you want a deepcopy)
Mat mtx(img, true);
Wendee Hsu
  • 98
  • 6