Crop Image Patch & Paste [closed]

asked 2016-10-22 11:54:16 -0600

Jishan gravatar image

So I am trying to extract a 16x16 patch from the centre of an image and paste it into some random location in the same image and display it. Here is the code I wrote for the same:

void patchPase(Mat& img, Mat& result) {

      Point pt1;
      pt1.x = rng.uniform( x_1, x_2 );
      pt1.y = rng.uniform( y_1, y_2 );
      Mat smallImage;
      smallImage = img(Rect((img.size().width/2),(img.size().height/2), 16, 16));
      Rect roi(Point(pt1.x, pt1.y), smallImage.size());
      smallImage.copyTo(img(roi));
      result = img;
}

However this fails to run with the following error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-92Ozt2/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp, line 269 terminate called after throwing an instance of 'cv::Exception' what(): /build/opencv-92Ozt2/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow

Where have I mistaken? Thanks.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2017-08-24 03:34:53.434566

Comments

2

in opencv 3.1 there is no bug (if img is not empty, check it). After calling function patchPase result and img are identical.

LBerger gravatar imageLBerger ( 2016-10-22 15:40:16 -0600 )edit

OpenCV 2.4.9.1 is ancient and full of bugs compared to recent editions. Please avoid using it!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-10-24 08:09:49 -0600 )edit