OpenCV图像处理之七种常用图像几何变换
作者:JeffchenITM 时间:2022-01-27 03:11:51
0 程序环境与所学函数
本章程序运行需要导入下面三个库,并定义了一个显示图像的函数
所学函数
##放大、缩小
cv.resize(img,dsize,[interpolation])
##平移变换
M = np.array([[...]], dtype=np.float32)
cv.warpAffine(img, M, dsize)
##镜像变换
cv.flip(img, 1) # 垂直镜像
cv.flip(img, 0) # 水平镜像
cv.flit(img, -1) # 水平垂直同时进行
##旋转变换
M = cv.getRotationMatrix2D(center, angle, scale)
img_rotate = cv.rotate(img, cv.ROTATE_90_CLOCKWISE)
##透视变换
M = cv.getPerspectiveTransform(src, dst)
img = cv.warpPerspective(img, M, dsize)
1 裁剪、放大、缩小
读入图像
img = cv.imread('pic/rabbit500x333.jpg')
show(img)
显示
裁剪:数组选择方法(冒号)
#裁剪
rabbit = img[150:450:] #限定行数,列数和三通道
show(rabbit)
显示
放大和缩小:resize()函数
插值方法
程序实现
#放大缩小
#cv.resize(img,dsize,[interpolation]) dsize表示大小,[interpolation]是插值方法,可选,有默认值
img2 = cv.resize(img,(500,400)) #放大为宽500高400
#使用定义插值方法
#一般来说放大地话选择LINEAR方法,缩小选择AREA方法
img3 = cv.resize(img,(500,400),interpolation=cv.INTER_NEAREST)
show(np.hstack([img2,img3]))
显示
2 平移变换
原理、平移矩阵推导
读入图像
img = cv.imread('pic/rabbit500x333.jpg')
show(img)
显示
程序实现
# M = np.array([[...]],dtype=np.float32)
# cv.warAffine(img,M,dsize) cv里面图像仿射变换函数,M是上面矩阵,dsize是输出图像大小
M=np.array([
[1,0,100],
[0,1,50]
],dtype=np.float32) #水平向右平移100个像素点,竖直向下平移50个像素点,原理见理论部分
img2 = cv.warpAffine(img,M,(333,500))
show(img2)
显示
3 错切变换
原理、错切矩阵推导
读入图像
img = cv.imread('pic/rabbit500x333.jpg')
show(img)
显示
水平错切
M = np.array([
[1,0.2,0],
[0,1,0]
],dtype=np.float32)
img3 = cv.warpAffine(img,M,(533,500))
show(img3)
显示
垂直错切
M = np.array([
[1,0,0],
[0.3,1,0]
],dtype=np.float32)
img3 = cv.warpAffine(img,M,(333,700))
show(img3)
显示
4 镜像变换
原理、镜像矩阵推导
读入图像
img = cv.imread('pic/rabbit500x333.jpg')
show(img)
显示
水平镜像
Mx = np.array([
[-1,0,333],
[0,1,0]
],dtype = np.float32)
img2 = cv.warpAffine(img,Mx,(333,500)) #仿射变换函数
show(img2)
显示
垂直镜像
My = np.array([
[1,0,0],
[0,-1,500]
],dtype=np.float32)
img3 = cv.warpAffine(img,My,(333,500))
show(img3)
显示
opencv内置函数实现镜像变换
#垂直镜像 cv.flip(img,1)
#水平镜像 cv.flip(img,0)
#水平垂直同时进行 cv.flip(img,-1)
程序实现
img4 = cv.flip(img,1) #垂直镜像
img5 = cv.flip(img,0) #水平镜像
img6 = cv.flip(img,-1) #水平垂直镜像同时进行
show(np.hstack([img4,img5,img6]))
显示
5 旋转变换
原理、旋转矩阵推导
读入图像
img = cv.imread('pic/rabbit500x333.jpg')
show(img)
显示
图像旋转
beta = np.pi/4
#旋转矩阵
M = np.array([
[np.cos(beta),np.sin(beta),0],
[-np.sin(beta),np.cos(beta),0]
],dtype=np.float32)
img2 = cv.warpAffine(img,M,(633,300))
show(img2)
显示
opencv内置获取旋转矩阵函数:
M = cv.getRotationMatrix2D(center,angle,scale)
center是旋转中心,angle是旋转角度,scale表示放大还是缩小
用上面函数获取旋转矩阵并实现图像旋转
h,w,c = img.shape #获取图像的高度和宽度,方便后面设置旋转中心
M2 = cv.getRotationMatrix2D((w//2,h//2),45,1)
img3 = cv.warpAffine(img,M2,(533,500)) #仿射函数实现
show(img3
显示
opencv内置实现图像旋转函数
img_rotate =cv.rotate(img,cv.ROTATE_90_COUNTERCLOCKWISE)
只能进行90度倍数的旋转
程序实现
# 逆时针旋转90度
img_rotate = cv.rotate(img,cv.ROTATE_90_COUNTERCLOCKWISE)
show(img_rotate)
显示
6 透视变换
M = cv.getPerspectiveTransform(str,dst)
str:原始图像矩阵端点位置,dst:目标图像矩阵位置
img2 = cv.warpPerspective(img,M,(w,h))
读入图像
img = cv.imread('pic/parthenon500x750.jpg')
show(img)
显示
程序实现
#在原图中定位四个点,这里找的是柱子前面四个点的大概位置,眼睛观察法找的
str = np.array([
[210,50],
[610,270],
[650,470],
[150,450]
],dtype=np.float32)
#目标图像中矩阵
dst = np.array([
[150,50],
[650,50],
[650,470],
[150,470]
],dtype=np.float32)
h,w,c = img.shape
#透视变换将一个类似矩形的图形拉成一个矩形
M = cv.getPerspectiveTransform(str,dst)
img2 = cv.warpPerspective(img,M,(w,h))
show(img2)
显示
应用:车道检测、图片矫正
7 最近邻插值、双线性插值
原理:
最近邻插值图示:
双线性插值图示
读入图像
img = cv.imread('pic/rabbit50x33.jpg')
show(img)
显示
程序实现
img1 = cv.resize(img,(330,500),interpolation=cv.INTER_NEAREST) #最近邻插值
img2 = cv.resize(img,(330,500),interpolation=cv.INTER_LINEAR_EXACT) #精确双线新插值
show(np.hstack([img1,img2]))
显示
可以看出最近邻插值还是比较模糊的,过渡结果没有双线性插值平滑
来源:https://blog.csdn.net/weixin_43498449/article/details/121874204
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言HTTPServer开发的六种方式小结
python破解同事的压缩包密码
![](https://img.aspxhome.com/file/2023/2/112102_0s.jpg)
使用django-guardian实现django-admin的行级权限控制的方法
python生成图片验证码的方法
![](https://img.aspxhome.com/file/2023/9/87829_0s.jpg)
Python函数中的可变长参数详解
![](https://img.aspxhome.com/file/2023/4/71314_0s.png)
如何从ASP连接到Oracle Server?
Python实现对二维码数据进行压缩
![](https://img.aspxhome.com/file/2023/2/92172_0s.png)
ASP环境中使用QQ纯真版IP数据库QQWry.dat
Python中yield返回生成器的详细方法
python追加元素到列表的方法
Python Pygame中精灵和碰撞检测详解
转换字符串单词的第一个字母为大写
ubuntu lamp(apache+mysql+php) 环境搭建及相关扩展更新
Oracle判断表、列、主键是否存在的方法
分享Python 的24个编程超好用技巧
解决GOPATH在GOLAND中的坑
![](https://img.aspxhome.com/file/2023/0/97170_0s.jpg)
JavaScript判断各种浏览器类型及版本
Flask框架利用Echarts实现绘制图形
![](https://img.aspxhome.com/file/2023/3/118413_0s.png)