• 插值
  • 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.

仿射变换。又称仿射映射。是指在几何中。一个向量空间进行一次线性变换并接上一个平移。变换为还有一个向量空间。

一个对向量 平移,与旋转放大缩小 的仿射映射为

上式在 齐次坐标上,等价于以下的式子

为了表示仿射变换。须要使用齐次坐标,即用三维向量 (xy, 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)-机器视觉-图像插值仿射的更多相关文章

  1. 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波

    拉普拉斯线性滤波,.边缘检測  . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...

  2. 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)

    Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...

  3. 数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波

    拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:myhaspl@myhaspl.com impor ...

  4. 数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)

    # -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #归一化块滤波 import cv2 import numpy as np fn="tes ...

  5. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)

    我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是  是图像中全部的灰度数, 是图像中全部的像素数,  实际上是图像的直方图,归一化到 . 把  作为相应于  的累计概率 ...

  6. 数学之路-python计算实战(19)-机器视觉-卷积滤波

    filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...

  7. 数学之路-python计算实战(13)-机器视觉-图像增强

    指数变换的基本表达式为:y=bc(x-a)-1 当中參数b.c控制曲线的变换形状,參数a控制曲线的位置. 指数变换的作用是扩展图像的高灰度级.压缩低灰度级.能够用于亮度过高的图像 本博客全部内容是原创 ...

  8. 数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)

    # -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #邻域平均法滤波,半径为2 import cv2 import numpy as np fn=&qu ...

  9. 数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )

    高斯滤波就是对整幅图像进行加权平均的过程.每个像素点的值,都由其本身和邻域内的其它像素值经过加权平均后得到.高斯滤波的详细操作是:用一个模板(或称卷积.掩模)扫描图像中的每个像素.用模板确定的邻域内像 ...

随机推荐

  1. Swift3.0:NSURLConnection的使用

    一.介绍 应用中也不必不可少的会使用网络通信,增强客户端和服务器的交互,可以使用NSURLConnection实现http通信. NSURLConnection提供了异步请求和同步请求两种请求方式.同 ...

  2. iOS:三种常见计时器(NSTimer、CADisplayLink、dispatch_source_t)的使用

    一.介绍 在iOS中,计时器是比较常用的,用于统计累加数据或者倒计时等,例如手机号获取验证码.计时器大概有那么三种,分别是:NSTimer.CADisplayLink.dispatch_source_ ...

  3. ping命令流程详解

    现在有以下需求,PC1的IP地址为192.168.0.10/24,PC2的IP地址为192.168.0.20/24,SW交换机的IP地址为192.168.0.30/24,问PC1能否ping通PC2? ...

  4. mybatis之foreach用法

    在做mybatis的mapper.xml文件的时候,我们时常用到这样的情况:动态生成sql语句的查询条件,这个时候我们就可以用mybatis的foreach了 foreach元素的属性主要有item, ...

  5. C#与Visual Basic的异与同

    C#是一种高级程序设计语言,是一种安全.稳定.简单.优雅的编程语言,它与Visual Basic有很多相同的地方,同时也有很多不同的地方.我们今天这篇博客本着学习C#的原则,着重介绍一下C#与Visu ...

  6. JPA(二):HellWord工程

    使用JPA持久化对象的操作步骤: 1)创建persistence.xml,在这个文件中配置持久化单元: --- 需要指定跟哪个数据库进行交互: --- 需要指定JPA使用哪个持久化的框架以及配置该框架 ...

  7. [Algorithm] Inorder Successor in a binary search tree

    For the given tree, in order traverse is: visit left side root visit right side // 6,8,10,11,12,15,1 ...

  8. 解决Android Studio提示gradle project sync failed报错的解决方法

    运行的时候报错,提示:gradle project sync failed 1.打开AS,切换到project目录结构依次进入目录app->gradle->gradle-wrapper.p ...

  9. oracle数据分组

    一,什么是分组函数 分组函数作用于一组数据,并对一组数据返回一个值 二,分组函数类型 Avg ,count,max,min,stddev(标准方差),sum. 函数名称 函数描述 Count 返回找到 ...

  10. VCAP5-DCA Objective 1.3 – Configure and Manage Complex Multipathing and PSA Plug-ins

    http://virtuallyhyper.com/2012/10/vcap5-dca-objective-1-3-configure-and-manage-complex-multipathing- ...