代码展示
# -*- coding:utf-8 -*-
import cv2
import numpy as np
def homo(mat):
if 2 == mat.ndim:
return np.vstack((mat, np.ones((1, mat.shape[1])))).copy()
else:
return np.hstack([mat, [1]]).copy()
def un_homo(mat):
temp = mat.copy().astype('float32')
temp /= temp[-1]
return temp[0:-1]
def project_points(pts, project_matrix):
assert project_matrix.shape[1] - pts.shape[0] < 2
if project_matrix.shape[1] == pts.shape[0]:
return project_matrix.dot(pts)
else:
return un_homo(project_matrix.dot(homo(pts)))
img = np.zeros((640, 640, 3), dtype=np.uint8)
h, w = img.shape[:2]
center_xy = (w // 2, h // 2)
pt_xy = np.array([[100], [100]])
for angle in range(1, 360):
rotate_matrix = cv2.getRotationMatrix2D(center=center_xy, angle=angle, scale=1)
rotate_image = cv2.warpAffine(img, rotate_matrix, (w, h))
tran_form_matrix = np.vstack((rotate_matrix, np.array([0.0, 0.0, 1.0])))
rot_pt = project_points(pt_xy, tran_form_matrix)
cv2.line(rotate_image, pt1=center_xy, pt2=tuple(rot_pt), color=(255, 0, 255), thickness=2)
cv2.circle(rotate_image, center=tuple(rot_pt), radius=5, color=(255, 0, 255), thickness=-1)
cv2.imshow('img', rotate_image)
cv2.waitKey(33)
效果展示