python opencv 进行简单几何变换
来源:互联网 发布:羽原大魔王知乎 编辑:程序博客网 时间:2024/06/05 07:20
16-python opencv 进行简单几何变换
- 16-python opencv 进行简单几何变换
- 概述
- 实现过程
- 读取原图并显示
- 放大
- 缩小
- 平移
- 旋转
- 源代码
- 运行结果
- 参考
概述
本节实现的是使用OpenCV里自带的函数,对图像进行简单的几何变换。
- 放大
- 缩小
- 平移
- 旋转
实现过程
读取原图并显示
不再赘述。
import cv2import numpy as np# read the originalimg = cv2.imread('../test2.jpg')cv2.imshow('original', img)
放大
利用OpenCV自带的resize()函数实现放大与缩小。其声明为:
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst
其中各个参数的意义如下:
- src – input image.
- dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.
- dsize –output image size; if it equals zero, it is computed as:
dsize = Size(round(fx × src.cols), round(fy × src.rows)) - fx –scale factor along the horizontal axis; when it equals 0, it is computed as
- fy –scale factor along the vertical axis; when it equals 0, it is computed as
- interpolation –interpolation method:
本文将原图放大至原来的2倍。
# expandrows, cols, channels = img.shapeimg_ex = cv2.resize(img, (2*cols, 2*rows), interpolation=cv2.INTER_CUBIC)cv2.imshow('expand', img_ex)
缩小
这里将原图缩小为原来的一半。
# zoomimg_zo = cv2.resize(img, (cols/2, rows/2), interpolation=cv2.INTER_AREA)cv2.imshow('zoom', img_zo)
平移
平移可以由平移矩阵描述:
其中
# transM = np.array([[1, 0, 50],[0, 1, 50]], np.float32)img_tr =cv2.warpAffine(img, M, img.shape[:2])cv2.imshow('trans', img_tr)
其中warpAffine()的声明如下:
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → dst
各个参数的意义如下:
- src – input image.
- dst – output image that has the size dsize and the same type as src .
- M – 2 × 3 transformation matrix.
- dsize – size of the output image.
- flags – combination of interpolation methods (see resize() ) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation.
- borderMode – pixel extrapolation method (see borderInterpolate()); when borderMode=BORDER_TRANSPARENT , it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function.
- borderValue – value used in case of a constant border; by default, it is 0.
旋转
利用getRotationMatrix2D()获得旋转矩阵,其声明为
cv2.getRotationMatrix2D(center, angle, scale) → retval
各个参数的意义:
- center – Center of the rotation in the source image.
- angle – Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
- scale – Isotropic scale factor.
- retval – The output affine transformation, 2x3 floating-point matrix.
然后再利用warpAffine()函数进行变换。
# RotationM=cv2.getRotationMatrix2D((cols/2,rows/2), 45, 1)img_ro =cv2.warpAffine(img, M, img.shape[:2])cv2.imshow('rotation', img_ro)
源代码
程序的源代码如下:
# created by Huang Lu# 2016/8/26 17:35# Department of EE, Tsinghua Univ.import cv2import numpy as np# read the originalimg = cv2.imread('../test2.jpg')cv2.imshow('original', img)# expandrows, cols, channels = img.shapeimg_ex = cv2.resize(img, (2*cols, 2*rows), interpolation=cv2.INTER_CUBIC)cv2.imshow('expand', img_ex)# zoomimg_zo = cv2.resize(img, (cols/2, rows/2), interpolation=cv2.INTER_AREA)cv2.imshow('zoom', img_zo)# transM = np.array([[1, 0, 50],[0, 1, 50]], np.float32)img_tr =cv2.warpAffine(img, M, img.shape[:2])cv2.imshow('trans', img_tr)# RotationM=cv2.getRotationMatrix2D((cols/2,rows/2), 45, 1)img_ro =cv2.warpAffine(img, M, img.shape[:2])cv2.imshow('rotation', img_ro)# wait the key and close windowscv2.waitKey(0)cv2.destroyAllWindows()
也可以参考我的GitHub上的,点击这里。
运行结果
在命令行进入该源程序所在目录后,运行python main.py
后即可显示结果。显示结果如下:
参考
- http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html
- OpenCV-Python-Toturial-中文版.pdf
- https://github.com/hlthu/Python/tree/master/Python_OpenCV/Geometric_Trans/
- http://hlthu.github.io/blogs/PythonOpenCV/16-Geometric_Trans.html
0 0
- python opencv 进行简单几何变换
- opencv-python几何变换
- opencv-python(8):几何变换
- Python下opencv图像的几何变换
- python opencv入门 几何变换(11)
- openCV—Python(5)——图像几何变换
- openCV—图像几何变换
- OpenCV 几何变换-图像缩放
- OpenCV 几何变换-图像平移
- OpenCV 几何变换-图像旋转
- opencv(12)---图像几何变换
- opencv 图像的几何变换
- OpenCV-图像的几何变换
- 一道简单的几何变换
- 一道简单的几何变换
- Python下opencv使用笔记(三)(图像的几何变换)
- 【opencv+python】图像处理之二、几何变换(仿射与投影)的原理
- 【opencv+python】图像处理之二、几何变换(仿射与投影)的应用
- JSP学习笔记---包含指令
- Mac下使用excel统计的几个坑
- RecyclerView 瞬间上手
- Linux命令与shell脚本编程学习笔记-16创建函数
- 快速获取网站页面
- python opencv 进行简单几何变换
- 移动端开发需要注意的一些事情
- JAVA 常用字符串用法
- 客户注册页--手机号验证--失败篇
- Overfencing_usaco2.4.2_bfs
- Q59:对称的二叉树
- Mysql MATCH() AGAINST() (MATCH+AGAINST)
- java Swing 自定义实现 下拉选择框,匹配输入提示
- JVM内存模型