Conversion from Iplimage* to Cv::Mat

Conversion from IplImage* to cv::MAT

here is a good solution

Mat(const IplImage* img, bool copyData=false);

Converting cv::Mat to IplImage*

cv::Mat is the new type introduce in OpenCV2.X while the IplImage* is the "legacy" image structure.

Although, cv::Mat does support the usage of IplImage in the constructor parameters, the default library does not provide function for the other way. You will need to extract the image header information manually. (Do remember that you need to allocate the IplImage structure, which is lack in your example).

Error when converting IplImage to Mat

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.

Convert IplImage to CvMat

Use your IplImage for counting, and then convert it to a Mat (in C++) with this constructor:

Mat(const IplImage* img, bool copyData=false);

So you would just do:

Mat myMat(img);

That would make your matrix. You would use it in the tracking section of your program!

NOTE: Data is not copied. You can set the copyData parameter to true, though, if you want data to be copied.

From IPLImage to Mat

// Mat mat; // a bgr, CV_8UC3 mat

for (int i=0; i<mat.rows; i++)
{
// get a new pointer per row. this replaces fumbling with widthstep, etc.
// also a pointer to a Vec3b pixel, so no need for channel offset, either
Vec3b *pix = mat.ptr<Vec3b>(i);
for (int j=0; j<mat.cols; j++)
{
Vec3b & p = pix[j];
if ( p[0] > 200 ) p[0] = 255;
if ( p[1] > 200 ) p[1] = 255;
if ( p[2] > 200 ) p[2] = 255;
}
}

conversion from ‘IplImage* {aka _IplImage*}’ to non-scalar type ‘cv::Mat’ requested error when using cvQueryFrame

Well, I don't really advise to you to use the old/obsolete IplImage format, but the conversion into cv::Mat is possible as below:

cv::Ptr<IplImage> iplimg(cvQueryFrame(capture)); // cv::Ptr<T> is safe ref-counting pointer class
if(!iplimg)
{
break;
}

// cv::Mat replaces the CvMat and IplImage, but it's easy to convert
// between the old and the new data structures (by default, only the header
// is converted, while the data is shared)
cv::Mat img = cv::cvarrToMat(iplimg);

Why don't you use cv::VideoCapture instead?

cv::VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;

while(true)
{
cv::Mat frame;
cap >> frame; // get a new frame from camera

cv::imshow("frame", frame);
cv::waitKey(1);
}


Related Topics



Leave a reply



Submit