简体   繁体   中英

Save contour to image

I used cv2.findContours to find objects and after processing I want to save certain contour(s). I must first create empty image and then use cv2.drawContours command. However docstring for this command is following:

drawContours(image, contours, contourIdx, color[, thickness[,lineType[, hierarchy[, maxLevel[, offset]]]]]) -> None

and contourIdx is required, while I have no idea what is it supposed to be.

Does anyone know how to get this parameter, or even demonstrate other way of dumping contours to file?


Update :

for dumping single contour contourIdx parameter should be set to -1

This should work.

drawing = np.zeros(img.shape)
for i in xrange(len(contours)):
    if (cv2.contourArea(contours[i]) > 15000): # just a condition
        cv2.drawContours(drawing, contours, i, (255, 255, 255), 1, 8, hierarchy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM