import cv2
import math
img = cv2.imread('test.jpg') #讀取影像
(h, w) = img.shape[:2] #取得影像長寬
center = (w/2, h/2) #取得影像中央座標位置
#旋轉影像
angle = 60 #設置旋轉角度
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_img = cv2.warpAffine(img, M, (w, h))
cv2.imwrite('result.jpg', rotated_img)
#未旋轉時, 方框的四個座標位置
(x1, y1) = (227, 121)
(x2, y2) = (436, 121)
(x3, y3) = (227, 381)
(x4, y4) = (436, 381)
#轉換後, 四個座標位置
tmp_angle = (angle * math.pi)/180
(tmp_x1,tmp_y1) = (x1-center[0], y1-center[1])
(tmp_x2, tmp_y2) = (x2-center[0], y2-center[1])
(tmp_x3, tmp_y3) = (x3-center[0], y3-center[1])
(tmp_x4, tmp_y4) = (x4-center[0], y4-center[1])
X1 = int(tmp_x1 * math.cos(tmp_angle) + tmp_y1 * math.sin(tmp_angle) + center[0])
Y1 = int(tmp_x1 * math.sin(tmp_angle) + tmp_y1 * math.cos(tmp_angle) + center[1])
X2 = int(tmp_x2 * math.cos(tmp_angle) + tmp_y2 * math.sin(tmp_angle) + center[0])
Y2 = int(tmp_x2 * math.sin(tmp_angle) + tmp_y2 * math.cos(tmp_angle) + center[1])
X3 = int(tmp_x3 * math.cos(tmp_angle) + tmp_y3 * math.sin(tmp_angle) + center[0])
Y3 = int(tmp_x3 * math.sin(tmp_angle) + tmp_y3 * math.cos(tmp_angle) + center[1])
X4 = int(tmp_x4 * math.cos(tmp_angle) + tmp_y4 * math.sin(tmp_angle) + center[0])
Y4 = int(tmp_x4 * math.sin(tmp_angle) + tmp_y4 * math.cos(tmp_angle) + center[1])
※查詢其他OpenCV用法如下※
openCV應用總整理: 點擊我
留言列表