简体   繁体   中英

How can I detect the rightmost line in an image

I get the following output seen in the picture. I have found the biggest object in the image but I need the right line.

例子

ret, thresh = cv2.threshold(imgGray, 100, 255, 1)
cnts = cv2.findContours(threshh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
extRight = tuple(c[c[:, :, 0].argmax()][0]) # sağ taraf
cv2.circle(img, (extRight[0], extRight[1]+yx), 6, (0, 0, 255), -1)

Moving my comment to an answer: the reason the thickest contour is always selected is because of the line c = max(cnts, key=cv2.contourArea) . Instead, try computing the centroid of each component and select the rightmost one.

The centroid of a contour can be computed with the "moments()" function as described here :

 M = cv.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00'])

Do this computation on each contour, and select the one with the largest cx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM