Ros*_*ber -3 python opencv python-3.x
请参阅此C ++代码,我尝试在python中进行更改以执行完全相同的操作,但我陷入for循环中以找到Aspect_ratio。我没有得到任何线索,以获得最小和最大 宽度和高度的RotatedRect在python作为替代品RotatedRect的C ++中Python是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)
Run Code Online (Sandbox Code Playgroud)
您可以找到width和height的旋转矩形:
rect = cv2.minAreaRect(contour)
(x, y), (width, height), angle = rect
Run Code Online (Sandbox Code Playgroud)
那么长宽比是:
aspect_ratio = min(width, height) / max(width, height)
Run Code Online (Sandbox Code Playgroud)