Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop Mat image in OpenCV 2.4.3 (iOS)

Tags:

ios

opencv

I am trying to crop images coming out of the video stream with OpenCV for iOS. I am not trying to do anything fancy, simply crop the image, and display it. I have tried this, and this, neither seem to work for me. As a delegate method, I get the current cv::Mat image passed in, so all I need is the code that will create the crop effect.

I could do something as complicated as this if need be, but I only need a rectangular crop, so I think there is an easier way. I just dont understand why setting the ROI is not working for me!

cv::Rect myROI(10, 10, 100, 100);
cv::Mat croppedImage = src(myROI);

src.copyTo(croppedImage);


[displayView setImage:[UIImage imageWithCVMat:src]];

^^^not working, just displaying original image

like image 385
Jameo Avatar asked Jan 21 '13 20:01

Jameo


1 Answers

The problem in your code is that by copying the src image you copy the whole image to the cropped image. There are two possibilities:

1 - Without copying data. Its the same as you did. But without the second step. In that case croppedImage is not a real copy it pointed to the data allocated in src.:

cv::Mat croppedImage = src(myRoi);

2 - With copying the data.

cv::Mat croppedImage;
cv::Mat(src, myRoi).copyTo(croppedImage)

(one line method - cv::Mat croppedImage = cv::Mat(src, myRoi).clone();)

like image 62
Tobias Senst Avatar answered Oct 13 '22 10:10

Tobias Senst