WYW991 2022-07-19 16:05 采纳率: 0%
浏览 63
已结题

open-cv轮廓检测

open-cv的轮廓检测矩形拟合 下图中如何实现最大矩形的检测呢

  1. img

  • 写回答

5条回答 默认 最新

  • 脚踏南山 2022-07-19 17:20
    关注
    获得5.00元问题酬金
    
    import cv2 as cv
    import numpy as np
    
    img_path = "/home/LTL/Desktop/20220719171125.png"
    
    img_np = cv.imread(img_path)
    ret, thresh1 = cv.threshold(img_np, 80, 255, cv.THRESH_BINARY)
    np_zeros = np.ones(thresh1.shape[0:2]) * 255
    
    index_1 = np.where(thresh1[:, :, 0] == 0)
    index_2 = np.where(thresh1[:, :, 1] == 0)
    index_3 = np.where(thresh1[:, :, 1] == 0)
    np_zeros[index_1[0], index_1[1]] = 0
    np_zeros[index_2[0], index_2[1]] = 0
    np_zeros[index_3[0], index_3[1]] = 0
    
    contour_list, _ = cv.findContours(np_zeros.astype(np.uint8), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    
    contour_area_list = list(map(lambda x: cv.contourArea(x), contour_list))
    contour_list = list(filter(lambda x: 100 < cv.contourArea(x) < img_np.shape[0] * img_np.shape[1] // 2, contour_list))
    result_xywh = []
    result_poly_point = []
    contour_list.sort(key=lambda x: cv.contourArea(x), reverse=True)
    
    for contour_i, contour in enumerate(contour_list[0:1]):
        contour_area = cv.contourArea(contour)
        x, y, w, h = cv.boundingRect(contour)
    
        poly_point_list = contour[:, 0, :].ravel().tolist()
        poly_point = np.int0(poly_point_list).reshape((-1, 1, 2))
        result_poly_point.append(poly_point)
        cv.rectangle(img_np, (x, y), (x + w, y + h), (255, 255, 0), 2)
    cv.imshow('', img_np)
    cv.waitKey()
    cv.destroyAllWindows()
    
    

    img

    img

    评论

报告相同问题?

问题事件

  • 系统已结题 7月27日
  • 创建了问题 7月19日