Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to get first row of np.vstack? midpoint of contour - Python

Hello,

Im new to OpenCV and Python, and Im trying to meassure the distance of the midpoints of two contours. This is my code so far:

image = cv2.imread('TwoMarkers.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127 , 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
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'])
    contourMidpoint= np.vstack([(cX, cY)])

    D = dist.euclidean(contourMidpoint[0], contourMidpoint[1])
    print(D)

How can I get the distance D between the two found values that are stacked? The apporach I tried dident work, and I have no clue how to sepererate the values that are stacked. Any help is much appreciated.