数学之路-python计算实战(9)-机器视觉-图像插值仿射
- 插值
- Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst
- interpolation –
interpolation method:
- INTER_NEAREST - a nearest-neighbor interpolation
- INTER_LINEAR - a bilinear interpolation (used by default)
- INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to theINTER_NEAREST method.
- INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
- INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
# -*- coding: utf-8 -*-
import cv2 fn="test2.jpg"
img=cv2.imread(fn)
w=img.shape[1]
h=img.shape[0] #放大,双立方插值
newimg1=cv2.resize(img,(w*2,h*2),interpolation=cv2.INTER_CUBIC)
#放大, 近期邻插值
newimg2=cv2.resize(img,(w*2,h*2),interpolation=cv2.INTER_NEAREST)
#放大, 象素关系重採样
newimg3=cv2.resize(img,(w*2,h*2),interpolation=cv2.INTER_AREA)
#缩小, 象素关系重採样
newimg4=cv2.resize(img,(300,200),interpolation=cv2.INTER_AREA) cv2.imshow('preview1',newimg1)
cv2.imshow('preview2',newimg2)
cv2.imshow('preview3',newimg3)
cv2.imshow('preview4',newimg4)
cv2.waitKey()
cv2.destroyAllWindows()
仿射可进行缩放、旋转、平衡操作
- Python: cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → dst
- C: void cvWarpAffine(const CvArr* src, CvArr* dst, const CvMat* map_matrix, intflags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, CvScalar fillval=cvScalarAll(0) )
- Python: cv.WarpAffine(src, dst, mapMatrix, flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, fillval=(0, 0, 0, 0)) → None
highlight=warpaffine#cv.WarpAffine" title="Permalink to this definition" style="color: rgb(101, 161, 54); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;">
- C: void cvGetQuadrangleSubPix(const CvArr* src, CvArr* dst, const CvMat*map_matrix)
highlight=warpaffine#void cvGetQuadrangleSubPix(const CvArr* src, CvArr* dst, const CvMat* map_matrix)" title="Permalink to this definition" style="color: rgb(101, 161, 54); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;">
- Python: cv.GetQuadrangleSubPix(src, dst, mapMatrix) → None
-
Parameters: - src – input image.
- dst – output image that has the size dsize and the same type assrc .
- M –
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 (seeborderInterpolate()); 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.
The function warpAffine transforms the source image using the specified matrix:

getRotationMatrix2D
Calculates an affine matrix of 2D rotation.
- C++: Mat getRotationMatrix2D(Point2f center, double angle, double scale)
- Python: cv2.getRotationMatrix2D(center, angle, scale) → retval
highlight=warpaffine#cv2.getRotationMatrix2D" title="Permalink to this definition" style="color: rgb(101, 161, 54); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;">
- C: CvMat* cv2DRotationMatrix(CvPoint2D32f center, double angle, double scale, CvMat* map_matrix)
- Python: cv.GetRotationMatrix2D(center, angle, scale, mapMatrix) → None
-
Parameters: - 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.
- map_matrix – The output affine transformation, 2x3 floating-point matrix.
The function calculates the following matrix:

where

The transformation maps the rotation center to itself. If this is not the target, adjust the shift.
仿射变换。又称仿射映射。是指在几何中。一个向量空间进行一次线性变换并接上一个平移。变换为还有一个向量空间。
一个对向量
平移
,与旋转放大缩小
的仿射映射为
上式在 齐次坐标上,等价于以下的式子

-
为了表示仿射变换。须要使用齐次坐标,即用三维向量 (x, y, 1) 表示二维向量,对于高维来说也是如此。依照这样的方法。就能够用矩阵乘法表示变换。
;
变为在矩阵中添加一列与一行,除右下角的元素为 1 外其他部分填充为 0,通过这样的方法,全部的线性变换都能够转换为仿射变换。比如,上面的旋转矩阵变为
通过这样的方法,使用与前面一样的矩阵乘积能够将各种变换无缝地集成到一起
# -*- coding: utf-8 -*-
import cv2 fn="test3.jpg"
img=cv2.imread(fn)
w=img.shape[1]
h=img.shape[0]
#得到仿射变换矩阵,完毕旋转
#中心
mycenter=(h/2,w/2)
#旋转角度
myangle=90
#缩放尺度
myscale=0.5
#仿射变换完毕缩小并旋转
transform_matrix=cv2.getRotationMatrix2D(mycenter,myangle,myscale) newimg=cv2.warpAffine(img,transform_matrix,(h,w))
cv2.imshow('preview',newimg) cv2.waitKey()
cv2.destroyAllWindows()
本博客全部内容是原创,假设转载请注明来源
http://blog.csdn.net/myhaspl/
本博客全部内容是原创。假设转载请注明来源
http://blog.csdn.net/myhaspl/
数学之路-python计算实战(9)-机器视觉-图像插值仿射的更多相关文章
- 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波
拉普拉斯线性滤波,.边缘检測 . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...
- 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)
Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...
- 数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波
拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:myhaspl@myhaspl.com impor ...
- 数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)
# -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #归一化块滤波 import cv2 import numpy as np fn="tes ...
- 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)
我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是 是图像中全部的灰度数, 是图像中全部的像素数, 实际上是图像的直方图,归一化到 . 把 作为相应于 的累计概率 ...
- 数学之路-python计算实战(19)-机器视觉-卷积滤波
filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...
- 数学之路-python计算实战(13)-机器视觉-图像增强
指数变换的基本表达式为:y=bc(x-a)-1 当中參数b.c控制曲线的变换形状,參数a控制曲线的位置. 指数变换的作用是扩展图像的高灰度级.压缩低灰度级.能够用于亮度过高的图像 本博客全部内容是原创 ...
- 数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)
# -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #邻域平均法滤波,半径为2 import cv2 import numpy as np fn=&qu ...
- 数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )
高斯滤波就是对整幅图像进行加权平均的过程.每个像素点的值,都由其本身和邻域内的其它像素值经过加权平均后得到.高斯滤波的详细操作是:用一个模板(或称卷积.掩模)扫描图像中的每个像素.用模板确定的邻域内像 ...
随机推荐
- iOS开发-UIActivityIndicatorView简单使用
软件开发的时候经常会遇到半天才加载出来数据的情况,不管是程序写的烂,还是说本来网速比较慢,一般都都会给个提示让用户感觉到我们在努力的加载数据,iOS可以通过UIActivityIndicatorVie ...
- 漂亮的CSS3提交意见输入框样式
做了个输入框样式,如图: CSS代码如下: <喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" cl ...
- 通过js去掉所有的html标签,得到HTML标签中的所有内容
<script> //替换掉所有的 html标签,得到Html标签中的内容 var s="<P><FONT face=宋体 color=#000000> ...
- MFC中打印对话框CPrintDialog类
void CCPrintDialogView::OnPrint() { DWORD dwflags=PD_ALLPAGES|PD_NOPAGENUMS|PD_USEDEVMODECOPIES|PD_S ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
- FireDAC中的SQLite(二)
我们接下来将要使用FDDemo.sdb数据库进行访问,开始我们的第一个SQLite访问例子. 我们的FDDemo.sdb存放目录在:C:\Program Files (x86)\Embarcadero ...
- mysql function 中使用游标
create function p_t1(tid varchar(20))returns varchar(200)begin declare tmpName varchar(50) default ' ...
- 人生规划和GTD——"知"、"得"与"合"
作为<小强升职记>的读书感悟,给我自己.作为分享,也送给或许需要的给你. 我不知道你是否真的需要.但我受Amy师姐等一众人的影响,已经爱上了分享.呵呵,话也可以倒过来说,其实分享也就是爱. ...
- LintCode: Search A 2d Matrix
1. 设查找的数位y,第一行最后一列的数位x 如果x<y,x是第一行最大的,所以第一行都小于y,删除第一行: 如果x>y,x是最后一列最小的,所以最后一列都大于y,删除最后一列: 这样保证 ...
- vsphere产品下载列表
https://my.vmware.com/cn/web/vmware/info/slug/datacenter_cloud_infrastructure/vmware_vsphere_with_op ...


