我想用python做图形旋转,如下图,有什么好的方法自动旋转到水平位置呢?初学opencv 想,不知该如何入手。
我想用python做图形旋转,如下图,有什么好的方法自动旋转到水平位置呢?初学opencv 想,不知该如何入手。
我尝试做了一下,代码如下:
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('image.png')
ROTATED_SIZE = 1200
CUT_SIZE = 0
W_cols, H_rows= image.shape[:2]
position = np.float32([[976, 393], [325, 6], [95, 391], [746, 777]])
position_t = np.float32([[0, 0],[ROTATED_SIZE,0],[ROTATED_SIZE, ROTATED_SIZE],[0,ROTATED_SIZE]])
image_T = cv2.getPerspectiveTransform(position, position_t)
result = cv2.warpPerspective(image, image_T, (ROTATED_SIZE,ROTATED_SIZE))
result = cv2.flip(result, 1, dst=None)
result = cv2.flip(result, -1)
result = cv2.resize(result, (800, 500), interpolation = cv2.INTER_AREA)
cv2.imshow("original_imge",image)
cv2.imshow("result",result)
cv2.imwrite("./reimage.png", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
输入图像:
输出图像:
如果有帮助,请采纳,谢谢!