2015-07-01 73 views
3

第一篇文章在这里!opencv connectedComponentsWithStats

我刚刚安装了python-opencv。根据蟒蛇我的版本是:

>>> import cv2 
>>> cv2.__version__ '2.4.8' 

我的Ubuntu版本是14.04。

我便开始一个python-opencv的教程,提示验证码:

img = cv2.imread('OpenCV_Chessboard.png') 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

# find Harris corners 
gray  = np.float32(gray) 
dst   = cv2.cornerHarris(gray, 2, 3, 0.04) 
dst   = cv2.dilate(dst,None) 
ret, dst = cv2.threshold(dst,0.01*dst.max(),255,0) 
dst   = np.uint8(dst) 

# find centroids 
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst) 

# define the criteria to stop and refine the corners 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001) 
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria) 

# Now draw them 
res = np.hstack((centroids,corners)) 
res = np.int0(res) 
img[res[:,1],res[:,0]]=[0,0,255] 
img[res[:,3],res[:,2]] = [0,255,0] 

cv2.imwrite('subpixel5.png',img) 

当试图执行不变的代码,我得到这个:

File "pyopencv_test.py", line 21, in <module> 
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst) 
AttributeError: 'module' object has no attribute 'connectedComponentsWithStats' 

快速搜索表明,我需要蟒蛇-opencv 3而不是我目前的版本2.4.8。我不知道如何更新opencv版本到opencv 3,以便python会自动访问它。任何帮助?一步一步的指示将非常感激!

+2

如果你想使用opencv 3.0,你需要自己编译和安装。 http://docs.opencv.org/3.0.0/d7/d9f/tutorial_linux_install.html – yangjie

回答

1

'connectedComponentsWithStats'方法仅在opencv3.x上可用。