import cv2
import json
import os
from math import *
def rota(img,degree,W,H,x1,y1,x2,y2):
if degree == 90:
tempx = x1
x1 = y1
y1 = W - tempx
tempx = x2
x2 = y2
y2 = W - tempx
elif degree == 180:
x1 = W - x1
y1 = H - y1
x2 = W - x2
y2 = H - y2
elif degree == 270:
tempx = x1
x1 = H - y1
y1 = tempx
tempx = x2
x2 = H - y2
y2 = tempx
height, width = img.shape[:2]
# 旋轉後的尺寸
heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
matRotation[0, 2] += (widthNew - width) / 2 # 重點在這步,目前不懂爲什麼加這步
matRotation[1, 2] += (heightNew - height) / 2 # 重點在這步
imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
return imgRotation,x1,y1,x2,y2
with open('兩個點.txt') as f:
datas = f.readlines()
for data in datas:
data = data.split(' ')
image = cv2.imread(os.path.join('jinglingbiaozhu',data[0]))
H,W,_ = image.shape
x1 = int(data[1])
y1 = int(data[2])
x2 = int(data[3])
y2 = int(data[4])
image,x1,y1,x2,y2 = rota(image,270,W,H,x1,y1,x2,y2)
cv2.circle(image, (x1, y1), 60,(0, 0, 255), 0)
cv2.circle(image, (x2, y2), 60, (0, 0, 255), 0)
cv2.imshow('',image)
cv2.waitKey(0)