Función warpPerspective de transformación de perspectiva de imagen de Python + OpenCV
1. Introducción a la función
warpPerspective() : Perspectiva transforma la imagen. En pocas palabras, existe una imagen de este tipo, su ángulo de disparo no se toma desde el frente, pero con un cierto ángulo, esperamos obtener la perspectiva observada desde el frente.
2. Ejemplo de código
Aquí usamos una imagen de cuatro naipes tomada oblicuamente desde arriba, y usamos el método de perspectiva de imagen para extraer las principales vistas en perspectiva de los tres naipes J, Q y K.
el código se muestra a continuación
import cv2
import numpy as np
img = cv2.imread("Photos/cards.jpg")
width,height = 250,350 #所需图像大小
#找K
pts1 = np.float32([[527,144],[772,192],[404,396],[677,457]]) #所需图像部分四个顶点的像素点坐标
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]]) #定义对应的像素点坐标
matrix_K = cv2.getPerspectiveTransform(pts1,pts2) #使用getPerspectiveTransform()得到转换矩阵
img_K = cv2.warpPerspective(img,matrix_K,(width,height)) #使用warpPerspective()进行透视变换
#找Q
pts3 = np.float32([[63,325],[340,279],[89,634],[403,573]])
pts4 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix_Q = cv2.getPerspectiveTransform(pts3,pts4)
img_Q = cv2.warpPerspective(img,matrix_Q,(width,height))
#找J
pts5 = np.float32([[777,107],[1019,84],[842,359],[1117,332]])
pts6 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix_J = cv2.getPerspectiveTransform(pts5,pts6)
img_J = cv2.warpPerspective(img,matrix_J,(width,height))
cv2.imshow("Original Image",img)
cv2.imshow("img K",img_K)
cv2.imshow("img Q",img_Q)
cv2.imshow("img J",img_J)
cv2.waitKey(0)
3. Darse cuenta del efecto