1. 寻找最小外接矩形,输入参数为轮廓,输出为rect对象。 2. rect[0]为最小外接矩形的中心点; 3. rect[1]为最小外接矩形的长和宽 4. rect[0]为旋转角度 rotatedRect = cv2.minAreaRect(contours[i])
5. cv2.getRotationMatrix2D计算旋转矩阵 6. cv2.warpAffine进行旋转矫正 angle = cv2.minAreaRect(contours[i])[-1] h, w = img1.shape[:2] center = (w//2, h//2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(img1, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
1. cv2.getRectSubPix(img,(height,width),(x,y)) 截取img中以(x,y)为中心,h为高,w为宽的图像 crop_img = cv2.getRectSubPix(rotated,(int(mingAreaRect[1][0]), int(mingAreaRect[1][1])), mingAreaRect[0])



