theDude
theDude

Reputation: 31

opencv connectedComponentsWithStats

first post here!

I just installed python-opencv. According to python my version is:

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

My Ubuntu version is 14.04.

I then started a python-opencv tutorial which suggested this code:

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)

When trying to execute the unaltered code I got this:

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

A quick search suggests that I need python-opencv 3 instead of my current version 2.4.8. I don't know how to update the opencv version to opencv 3 so that python will automatically access it. Any help? Step by step instructions would be very appreciated!

Upvotes: 3

Views: 6388

Answers (1)

gkhanacer
gkhanacer

Reputation: 739

'connectedComponentsWithStats' method is only usable on opencv3.x.

Upvotes: 1

Related Questions