如何解决寻找轮廓中最宽的水平线
通过 findContours() 提取对象的轮廓后,我想通过每个轮廓找到最宽的水平线。我已经尝试使用极值点,但由于形状延迟,它不会导致水平线...... 到目前为止的代码:
###contour extraction
cnts = cv2.findContours(img_thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
cnts = cnts[0]
(cnts,_) = contours.sort_contours(cnts)
for cnt in cnts:
if cv2. contourArea(cnt) > 500:
contour_lenght = cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,0.009 * contour_lenght,True)
obj_cor = len(approx)
if obj_cor > 4:
cv2.drawContours(blank_image,cnt,-1,(0,0),1)
###boundingbox
x,y,w,h = cv2.boundingRect(approx)
bbox = (x,y),(x,y + h),(x + w,y)
tl = (x,y)
bl = (x,y + h)
br = (x + w,y + h)
tr = (x + w,y)
bbox = cv2.rectangle(blank_image_3,(tl),(br),1)
cnt_poly = cv2.polylines(blank_image_3,True,0))
###extreme points
leftmost = tuple(approx[approx[:,:,0].argmin()][0])
rightmost = tuple(approx[approx[:,0].argmax()][0])
cv2.circle(blank_image_3,leftmost,2,255),3)
cv2.circle(blank_image_3,rightmost,3)
leftmost = (leftmost[0],leftmost[1])
leftmost_direction = (leftmost[0] + w,leftmost[1])
rightmost_direction = (rightmost[0] - w,rightmost[1])
image shows the outlines outlines with bbox and extreme points
非常感谢任何帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。