本文介绍了查找 RotatedRect 的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参阅 此 C++ 代码for 循环以查找 aspect_ratio.我没有得到任何线索来获得 RotatedRect 的最小值和最大值 宽度和高度在 Python 中作为 C++ 的 RotatedRect 的替代,python 中的 strong> 是 cv2.boxPoints
Refer to This C++ code I am trying to change in python to do the exact same thing but I am stuck at for loop to find aspect_ratio. I am not getting any clue to get the minimum and maximum width and height of RotatedRect in python as an alternate to RotatedRect of C++ in Python is 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)
推荐答案
你可以找到旋转矩形的width
和height
:
You can find width
and height
of the rotated rect with:
rect = cv2.minAreaRect(contour)
(x, y), (width, height), angle = rect
那么纵横比为:
aspect_ratio = min(width, height) / max(width, height)
这篇关于查找 RotatedRect 的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!