1. 图像缩放
1.1. resize()函数
语法格式为:dst=cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
src
–原始图像
dsize
– 缩放大小
例:
b=cv2.resize(a,(122,122))
又 dst=cv2.resize(src, dsize, fx, fy)
fx , fy
缩放大小
例:b=cv2.resize(a,None,fx=0.5,fy=0.7)
1.2. 指定大小的缩放
#%%图像缩放resize函数 import cv2 img1 = cv2.imread(r"C:\Users\lihuanyu\Desktop\opencv\image\lenacolor.png") img2 = cv2.resize(img1,(200,200)) cv2.imshow("origial",img1) cv2.imshow("new",img2) print(img1.shape) cv2.waitKey() cv2.destroyAllWindows()
#%%按比例缩放 #先列后行 import cv2 img1 = cv2.imread(r"C:\Users\lihuanyu\Desktop\opencv\image\lenacolor.png") rows,cols = img1.shape[:2] size = (round(cols*0.5),round(rows*1.2)) img2 = cv2.resize(img1,size) cv2.imshow("origial",img1) cv2.imshow("news",img2) cv2.waitKey() cv2.destroyAllWindows()
1.3. resize函数(fx,fy)
#%%图像缩放-resize函数(fx,fy) import cv2 img1 = cv2.imread(r"C:\Users\lihuanyu\Desktop\opencv\image\lenacolor.png") img2 = cv2.resize(img1,None,fx = 1.2,fy = 0.5) cv2.imshow("origial",img1) cv2.imshow("news",img2) cv2.waitKey() cv2.destroyAllWindows()
2. 图像翻转-flip函数
语法:dst = cv2.flip( src, flipCode )
例:
img2 = cv2.flip(img1,0)—上下翻转
img2 = cv2.flip(img1,1)—以 Y 轴 为 对 称 轴 翻 转
img2 = cv2.flip(img1,-1)—在X轴和Y轴方向同时翻转
#%%图像的翻转 import cv2 img1 = cv2.imread(r"C:\Users\lihuanyu\Desktop\opencv\image\lenacolor.png") img2 = cv2.flip(img1,0)#上下翻转 #img2 = cv2.flip(img1,1)#以 Y 轴 为 对 称 轴 翻 转 #img2 = cv2.flip(img1,-1)#在X轴、 Y轴方向同时翻转 cv2.imshow("origial",img1) cv2.imshow("news",img2) cv2.waitKey() cv2.destroyAllWindows()