2010-08-10 71 views
2

我使用pyopencv查找輪廓,但無法繪製找到的輪廓。我得到的錯誤:pyopencv drawContours

23 color = Scalar(255) 
24 print type(color) 

---> 25 drawContours(img, list(contours), -1, color) 26 27 imshow('Xe may - 0', img)

ArgumentError: Python argument types in pyopencv.pyopencvext.drawContours(Mat, list, int, Scalar) did not match C++ signature: drawContours(cv::Mat {lvalue} image, std::vector, std::allocator > >, std::allocator, std::allocator > > > > contours, int contourIdx, cv::Scalar_ color, int thickness=1, int lineType=8, std::vector, std::allocator > > hierarchy=vector_Vec4i(len=0, []), int maxLevel=2147483647, cv::Point_ offset=Point2i(x=0, y=0)) WARNING: Failure executing file:

這裏是我的代碼

# load image 
img = imread('37S2231.jpg') 
# gray scale 
out = img.clone() 
cvtColor(img, out, CV_RGB2GRAY) 
# equalizes the histogram of a grayscale image 
# increases the contrast of the image 
out2 = out.clone() 
equalizeHist(out, out2) 
# canny to extract edges 
out3 = out2.clone() 
Canny(out2, out3, 150, 200) 
# threshold 
out4 = out3.clone() 
threshold(out3, out4, 254, 255, THRESH_BINARY) 
# contours 
contours = findContours(out4, 1, 1) 
print type(contours) 
color = Scalar(255) 
print type(color) 
drawContours(img, list(contours), -1, color) 

我已經檢查了drawContours功能在http://packages.python.org/pyopencv/2.1.0.wr1.0.2/但它類似於我的代碼。我做錯什麼了嗎?

感謝

回答

1

首先感謝這個例子中,它是唯一一個我發現,說明pyopencv.findContours的用法。對於你的問題:使用輪廓[0]而不是列表(輪廓)!所以,最後一行改爲

drawContours(img,contour [0],-1,color)