opencv进行图像的几何变换

1.cv.resize()改变图像大小

原始图像(opencv读取后会变为BGR图像):在这里插入图片描述

import cv2 as cv
img = cv.imread('1.JPG',0)

img = cv.resize(img,(256,256))  #改变大小到(256,256)

cv.imshow('img',img)
cv.imwrite('img.jpg',img)
cv.waitKey(0)

效果图:
在这里插入图片描述

2.cv .warpAffine()进行图像变换

使用np创建平移矩阵 M M M,而后将其传递给warpAffine()。
M = [ 1 0 t x 0 1 t y ] M = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{bmatrix} M=[1001txty]

import cv2 as cv
import numpy as np
img = cv.imread('1.JPG',0)
img = cv.resize(img,(256,256))

rows,cols = img.shape
M = np.float32([[1,0,50],[0,1,50]])   #转移矩阵
dst = cv.warpAffine(img,M,(cols,rows))      #参数:平移图片,转移矩阵,输出大小(width,height)

cv.imshow('img',dst)
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述

3.cv .getRotationMatrix2D()获得旋转矩阵

旋转矩阵: M = [ c o s θ − s i n θ s i n θ c o s θ ] M = \begin{bmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{bmatrix} M=[cosθsinθsinθcosθ],角度为 θ \theta θ

OpenCV提供了可缩放的旋转以及可调整的旋转中心,可以在任何位置旋转,修改后的变换矩阵为:
[ α β ( 1 − α ) ⋅ c e n t e r . x − β ⋅ c e n t e r . y − β α β ⋅ c e n t e r . x + ( 1 − α ) ⋅ c e n t e r . y ] \begin{bmatrix} \alpha & \beta & (1- \alpha ) \cdot center.x - \beta \cdot center.y \\ - \beta & \alpha & \beta \cdot center.x + (1- \alpha ) \cdot center.y \end{bmatrix} [αββα(1α)center.xβcenter.yβcenter.x+(1α)center.y]
其中:
α = s c a l e ⋅ cos ⁡ θ , β = s c a l e ⋅ sin ⁡ θ \begin{array}{l} \alpha = scale \cdot \cos \theta , \\ \beta = scale \cdot \sin \theta \end{array} α=scalecosθ,β=scalesinθ

import cv2 as cv
img = cv.imread('1.JPG',0)
img = cv.resize(img,(256,256))
rows,cols = img.shape

# cols-1 和 rows-1 是坐标限制
M = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1)  #参数:center, angle, scale
dst = cv.warpAffine(img,M,(cols,rows))    # #参数:img:平移图片,M:变换矩阵,输出大小(width,height)

cv.imshow('img',dst)
cv.imwrite('img.jpg',dst)
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述

cv .getAffineTransform()获得仿射变换矩阵

在仿射变换中,原始图像中的所有平行线在输出图像中仍将平行。

import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
img = cv.imread('1.JPG')

rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
dst = cv.warpAffine(img,M,(cols,rows))

plt.figure(12)
plt.subplot(121),plt.axis('off'),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.axis('off'),plt.imshow(dst,),plt.title('Output')
plt.savefig('img.jpg',)
plt.show()
cv.waitKey(0)

在这里插入图片描述

cv.getPerspectiveTransform()获得透视变换矩阵

对于透视变换,您需要3x3变换矩阵。即使在转换后,直线也将保持直线。要找到此变换矩阵,您需要在输入图像上有4个点,在输出图像上需要相应的点。在这四个点中,其中三个不应共线。然后可以通过函数cv.getPerspectiveTransform找到变换矩阵。然后将次矩阵送入cv .warpPerspective

import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
img = cv.imread('1.JPG')
rows,cols,ch = img.shape

pts1 = np.float32([[0,0],[300,0],[0,300],[300,300]])  #源图像中四边形顶点的坐标
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])   #目标图像中相应四边形顶点的坐标。
M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))

plt.figure(12)
plt.subplot(121),plt.axis('off'),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.axis('off'),plt.imshow(dst,),plt.title('Output')
plt.savefig('img.jpg',)
plt.show()
cv.waitKey(0)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41214679/article/details/112840921