Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Crop Image Patch & Paste

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.