Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

this looks like a more general code-flow / logic problem.

i'd try to:

1.: collect midpoints 2.: check their distances.

(you simply can't do all of it in the same loop)

# step 1, collect centers:
centers=[]
for c in contours:
    # if the contour is not sufficient large, or to small, ignore it
    if cv2.contourArea(c) > 2000:
        continue
    elif cv2.contourArea(c) < 100:
        continue
    M = cv2.moments(c)
    cX = int(M['m10'] /M['m00'])
    cY = int(M['m01'] /M['m00'])
    centers.append([cx,cy])

# step 2, compare:
if len(centers) >=2:
    D = dist.euclidean(centers[0], centers[1])
    print(D)

this looks like a more general code-flow / logic problem.

i'd try to:

1.: collect midpoints 2.: check their distances.

(you simply can't do all of it in the same loop)

# step 1, collect centers:
centers=[]
for c in contours:
    # if the contour is not sufficient large, or to small, ignore it
    if cv2.contourArea(c) > 2000:
        continue
    elif cv2.contourArea(c) < 100:
        continue
    M = cv2.moments(c)
    cX = int(M['m10'] /M['m00'])
    cY = int(M['m01'] /M['m00'])
    centers.append([cx,cy])

# step 2, compare:
if len(centers) >=2:
    dx = centers[0][0] - centers[1][0]
    dy = centers[0][1] - centers[1][1]
    D = dist.euclidean(centers[0], centers[1])
math.sqrt(dx*dx+dy*dy)
    print(D)