OpenCv 几何变换
几何变换
import cv2
import numpy as np
cv2.waitKey(0)
cv2.destroyAllWindows()
1.图像的简单缩放
img=cv2.imread("lena.jpg",0)
rows,cols,_ = img.shape
//第一种缩放方式
size1 = (int(rows*0.9),int(cols*0.8))//宽变成0.9倍,长变成0.8倍
img_resize1 = cv2.resize(img,size1)
//第二种缩放方式
img_resize2 = cv2.resize(img,None,fx=2,fy=1.5)//宽变成两倍,长变成1.5倍cv2.imshow("SRC",img)
cv2.imshow("RESIZE1",img_resize1)
cv2.imshow("RESIZE2",img_resize2)
2.图像的翻转
mg_flip1=cv2.flip(img,0)//x轴翻转
mg_flip2=cv2.flip(img,1)//y轴翻转
mg_flip1=cv2.flip(img,-1)//x,y轴翻转
2.图像的平移
rows,cols,_ = img.shape
x = 100
y = 200
M = np.float32([[1,0,x],[0,1,y]])//转换矩阵
img_move=cv2.warpAffine(img,M,(cols,rows))//平移操作,注意(cols,rows)
cv2.imshow("img",img_move)
3.图像的旋转
height,width,_ = img.shape
M = cv2.getRotationMatrix2D((width/2, height/2), 45, 0.6)
img_rotate=cv2.warpAffine(img,M,(width, height))//平移操作
cv2.imshow("ROTATE",img_rotate)
4.图像的透视
img = cv2.imread("lianxi.png")
row,cols = img.shape[:2]
pts_o = np.float32([[45, 180], [220, 80], [60, 290], [270, 190]])
pts_d = np.float32([[0, 0], [200, 0], [0, 100], [200, 100]])
M = cv2.getPerspectiveTransform(pts_o, pts_d)
dst = cv2.warpPerspective(img, M, (200, 100))
cv2.imshow('img', img)
cv2.imshow('dst', dst)