Opencv para transformación geométrica de imágenes

1.cv.resize () para cambiar el tamaño de la imagen

Imagen original (opencv se convertirá en una imagen BGR después de leerla):Inserte la descripción de la imagen aquí

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)

Imagen de efecto:
Inserte la descripción de la imagen aquí

2.cv.warpAffine () para la transformación de imágenes

Utilice np para crear la matriz de traducción MMM y luego páselo a warpAffine ().
M = [1 0 tx 0 1 ty] M = \ begin {bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \ end {bmatrix}METRO=[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()

Inserte la descripción de la imagen aquí

3.cv.getRotationMatrix2D () para obtener la matriz de rotación

旋转 矩阵 :M = [cos θ - sin θ sin θ cos θ] M = \ begin {bmatrix} cos \ theta & -sin \ theta \\ sin \ theta & cos \ theta \ end {bmatrix}METRO=[c o s θs i n θ- s i n θc o s θ] , El ángulo esθ \ thetaθ

OpenCV proporciona rotación escalable y centro de rotación ajustable, que se puede rotar en cualquier posición. La matriz de transformación modificada es:
[α β (1 - α) ⋅ centro. X - β ⋅ centro. Y - β α β ⋅ centro. x + (1 - α) ⋅ centro. 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}[un- bsegundoun( 1-a )c e n t e r . X-segundocenter.ysegundoc e n t e r . X+( 1-a )center.y]
其中 :
α = escala ⋅ cos ⁡ θ, β = escala ⋅ sin ⁡ θ \ begin {array} {l} \ alpha = scale \ cdot \ cos \ theta, \\ \ beta = scale \ cdot \ sin \ theta \ end {arrayun=s c a l eporqueθ ,segundo=s c a l esinyo

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()

Inserte la descripción de la imagen aquí

cv.getAffineTransform () para obtener la matriz de transformación afín

En la transformación afín, todas las líneas paralelas de la imagen original seguirán siendo paralelas en la imagen de salida.

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)

Inserte la descripción de la imagen aquí

cv.getPerspectiveTransform () para obtener la matriz de transformación de perspectiva

Para la transformación de perspectiva, necesita una matriz de transformación de 3x3. Incluso después de la conversión, la línea recta seguirá siendo recta. Para encontrar esta matriz de transformación, necesita 4 puntos en la imagen de entrada y los puntos correspondientes en la imagen de salida. De estos cuatro puntos, tres de ellos no deben ser colineales. Luego, la matriz de transformación se puede encontrar a través de la función cv.getPerspectiveTransform . Luego envíe la submatriz a 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)

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/qq_41214679/article/details/112840921
Recomendado
Clasificación