[英]How to detect and draw contours using OpenCV in Python?
我編寫了以下代碼來檢測和繪制輪廓:
img = cv2.imread('test2.tif');
if not img is None:
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY);
ret,thresh = cv2.threshold(imgray,127,255,0);
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE);
#draw a three pixel wide outline
cv2.drawContours(img,contours,-1,(0,255,0),3);
這是我收到的錯誤:
追溯(最近一次通話):文件“ C:/ Users / RKsingh / Desktop / Image processing / intro-to-contours.py”,第10行,輪廓,層次結構= cv2.findContours(thresh,cv2.RETR_TREE,cv2 .CHAIN_APPROX_SIMPLE); ValueError:太多值無法解包
怎么了? 我正在使用Python 2.7和OpenCV 3.1.0
更改以下行。 您正在使用OpenCV 3.1.0,但已使用OpenCV 2.7.x進行了編碼。
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
此外,此鏈接將為您提供幫助。
為了強調Selchuk的觀點,涉及OpenCV 3.x的語法有些變化。 當涉及cv2.findContours
時,它具有不同的返回值。 它返回以下image, contours, hierarchy
。
但是,OpenCV的早期版本僅返回contours, hierarchy
。 他們不返回圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.