Creating your own contour in opencv using python

Shan picture Shan · Jan 4, 2013 · Viewed 18.7k times · Source

I have a set of boundary points of an object.

I want to draw it using opencv as contour.

I have no idea that how to convert my points to contour representation.

To the same contour representation which is obtained by following call

  contours,_ = cv2.findContours(image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

Any ideas?

Thanks

Answer

sietschie picture sietschie · Jan 4, 2013

By looking at the format of the contours I would think something like this should be sufficient:

contours = [numpy.array([[1,1],[10,50],[50,50]], dtype=numpy.int32) , numpy.array([[99,99],[99,60],[60,99]], dtype=numpy.int32)]

This small program gives an running example:

import numpy
import cv2

contours = [numpy.array([[1,1],[10,50],[50,50]], dtype=numpy.int32) , numpy.array([[99,99],[99,60],[60,99]], dtype=numpy.int32)]

drawing = numpy.zeros([100, 100],numpy.uint8)
for cnt in contours:
    cv2.drawContours(drawing,[cnt],0,(255,255,255),2)

cv2.imshow('output',drawing)
cv2.waitKey(0)