This C++ code 我试图在python中做同样的事情,但我被困在 最低限度 和 宽度 高度 属于 旋转体 旋转体 属于 在Python中是 cv2.1点
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 旋转矩形的:
width
height
rect = cv2.minAreaRect(contour) (x, y), (width, height), angle = rect
aspect_ratio = min(width, height) / max(width, height)