OpenCV 3.1 drawContours '(-215) npoints > 0'

后端 未结 5 2170
一个人的身影
一个人的身影 2021-02-08 12:51

I\'m trying to create a mask from a contour, but am getting a C++ error.

Using OS X Yosemite, Python 2.7.10, OpenCV 3.1.0.

def create_mask(img, cnt):
            


        
5条回答
  •  长发绾君心
    2021-02-08 13:22

    I think you are adding extra [] around cnt it should be

    cv2.drawContours(mask, cnt, 0, (0, 255, 0), -1)
    

    as cnt is already array of array but [cnt] is array of array of arrays which won't work


    Update to the above code

    you should convert your contour to numpy array first

    ctr = numpy.array(cnt).reshape((-1,1,2)).astype(numpy.int32)
    cv2.drawContours(mask, [ctr], 0, (0, 255, 0), -1)
    

    check documentation here

    contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.

提交回复
热议问题