close

Python - OpenCV Affine + Projective

 

=====================================


# https://blog.csdn.net/on2way/article/details/46801063
# https://blog.csdn.net/a352611/article/details/51418178

import cv2
import numpy as np

img = cv2.imread('C:\\Users\\Lightel-Ricky\\Desktop\\14.png',cv2.IMREAD_COLOR)
img2 = cv2.imread('C:\\Users\\Lightel-Ricky\\Desktop\\15.png',cv2.IMREAD_COLOR)
# .缩放 Scaling

ResizeImg = cv2.resize(src=img,dsize=None,fx=0.5, fy=2,interpolation=cv2.INTER_LINEAR)
'''
scr:原图

dsize:输出图像尺寸
fx:沿水平轴的比例因子
fy:沿垂直轴的比例因子
interpolation:插值方法

'''

# 旋转 Rotate
RotateMatrix = cv2.getRotationMatrix2D(center=(img.shape[1]/2, img.shape[0]/2), angle=40, scale=1)
RotImg = cv2.warpAffine(img, RotateMatrix, (img.shape[0]*1, img.shape[1]*1))

# 仿射 Affine


SrcPointsA = np.float32([[146,378],[150,120],[438,172]])
CanvasPointsA = np.float32([[2,510],[0,0],[505,4]])

AffineMatrix = cv2.getAffineTransform(np.array(SrcPointsA),
                                      np.array(CanvasPointsA))
AffineImg = cv2.warpAffine(img, AffineMatrix, (img.shape[1], img.shape[0]))

# 投影 Projective

SrcPointsB = np.float32([[77,465],[403,110],[1208,372],[978,831]])
CanvasPointsB = np.float32([[188,819],[188,86],[1107,86],[1107,819]])

PerspectiveMatrix = cv2.getPerspectiveTransform(np.array(SrcPointsB), np.array(CanvasPointsB))
PerspectiveImg = cv2.warpPerspective(img2, PerspectiveMatrix, (img2.shape[1], img2.shape[0]))


#cv2.imshow('My Image1', img2 )
cv2.imshow('My Image', PerspectiveImg )

cv2.waitKey(0)

=====================================

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ricky10116r2d2 的頭像
    ricky10116r2d2

    ricky10116r2d2的部落格

    ricky10116r2d2 發表在 痞客邦 留言(0) 人氣()