2015-01-23 21 views
7

मैं here से एक उदाहरण चलाने का प्रयास करता हूं।findContours और drawContours त्रुटियों को opencv 3 बीटा/पायथन

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) 

त्रुटि

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) 

है अगर मैं "पदानुक्रम" हटाएँ त्रुटि drawContours में उठता है:

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

अगर मैं आकृति का उपयोग [0] drawContours

में
cv2.error: E:\opencv\opencv\sources\modules\imgproc\src\drawing.cpp:2171: error: (-215) npoints > 0 in function cv::drawContours 

यहां क्या समस्याएं हो सकती हैं?

उत्तर

11

opencv 3 यहाँ एक से थोड़ा changed syntax है, वापसी मान अलग:

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

OpenCV 3.0 में दस्तावेज़ प्राप्त करने (ऊपर लिंक वर्तमान में विफल रहता है) वर्तमान में कठिन है, तो मैं बस कि क्या 'findContours()' 3.0 में अभी भी छवि को संशोधित करने की कोशिश की। यह करता है, और छवि पारित हो जाती है और लौटाई गई छवि 'is' के अनुसार समान होती है। –

+2

[यहां है] (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 दस्तावेज़ों के लिए एक कार्यशील लिंक जो findContours के पायथन संस्करण के लिए तीन रिटर्न मान दिखाता है। –

10

berak के जवाब जारी रखते हुए, सिर्फ [-2:]findContours() के लिए कॉल को जोड़ने बनाता है उन दोनों OpenCV 2.4 और 3.0 के लिए काम करते हैं:

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

मुझे पहले एक ही समस्या का सामना करना पड़ता है और मैं इसे ठीक करने के लिए इस कोड का उपयोग करता हूं। मैं वैसे भी 3.1 का उपयोग कर रहा हूँ। अजगर के लिए

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