Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openCV python, get the region from closed curve

Tags:

python

opencv

I am using openCV with python bindings, and I can use the mouse as a paint brush. Suppose I have the following image, and I want to get all the colors inside the blue curve.enter image description here

I'd thought I can save all the coordinates into a list, and if a point that is already there is appended, get the closed region.

BUT how can I do that, or is at least possible? Thanks!


EDIT:

I've used the method suggested by @Arijit Mukherjee and I've obtained the following: enter image description here

How can I now get all the colors inside that contour? An irrational solution would be to parse every pixel of the mask image, and make a set. How can I do this in another way?

like image 495
dimmg Avatar asked Dec 01 '25 18:12

dimmg


1 Answers

Ok I assume that the area is selected from mouse click Follow the steps below:

  1. Read The original Image
  2. Create the mask
  3. Bitwise and both images

enter image description here

Example Code:

import cv2
import numpy as np

# original image
image = cv2.imread('image.png')

# mask (of course replace corners with yours)
mask = np.zeros(image.shape, dtype=np.uint8)
roi_corners = np.array(points, dtype=np.int32) #pointsOf the polygon Like [[(10,10), (300,300), (10,300)]]
white = (255, 255, 255)
cv2.fillPoly(mask, roi_corners, white)

# apply the mask
masked_image = cv2.bitwise_and(image, mask)

# display your handywork
cv2.imshow('masked image', masked_image)
cv2.waitKey()
cv2.destroyAllWindows()

Edit

to find the color of the ROI you can use any of these two methods according to your needs->

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)
mean_val = cv2.mean(im,mask = mask)
like image 138
Arijit Avatar answered Dec 04 '25 08:12

Arijit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!