图像的几何变换,
打开任意一个图像的编辑器,一般都可以进行对图像进行放大,缩小,旋转等操作,这类操作改变了原图中各区域的空间关系,对于这类操作,通常称为图像的几何变换.
完成一张图像的几何变换需要两个独立的算法,首先,需要一个算法实现空间坐标变换,用它描述每个像素如何从初始位置移动到终止位置,其次,还需要一个插值算法完成输出图像的每个像素的灰度值.
图像像素有2个属性,一个是像素的值,一个是像素的坐标,
scale:尺度变换
rotation:旋转变换
translation:平移
参考https://blog.csdn.net/frozenshore/article/details/50283583
借一个图
https://blog.csdn.net/qq_27261889/article/details/80720359
![](https://img-blog.csdnimg.cn/20190806103040596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODE0NTMxNw==,size_16,color_FFFFFF,t_70)
# usr/bin/env python
# coding: utf-8
##################### 对图像进行变换(旋转)
import cv2
import numpy as np
# 这里说一下旋转的opencv中为旋转提供的三个要素
# 旋转的中心点(center)
# 旋转角度()
# 旋转后进行放缩
# 我们可以通过cv2.getRotationMatrix2D函数得到转换矩阵
img_path='/home/jerry/PY_project_wang/one_stage_wang_0802/testing/4.jpg'
def rotation():
img = cv2.imread(img_path)
rows,cols,_ = img.shape
matrix = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
# 得到变换的矩阵,通过这个矩阵再利用warpAffine来进行变换
# 第一个参数就是旋转中心,元组的形式,这里设置成相片中心
# 第二个参数90,是旋转的角度
# 第三个参数1,表示放缩的系数,1表示保持原图大小
img1 = cv2.warpAffine(img,matrix,(cols,rows))
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.imshow('img1',img1)#旋转后的图片会与原图有一定区别,有一定的裁剪
cv2.waitKey(0)
cv2.destroyAllWindows()
##################### 对图像进行变换(三点得到一个变换矩阵)
# 我们知道三点确定一个平面,我们也可以通过确定三个点的关系来得到转换矩阵
# 然后再通过warpAffine来进行变换
def rotation2():
img = cv2.imread(img_path)
rows,cols,_ = img.shape
points1 = np.float32([[50,50],[200,50],[50,200]])
points2 = np.float32([[10,100],[200,50],[100,250]])
matrix = cv2.getAffineTransform(points1,points2)
output = cv2.warpAffine(img,matrix,(cols,rows))
cv2.imshow('input',img)
cv2.waitKey(0)
cv2.imshow('output',output)
cv2.waitKey(0)
cv2.destroyAllWindows()
##################### 对图像进行变换(四点得到一个变换矩阵)
# 进行透视变换
# 可以先用四个点来确定一个3*3的变换矩阵(cv2.getPerspectiveTransform)
# 然后通过cv2.warpPerspective和上述矩阵对图像进行变换
def toushi():
img = cv2.imread(img_path)
rows,cols,_ = img.shape
points1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
points2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
matrix = cv2.getPerspectiveTransform(points1,points2)
# 将四个点组成的平面转换成另四个点组成的一个平面
output = cv2.warpPerspective(img, matrix, (cols, rows))
# 通过warpPerspective函数来进行变换
cv2.imshow('img',img)
cv2.waitKey()
cv2.imshow('output',output)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == '__main__':
# rotation2()
toushi()
图像平移之translation
平移是最简单的图像的集合变换,看下图,来自
https://blog.csdn.net/qq_39507445/article/details/79018201
![](https://img-blog.csdnimg.cn/20190808144726762.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODE0NTMxNw==,size_16,color_FFFFFF,t_70)
假设某个像素的坐标为(x1,y1),让其先水平方向(x轴)平移400, 再沿纵轴(y轴)平移200,就得到上面的平移图片,推广到一般的情况,假设图像上任意一点(x1,y1),先沿x轴平移tx,再沿着y轴平移ty,最后得到坐标为(x2,y2)=(x1+tx,y1+ty),用矩阵表示就是:
![\begin{pmatrix} x2\\ y2\\ 1 \end{pmatrix}=\begin{pmatrix} 1 &0 & tx\\ 0 &1 & ty\\ 0 &0 & 1 \end{pmatrix}\begin{bmatrix} x\\ y\\ 1 \end{bmatrix}](https://private.codecogs.com/gif.latex?%5Cbegin%7Bpmatrix%7D%20x2%5C%5C%20y2%5C%5C%201%20%5Cend%7Bpmatrix%7D%3D%5Cbegin%7Bpmatrix%7D%201%20%260%20%26%20tx%5C%5C%200%20%261%20%26%20ty%5C%5C%200%20%260%20%26%201%20%5Cend%7Bpmatrix%7D%5Cbegin%7Bbmatrix%7D%20x%5C%5C%20y%5C%5C%201%20%5Cend%7Bbmatrix%7D)
其中,若tx>0,表示图片沿x轴正方向移动,若tx |