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
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();
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With