0

How to draw filled contour from numpy array of points using cv2.drawContours

For some reason this code draw single points and not filled contour:

ctr = np.array(pts).reshape((-1, 1, 2)).astype(np.int32)
mask = cv2.drawContours(mask, ctr, contourIdx=-1, color=1, thickness=-1)

Using skimage I can do it like:

from skimage.draw import polygon

mask = mask.astype(np.uint8)

rr, cc = polygon(pts[:, 1], pts[:, 0], mask.shape)
mask[rr, cc] = 1

mask = mask.astype(np.float32)
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • 2
    Check [this question](https://stackoverflow.com/questions/36311398/contour-shows-dots-rather-than-a-curve-when-retrieving-it-from-the-list-but-sho/36315105#36315105) out – Dan Mašek Dec 10 '18 at 13:30
  • you can also use the fillPoly of OpenCV if you are drawing close contours – api55 Dec 10 '18 at 13:33
  • @DanMašek not sure what format `contours` after `cv2.findContours` have, I have numpy array of `np.float32` with shape `[n_pts,2]` – mrgloom Dec 10 '18 at 13:35
  • It's been a while since I wrote that... but try drawing `[ctr]` instead of `ctr`? When it's drawing just dots, it's missing one level of hierarchy in the nested arrays, and treats each point as a separate contour. – Dan Mašek Dec 10 '18 at 13:37

0 Answers0