opencv之对图像中的点做几何变换

代码展示

# -*- 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)


效果展示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值