Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using drawContours OpenCV function in python

Tags:

python

opencv

I have installed OpenCV 2.2 and when I try to use drawContours I get the following error:

cv.drawContours(frame, contours, 0, cv.RGB(255, 0, 0))
TypeError: <unknown> is not a numpy array

The code related to this error is the following:

storage = cv.CreateMemStorage(0)
contours = cv.FindContours (color_mask, storage, method = cv.CV_CHAIN_APPROX_SIMPLE)
cv.drawContours(frame, contours, 0, cv.RGB(255, 0, 0))

The python documentation does not correspond with the correct order of parameters (I know the correct order thank to IDLE) and the C++ documentation for this function does not help me very much

Here is the full code (relevant code):

    cv.NamedWindow("MyWindow", 1)
    capture = cv.CaptureFromCAM(0)

    while 1:
        frame = cv.QueryFrame(capture)

        color_mask = cv.CreateImage(cv.GetSize(frame), 8, 1)

        cv.InRangeS(frame, cv.Scalar(*min_color), cv.Scalar(*max_color), color_mask)

        cv.CvtColor(frame, frame, cv.CV_BGR2HSV)

        storage = cv.CreateMemStorage(0)
        contours = cv.FindContours (color_mask, storage, method = cv.CV_CHAIN_APPROX_SIMPLE)
        cv.drawContours(image = frame, contours = contours, contourIdx = 0, color = cv.RGB(255, 0, 0))

        cv.ShowImage("MyWindow", frame)

Thanks in advance

like image 873
Manuel Avatar asked Apr 06 '11 11:04

Manuel


People also ask

How do you show contours in Python?

findContours() function. First one is source image, second is contour retrieval mode, third is contour approximation method and it outputs the image, contours, and hierarchy. 'contours' is a Python list of all the contours in the image.

Where is contour area in OpenCV Python?

Contour area is given by the function cv. contourArea() or from moments, M['m00'].


1 Answers

You should be aware that drawContours and DrawContours are two different functions. They actually do the same thing, but they accept different parameters. I believe the first one only accepts numpy arrays instead of CvMat or other arrays from openCV.

like image 166
Zebreu Avatar answered Oct 16 '22 08:10

Zebreu