赞
踩
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
将一个图片缩放成和另一个图片的大小相同示例:
import cv2 import numpy as np dog = cv2.imread('./dog.jpeg') cat = cv2.imread('./cat.jpeg') print('dog',dog.shape) print('cat',cat.shape) # 把猫缩放成和狗一样大 new_cat = cv2.resize(cat, (499, 360)) print('new_cat',new_cat.shape) cv2.imshow('new_cat', np.hstack((dog, new_cat))) cv2.imshow('cat', cat) cv2.waitKey(0) cv2.destroyAllWindows()
四种插值算法的对比:
import cv2 import numpy as np dog = cv2.imread('./dog.jpeg') new_dog1 = cv2.resize(dog, (640, 480), interpolation=cv2.INTER_NEAREST) new_dog2 = cv2.resize(dog, (640, 480), interpolation=cv2.INTER_LINEAR)# 默认效果 new_dog3 = cv2.resize(dog, (640, 480), interpolation=cv2.INTER_CUBIC) new_dog4 = cv2.resize(dog, (640, 480), interpolation=cv2.INTER_AREA) cv2.imshow('dog', dog) cv2.imshow('new_dog1', new_dog1) cv2.imshow('new_dog2', new_dog2) cv2.imshow('new_dog3', new_dog3) cv2.imshow('new_dog4', new_dog4) cv2.waitKey(0) cv2.destroyAllWindows()
还可以按照x,y轴的比例进行缩放
import cv2
import numpy as np
dog = cv2.imread('./dog.jpeg')
new_dog = cv2.resize(dog, dsize=None, fx=1.5, fy=0.5, interpolation=cv2.INTER_AREA)
cv2.imshow('dog', dog)
cv2.imshow('new_dog', new_dog)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.flip(src, flipCode[, dst])
src 输入图像
flipCode 翻转形式
dst 输出图像
# flip import cv2 import numpy as np cv2.namedWindow('dog1',cv2.WINDOW_NORMAL) cv2.namedWindow('dog2',cv2.WINDOW_NORMAL) cv2.namedWindow('dog3',cv2.WINDOW_NORMAL) cv2.resizeWindow('dog1',(400,200)) cv2.resizeWindow('dog2',(400,200)) cv2.resizeWindow('dog3',(400,200)) dog = cv2.imread('./dog.jpeg') # 上下翻转 new_dog1 = cv2.flip(dog, 0) # 左右翻转 new_dog2 = cv2.flip(dog, 1) # 上下左右翻转 new_dog3 = cv2.flip(dog, -1) cv2.imshow('dog1', np.hstack((dog, new_dog1))) cv2.imshow('dog2', np.hstack((dog, new_dog2))) cv2.imshow('dog3', np.hstack((dog, new_dog3))) cv2.waitKey(0) cv2.destroyAllWindows()
src 输入图像
rotateCode: 旋转角度
dst 输出图像
# flip import cv2 import numpy as np dog = cv2.imread('./dog.jpeg') # - ROTATE_90_CLOCKWISE 90度顺时针 new_dog1 = cv2.rotate(dog, rotateCode=cv2.ROTATE_90_CLOCKWISE ) # - ROTATE_180 180度 new_dog2 = cv2.rotate(dog, rotateCode=cv2.ROTATE_180) # - ROTATE_90_COUNTERCLOCKWISE 90度逆时针 new_dog3 = cv2.rotate(dog, rotateCode=cv2.ROTATE_90_COUNTERCLOCKWISE) cv2.imshow('dog', dog) cv2.imshow('new_dog1', new_dog1) cv2.imshow('new_dog2', new_dog2) cv2.imshow('new_dog3', new_dog3) cv2.waitKey(0) cv2.destroyAllWindows()
仿射变换是图像旋转, 缩放, 平移的总称.具体的做法是通过一个矩阵和和原图片坐标进行计算, 得到新的坐标, 完成变换. 所以关键就是这个矩阵.
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
src:输入图像
M:仿射变换矩阵,一般反映平移或旋转的关系,可通过M = cv2.getRotationMatrix2D(center, angle, scale)获得
dst:输出图像。先宽度,后高度,即(宽度,高度)
dsize: 输出图片大小
flags: 与resize中的插值算法一致
borderMode: 边界外推法标志
borderValue: 填充边界值
平移矩阵
# 在进行旋转操作的时候, 不方便手动计算变换矩阵, # opencv提供了获取变换矩阵的API. import cv2 import numpy as np dog = cv2.imread('./dog.jpeg') # 高度,宽度,通道数 h, w, ch = dog.shape # 获取变换矩阵 # 规定按照逆时针转动图片 M = cv2.getRotationMatrix2D((w/2, h/2), 45, 1) # 注意opencv中是先宽度, 再高度 new_dog = cv2.warpAffine(dog, M, (w, h)) cv2.imshow('dog_ndog', np.hstack((dog,new_dog))) cv2.waitKey(0) cv2.destroyAllWindows()
仿射变换的难点就是计算变换矩阵, 仿射变换需要一个M矩阵,但是由于仿射变换比较复杂,一般直接很难找到这个矩阵,为此OpenCV提供了计算变换矩阵的API
M = cv2.getAffineTransform(src, dst) 通过三点可以确定变换后的位置, 相当于解方程, 3个点对应三个方程, 能解出偏移的参数和旋转的角度.
src 原始图像的三个点
dst 对应变换后的三个点
M 根据三个对应点求出对应的仿射变换矩阵
然后再使用函数cv2.warpAffine( )利用得到的M对原始图像进行变换即可。
# 通过三个点来确定变换矩阵 import cv2 import numpy as np dog = cv2.imread('./dog.jpeg') h, w, ch = dog.shape src = np.float32([[100, 100], [200, 100], [200, 300]]) dst = np.float32([[100, 150], [360, 200], [280, 120]]) # 需要原始图片的三个点坐标, 和变换之后的三个对应的坐标 M = cv2.getAffineTransform(src, dst) new_dog = cv2.warpAffine(dog, M, (w, h)) cv2.imshow('dog', dog) cv2.imshow('new_dog', new_dog) cv2.waitKey(0) cv2.destroyAllWindows()
透视变换就是将一种坐标系变换成另一种坐标系. 简单来说可以把一张"斜"的图变"正".
cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
src:输入图像
M:仿射变换矩阵,一般反映平移或旋转的关系,可通过 M = cv2.getPerspectiveTransform(src, dst[, solveMethod]) 获取透视变换的变换矩阵, 需要4个点, 即图片的4个角.
dsize: 输出图片大小
dst:输出图像。先宽度,后高度,即(宽度,高度)
flags: 与resize中的插值算法一致
borderMode: 边界外推法标志
borderValue: 填充边界值
import cv2 import numpy as np img = cv2.imread('./123.png') # print(img.shape) # 获取变换矩阵 # src是原图的4个坐标 src = np.float32([[100, 1100], [2100, 1100], [0, 4000], [2500, 3900]]) dst = np.float32([[0, 0], [2300, 0], [0, 3000], [2300, 3000]]) M = cv2.getPerspectiveTransform(src, dst) # 透视变换 new_img = cv2.warpPerspective(img, M, (2300, 3000)) cv2.namedWindow('img', cv2.WINDOW_NORMAL) cv2.resizeWindow('img', 640, 480) cv2.imshow('img', img) cv2.namedWindow('new_img', cv2.WINDOW_NORMAL) cv2.resizeWindow('new_img', 640, 480) cv2.imshow('new_img', new_img) cv2.waitKey(0) cv2.destroyAllWindows()
附OpenCV目录:OpenCV总目录学习笔记
智科专业小白,写博文不容易,如果喜欢的话可以点个赞哦!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。