简体   繁体   中英

opencv: Convert Mat to IplImage Issue

In my program I have to mix the c++ and c api a bit.

I capture an image with the c api and get one frame:

CvCapture* capture = 0; 
capture = cvCaptureFromCAM(0);

// some code

IplImage* image = cvQueryFrame(capture);

Then it is converted to Mat to be compatible with the new c++ api and I get a ROI:

Mat captureFrame = cvarrToMat(image);

// some code

Mat roi = captureFrame(roiRect);

At the end I have to convert the Mat back to IplImage* to work with the c api:

IplImage imgCaptureFrame = roi;

when I use this as reference &roi I get a

OpenCV Error: Assertion failed (svec[j].size == dst.size && svec[j].depth() == d
st.depth() && svec[j].channels() == 1 && i < dst.channels()) in unknown function
, file C:\slave\builds\WinInstallerMegaPack\src\opencv\modules\core\src\convert.
cpp, line 1306

in code using c api.

When I just use

IplImage imgCaptureFrame = captureFrame;

instead of

IplImage imgCaptureFrame = roi;

there isn't any error but then I don't have my roi.

What can I do to convert my roi to use it in c api?

To convert an IplImage* to cv::Mat and make an independent copy, do:

cv::Mat captureFrame = cv::Mat(image, true);

To create a ROI for captureFrame you could do something like:

cv::Rect roi;
roi.x = 165;
roi.y = 50;
roi.width = 440;
roi.height = 80;

cv::Mat cropped = new cv::Mat(captureFrame, roi);

and finally, to do the conversion the other way:

IplImage imgCaptureFrame = cropped;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM