gpt4 book ai didi

math - 我们如何识别一组近似矩形的像素?

转载 作者:行者123 更新时间:2023-12-04 12:04:28 26 4
gpt4 key购买 nike

将左侧像素组视为矩形的数学逻辑是什么?
reference_image

最佳答案

这是评论中想法的实现。做法是:

  • 找到旋转的矩形边界框
  • 计算这个旋转边界框的面积和轮廓的面积
  • 比较两者。如果轮廓的面积至少是旋转边界框面积的 80%(或任意阈值),那么我们将其视为矩形

  • 这是图像处理管道的可视化
    输入图片 ->阈值 ->检测到旋转的矩形边界框 ->面具



    Contour area: 17719.0
    Mask area: 20603.0
    Compared area percentage: 86.002%
    It is a rectangle!
    对于其他图像
    输入图片 ->阈值 ->检测到旋转的矩形边界框 ->面具



    Contour area: 13395.5
    Mask area: 19274.5
    Compared area percentage: 69.499%
    It is not a rectangle!
    您没有指定语言,所以这里有一个使用 Python OpenCV 的简单实现
    import cv2
    import numpy as np

    # Load image, convert to grayscale, Otsu's threshold for binary image
    image = cv2.imread('1.png')
    mask = np.zeros(image.shape, dtype=np.uint8)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Find contours, find rotated rectangle, find contour area
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    contour_area = cv2.contourArea(cnts[0])
    print('Contour area: {}'.format(contour_area))
    rect = cv2.minAreaRect(cnts[0])
    box = np.int0(cv2.boxPoints(rect))
    cv2.drawContours(image, [box], 0, (36,255,12), 3)

    # Find area of rotated bounding box and draw onto mask image
    mask_area = cv2.contourArea(box)
    cv2.drawContours(mask, [box], 0, (255,255,255), -1)
    print('Mask area: {}'.format(mask_area))

    # Compare areas and calculate percentage
    rectangular_threshold = 80
    percentage = (contour_area / mask_area) * 100
    print('Compared area percentage: {:.3f}%'.format(percentage))
    if percentage > rectangular_threshold:
    print('It is a rectangle!')
    else:
    print('It is not a rectangle!')

    # Display
    cv2.imshow('image', image)
    cv2.imshow('thresh', thresh)
    cv2.imshow('mask', mask)
    cv2.waitKey()

    关于math - 我们如何识别一组近似矩形的像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69249489/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com