Find points inside a contour

asked 2018-07-13 05:14:58 -0500

Hello, I am trying to get the coordinates of all the points which compose an instance for all the different instances in a binary image. To do so, I first get all the contours (which define an instance) with cv2.findContours(). Then, I want to get all the points inside a contour and I have only found this way (see below) of doing so. I am sure there is a more efficient way to do so (without my dummy_mask for example) but I havn't found it).

import numpy as np
import cv2
def shape_from_contour(img, contour):
    dummy_mask = np.zeros((img.shape[0],img.shape[1],3))
    dummy_mask = cv2.drawContours(mask,[contour],0,(1,0,0),thickness=cv2.FILLED)
    x,y = np.where(dummy_mask[:,:,0]==1)
    inside_points = np.stack((x,y),axis=-1)
    return inside_points
edit retag flag offensive close merge delete