寻找轮廓中最宽的水平线

时间:2021-01-13 20:26:40

标签: python opencv contour

通过 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, 0), 1)
###boundingbox
        x, y, w, h = cv2.boundingRect(approx)
        bbox = (x, y), (x, y + h), (x + w, 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), (0, 0, 0), 1)
        cnt_poly = cv2.polylines(blank_image_3, cnt, True, (0, 0, 0))

###extreme points
        leftmost = tuple(approx[approx[:, :, 0].argmin()][0])
        rightmost = tuple(approx[approx[:, :, 0].argmax()][0])

        cv2.circle(blank_image_3, leftmost, 2, (0, 0, 255), 3)
        cv2.circle(blank_image_3, rightmost, 2, (0, 0, 255), 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

非常感谢任何帮助!

0 个答案:

没有答案