2015-01-23 23 views
7

here'dan bir örnek çalıştırmayı deniyorum. Ben "hiyerarşi" silersenizfindContours ve drawContours hataları in opencv 3 beta/python

import numpy as np 
import cv2 
img = cv2.imread('final.jpg') 
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) 
cv2.drawContours(img, contours, -1, (0,255,0), 3) 

hata

Traceback (most recent call last): 
    File "E:\PC\opencv3Try\findCExample.py", line 7, in <module> 
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 
ValueError: too many values to unpack (expected 2) 

olan hata drawContours ortaya çıkmaktadır:

TypeError: contours is not a numpy array, neither a scalar 

Ben hatlarını kullanıyorsanız [0] drawContours

yılında
cv2.error: E:\opencv\opencv\sources\modules\imgproc\src\drawing.cpp:2171: error: (-215) npoints > 0 in function cv::drawContours 

Burada hangi problemler olabilir?

cevap

11

opencv 3 Burada biraz changed syntax sahiptir, dönüş değerleri farklılık gösterir:

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) → image, contours, hierarchy 
+2

OpenCV 3.0 belgeleri (Yukarıdaki bağlantı şu anda başarısız) bulmak şu anda zor, bu yüzden sadece 'findContours()' 3.0 hala görüntüyü değiştirip değiştirmediğini çalıştı. Görüntüyü döndü ve görüntü döndü ve döndürülen görüntü '' ye göre aynıdır. –

+2

[Burada] (http://docs.opencv.org/3.0-last-rst/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours) OpenCV 3.0.0-dev belgelerine çalışan bir bağlantı findContours'un Python sürümü için üç dönüş değerini gösterir. –

10

Berak en yanıta ardından, sadece [-2:]findContours() çağrıları ekleme yapar ikisini de OpenCV 2.4 ve 3.0 için çalışmak:

contours, hierarchy = cv2.findContours(...)[-2:] 
0

Daha önce aynı sorunla karşılaşıyorum ve düzeltmek için bu kodu kullanıyorum. Yine de 3.1 kullanıyorum. Python için

(_,contours,_) = cv2.findContours(
    thresh.copy(), 
    cv2.RETR_LIST, 
    cv2.CHAIN_APPROX_SIMPLE 
)