cv2.resize(src,dsize,fx=0,fy=0,interpolation=cv2.INTER_LINEAR)
参数:
实战!
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img=cv.imread("C:/Users/LEGION/Pictures/bqb/shuai.jpg")
#图像缩放
#绝对尺寸
rows,cols=img.shape[:2]#缩1/2即放大2倍
img1=cv.resize(img,(2*cols,2*rows),interpolation=cv.INTER_AREA)
#cv2.INTER_LINEAR 双线性插值法
#cv2.INTER_NEAREST 最近邻插值
#cv2.INTER_AREA 像素区域重采样(默认)
#cv2.CUBIC 双三次插值
#相对尺寸
img2=cv.resize(img,None,fx=0.3,fy=0.4)#别管我啦
fig,axes=plt.subplots(nrows=1,ncols=3,figsize=(10,8),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("yuan")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("da")
axes[2].imshow(img2[:,:,::-1])
axes[2].set_title("xiao")
plt.show()
忘记存结果了…想象一下好啦
cv.warpAffine(img,M,dsize)
实战!
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img=cv.imread("C:/Users/LEGION/Pictures/bqb/shu.jpg")
M=np.float32([[1,0,66],[0,1,66]])#平移矩阵
rows,cols=img.shape[:2]
img1=cv.warpAffine(img,M,(cols,rows))#先列数后行数
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("yuan")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("yi")
plt.show()
cv2.getRotationMatrix2D(center, angle, scale)
实战!
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img = cv.imread("C:/Users/LEGION/Pictures/bqb/zhu.jpg")
# 2 图像旋转
rows,cols = img.shape[:2]
# 2.1 生成旋转矩阵
M = cv.getRotationMatrix2D((cols/2,rows/2),90,1)
# 2.2 进行旋转变换
img1 = cv.warpAffine(img,M,(cols,rows))
# 3 图像展示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("yuan")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("zhuan")
plt.show()
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 图像读取
img = cv.imread("C:/Users/LEGION/Pictures/bqb/zhu.jpg")
# 2 仿射变换
rows,cols = img.shape[:2]
# 2.1 创建变换矩阵
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[100,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
# 2.2 完成仿射变换
img1 = cv.warpAffine(img,M,(cols,rows))
# 3 图像显示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("yuan")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("she")
plt.show()
cv.warpPerspective(img,T,(cols,rows))
参数:
实战!
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img = cv.imread("C:/Users/LEGION/Pictures/bqb/zhu.jpg")
# 2 透射变换
rows,cols = img.shape[:2]
# 2.1 创建变换矩阵
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[100,145],[300,100],[80,290],[310,300]])
T = cv.getPerspectiveTransform(pts1,pts2)
# 2.2 进行变换
img1 = cv.warpPerspective(img,T,(cols,rows))
# 3 图像显示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("yuan")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("tou")
plt.show()
cv.pyrUp(img) #对图像进行上采样
cv.pyrDown(img) #对图像进行下采样
实战
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img = cv.imread("C:/Users/LEGION/Pictures/bqb/zhu.jpg")
# 2 进行图像采样
up_img = cv.pyrUp(img) # 上采样操作:变大,分辨率高
img_1 = cv.pyrDown(img) # 下采样操作
# 3 图像显示
cv.imshow('enlarge', up_img)
cv.imshow('original', img)
cv.imshow('shrink', img_1)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果:
本次博客参考https://blog.csdn.net/qq_45066628/article/details/119353123
侵删