czxt3000 2023-12-26 15:24 采纳率: 59.8%
浏览 32
已结题

检测轮廓应该为矩形,缺角怎么补齐?

如图

img

如果用逼近的方法,不能补齐,反而会切掉。

img

能用什么方法补齐?

  • 写回答

12条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-12-26 15:25
    关注

    引自微信小程序:皆我百晓生

    import cv2
    import numpy as np
    
    # 读取图像
    image = cv2.imread('71.png')
    
    # 灰度图
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
    
    # 查找轮廓
    contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    # 最大轮廓
    max_area = 0
    max_contour = np.array([])
    for contour in contours:
        # 逼近轮廓以减少点的数量
        epsilon = 0.1 * cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, epsilon, True)
        # 面积
        area = cv2.contourArea(approx)
    
        if area > max_area:
            max_area = area
            max_contour = approx
    
    # 补齐缺角
    hull = cv2.convexHull(max_contour)
    
    # 画轮廓
    cv2.drawContours(image, [hull], 0, (0, 0, 255), 3)
    
    # 显示结果
    cv2.imshow('image', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(11条)

报告相同问题?

问题事件

  • 系统已结题 1月9日
  • 已采纳回答 1月1日
  • 创建了问题 12月26日