10

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

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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 ) 

4 Comments

The right function parameters does not correspond with the official reference (I know this sounds stupid). If I do as you said the following error is raised: TypeError: Required argument 'contourIdx' (pos 3) not found
After a small break, I think the error could be in the contours generation
i think you are looking at the wrong documentation follow the link on the answer, its the documentation for the python API
I have read this documentation, but after download OpenCV 2.2.0 and compile & install it, if I open Idle, import OpenCV module and write cv.DrawContours, the parameters showed by Idle are not the same documented, I think the doc was generated before the final version. If I try to label parameters like 'img = wherever' the python interpreter raise and error saying there are any parameter with label 'img'
1

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))

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.