[英]rotate an image based on the two coordinates
這是三角形中的角度的基本數學。
如果你有兩個點(x1,y1)
, (x2, y2)
那么你可以計算dx = x2-x1
, dy = y2-y1
然后你可以計算tangens_alpha = dy/dx
和alpha = arcus_tangens(tangens_alpha)
然后你有angle
,你哈瓦用來計算旋轉- 90-alpha
在 Python 中,它將如下所示。 我從你的形象中獲得了分數。
因為圖像在左上角有(0,0)
,而不是像math
那樣在左下角,所以我使用dy = -(y2 - y1)
來翻轉它
import math
x1 = 295
y1 = 605
x2 = 330
y2 = 100
dx = x2 - x1
dy = -(y2 - y1)
alpha = math.degrees(math.atan2(dy, dx))
rotation = 90-alpha
print(alpha, rotation)
現在你可以使用PIL
/ pillow
或cv2
+ imutils
旋轉它
import math
import cv2
import imutils
x1 = 295
y1 = 605
x2 = 330
y2 = 100
dx = x2 - x1
dy = -(y2 - y1)
alpha = math.degrees(math.atan2(dy, dx))
rotation = 90-alpha
print(alpha, rotation)
img = cv2.imread('image.jpg')
img_2 = imutils.rotate(img, rotation)
cv2.imwrite('rotate.jpg', img_2)
img_3 = imutils.rotate_bound(img, -rotation)
cv2.imwrite('rotate_bound.jpg', img_3)
cv2.imshow('rotate', img_2)
cv2.imshow('rotate_bound', img_3)
cv2.waitKey(0)
旋轉.jpg
旋轉邊界.jpg
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.