Ask Your Question

Extract patch from image

asked 2012-10-10 11:32:29 -0500

dzvrt gravatar image


How can I extract a patch from an image, by knowing the image file to which it belongs? The patch is given with its center (the patch has the shape of an ellipse), its orientation, its scale, and its affine transformation with respect to a unit circle, and a set of descriptors.Any hints or code sample? Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-10-10 13:10:50 -0500

imran gravatar image

First you will need to determine the bounding rectangle for the ellipse. Here is the sample code for it,

Rect patch =  your_ellipse.boundingRect();

Secondly to extract the patch you will need to extract the ROI of the patch. Here is the sample code for it,

Mat ROI(image,patch);

Hope it helps.

edit flag offensive delete link more


Thanks, it was a good starting point. Actually I was thinking about something like this , where the region of interest can be a circle, ellipse or any shape. The only thing in this solution not suitable for my case, is that the extracted circle patch has a color, and I would like to be just an image part. Do you have any suggestions for this?

dzvrt gravatar imagedzvrt ( 2012-10-10 19:25:43 -0500 )edit

I don't think its possible. The extracted region will always need to be a square or a rectangle. What you can do is to extract the rectangular region and then apply a binary mask with your shape to the rectangular region. The output will be such that all the pixels within your shape has colour and everything outside the shape will be black.

imran gravatar imageimran ( 2012-10-11 05:51:24 -0500 )edit

If I apply your suggestion, then will the color of the pixels within the shape, correspond to the color of the original image pixels? It is very important to preserve this information.

dzvrt gravatar imagedzvrt ( 2012-10-11 06:00:59 -0500 )edit

Yes it will. What you do is, given a white pixel in your mask, copy the exact pixel (actually the 3 pixels for RGB) and place it in the same position as the white pixel. In this way, the final output will be the same mask (your shape), but just with colour.

imran gravatar imageimran ( 2012-10-11 11:47:32 -0500 )edit

Thanks! :)

dzvrt gravatar imagedzvrt ( 2012-10-11 14:58:59 -0500 )edit

Your welcome. If the suggestions answers your question then click on the tick on the left, so it would help others as well.

imran gravatar imageimran ( 2012-10-14 01:55:00 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-10-10 11:32:29 -0500

Seen: 2,848 times

Last updated: Oct 10 '12