1. 尋找最小外接矩形,The input parameter is the contour,輸出為rect對象.
2. rect[0]is the center point of the smallest circumscribed rectangle;
3. rect[1]are the length and width of the smallest enclosing rectangle
4. rect[0]為旋轉角度
rotatedRect = cv2.minAreaRect(contours[i])
5. cv2.getRotationMatrix2D計算旋轉矩陣
6. cv2.warpAffinePerform rotation correction
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)