【问题标题】:Find width and height of RotatedRect [closed]查找RotatedRect的宽度和高度[关闭]
【发布时间】:2019-02-14 07:11:35
【问题描述】:

请参阅This C++ code 我正在尝试在 python 中进行更改以执行完全相同的操作,但我被困在 for 循环以查找 aspect_ratio。我没有得到任何线索来获得 RotatedRect 的 minimumma​​ximum widthheight Python 中的 strong> 作为 Python 中 C++RotatedRect 的替代品是 cv2.boxPoints

import cv2
from matplotlib import pyplot as plt
img = cv2.imread('C:/Users/Zeeesh/Desktop/1.jpg', cv2.IMREAD_GRAYSCALE)
ret,thresh = cv2.threshold(img,200,255,0)
img1,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for contour in contours:
    rect = cv2.minAreaRect(contour)
    (x, y), (width, height), angle = rect
    aspect_ratio = min(width, height) / max(width, height)

    thresh1 = 0.2
    if (aspect_ratio > thresh1):
        print('Straight Line')
        for pt in contour:
            cv2.drawContours(out, [pt], 0,255,0)
    else:
        print('Curve')
        for pt in contour:
            cv2.drawContours(out, [pt], 0,0,255)






plt.imshow(out)

【问题讨论】:

    标签: python python-3.x opencv


    【解决方案1】:

    您可以找到旋转矩形的widthheight

    rect = cv2.minAreaRect(contour)
    (x, y), (width, height), angle = rect
    

    那么纵横比就是:

    aspect_ratio = min(width, height) / max(width, height)
    

    【讨论】:

    • 非常感谢您的帮助。我试图在这个This Image 中找到曲线,但它给了我一个错误。
    • 我已经根据我的问题编辑了你的代码,但这对我没有帮助
    • 以上代码已编辑。请帮帮我
    • 宽高是px吗?
    • @Fra 是的,当然
    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多