6

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

Community
  • 1
  • 1
Jameo
  • 4,507
  • 8
  • 40
  • 66
  • Hi am facing the same problem. Can you please have a look at my post http://stackoverflow.com/questions/14756505/croping-an-image-in-ios-uisng-opencv-face-detect – 2vision2 Feb 07 '13 at 18:54

1 Answers1

24

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();)

user924
  • 8,146
  • 7
  • 57
  • 139
Tobias Senst
  • 2,665
  • 17
  • 38
  • 1
    I had tried your first suggestion before, and when I displayed it, it was very screwed up looking (kind of looked like tv static). The second method you mention has worked for me, thanks! – Jameo Jan 21 '13 at 21:26
  • 2
    @Jameo your display method probably assumed that the data was continuous which is not true using the first method. – Hammer Jan 21 '13 at 23:38
  • The second suggestion worked for me while the first one failed. – jarora Mar 19 '16 at 17:12
  • @jarora - what kind of error did you get? Please mentoin that in the first case croppedImage and myRoi the image data ptr points to one data object – Tobias Senst Mar 21 '16 at 09:39
  • thanks! you just saved me. in `Java` (Android) I could use ` `public Mat(Mat m, Rect roi);` - for example - `Mat cropped = new Mat(src, myRoi);` - and it worked good. In iOS I tried first method and didn't work, now I using second method, works fine – user924 Mar 15 '18 at 13:09
  • there is also method like `croppedImage = cv::Mat::Mat(src, myRoi);` but seems it also works like first (1) method, and also `cv::Mat(src, myRoi);` – user924 Mar 15 '18 at 13:13
  • also one line method (using `clone` instead of `copyTo`) - `cv::Mat croppedImage = cv::Mat(src, myRoi).clone();` – user924 Mar 15 '18 at 13:28
  • p.s. also we don't always need to copy it (it better to use first method), until we edit this or trying to convert to `UIImage` (then we have to use second method - copying) – user924 Mar 15 '18 at 13:36