How can I clip an Image [closed]

asked 2015-03-11 09:07:05 -0600

BetaVersion gravatar image

I have images with a region of interest (=rectangle) and I want to clip the image to this region. Is there a simple way of doing this ? The calls I tried do the row-clipping just fine, but ignore the colums at least if I show the image on the screen ?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-13 12:41:19.344601

Comments

Mat image2=image(Rect(x0,y0,w,h))

kbarni gravatar imagekbarni ( 2015-03-11 09:47:09 -0600 )edit

Actually this is what I do, but as I said, if I do imshow("test", image2) then the rows above and below are clipped, but not the columns. So p.ex. the part between x=0 and x0 is still shown. My question is, if I need to build this myself or if there is a standard to do this...

BetaVersion gravatar imageBetaVersion ( 2015-03-11 10:09:12 -0600 )edit

This should really give you an image of size WxH starting from x0,y0.

Note that this operation only defines a ROI on the image (a reference to a sub-image), so if you change something, image will change too. To crop the image, you have to make a copy of image2.

To erase the contents of an image outside a ROI, create a mask (black background and white rectangle) and use image.copyTo(image2,mask)

kbarni gravatar imagekbarni ( 2015-03-11 10:22:59 -0600 )edit

Ok that was the trick by adding: image2.copyto(image2);

made it work, thx...

I only did a resize, and that seems not to do the same...

BetaVersion gravatar imageBetaVersion ( 2015-03-11 11:04:32 -0600 )edit