Manuel
Manuel

Reputation: 2296

Using drawContours OpenCV function in python

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

Upvotes: 10

Views: 17586

Answers (4)

P2bM
P2bM

Reputation: 1048

You should check the function parameters in the python reference of DrawContours, and try not to rely on the order of the parameters when calling a function that takes multiple arguments, you should use labels.

In other words :

cv.DrawContours(img=frame, contour=contours, ...)

If you check the documentation of DrawContours:

DrawContours(img, contour, external_color, hole_color, max_level, thickness=1, lineType=8, offset=(0, 0))

You will notice that the function accepts 8 arguments:

  • 5 needed (img, contour, external_color, hole_color, max_level)
  • 3 optional (thickness, lineType, offset)

and there are no arguments called contourIdx or color

eg:

cv.DrawContours(img=frame, contour=contours, external_color=cv.RGB(255, 0, 0), hole_color=cv.RGB(0, 255, 0), max_level=1 ) 

Upvotes: 2

Anurag
Anurag

Reputation: 41

Use the function cv2array and array2cv given on OpenCV Cheatsheet to convert the image to the specific format.

Something like this:

imgray = array2cv(cv2.cvtColor(cv2array(image), cv.CV_RGB2GRAY))
storage = cv.CreateMemStorage(0)
contours = cv.FindContours(imgray, storage,cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE,(0,0))

Upvotes: 1

Zebreu
Zebreu

Reputation: 76

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.

Upvotes: 6

Manuel
Manuel

Reputation: 2296

I have found some errors in Python wrapper of OpenCV 2.2. Examples like "camshift.py" run with OpenCV 2.1 but not with OpenCV 2.2. I suppose my problems are derivated from this bug (now I will use 2.1 version)

I have reported this bug as well as the documentation error

@P2bM thanks for your help

Upvotes: 0

Related Questions