Extract selected region from image with OpenCV

1.7k Views Asked by At

I have a region of an image selected, like this:

http://slideplayer.com/4593320/15/images/9/Intelligent+scissors+http%3A%2F%2Frivit.cs.byu.edu%2FEric%2FEric.html.jpg

and now, using OpenCV I would like to extract the region selected.

How could I do it? I have already researched but nothing useful got.

Thanks in advance.

1

There are 1 best solutions below

0
On

First of all you have to import your pixel locations into the program and you have to create contour object using the points. I guess you know how to do this.

You can find from following link how to create contour object:

Creating your own contour in opencv using python

You can fill black using following code out of your selected image

black = np.zeros(img.shape).astype(img.dtype)
color = [1, 1, 1]
cv2.fillPoly(black, contours, color)
new_img = img * black

I guess you know (or find) how to crop after black out remaining image using contour pixels.