数学之路-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, int flags=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
C:  void  cvGetQuadrangleSubPix (const CvArr*  src, CvArr*  dst, const CvMat* map_matrix )
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 – 2\times 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 ( \texttt{dst}\rightarrow\texttt{src} ).
  • 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:

\texttt{dst} (x,y) =  \texttt{src} ( \texttt{M} _{11} x +  \texttt{M} _{12} y +  \texttt{M} _{13}, \texttt{M} _{21} x +  \texttt{M} _{22} y +  \texttt{M} _{23})



getRotationMatrix2D

Calculates an affine matrix of 2D rotation.

C++:  Mat  getRotationMatrix2D (Point2f  center, double  angle, double  scale )
Python:   cv2. getRotationMatrix2D (center, angle, scale ) → retval
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:

\begin{bmatrix} \alpha &  \beta & (1- \alpha )  \cdot \texttt{center.x} -  \beta \cdot \texttt{center.y} \\ - \beta &  \alpha &  \beta \cdot \texttt{center.x} + (1- \alpha )  \cdot \texttt{center.y} \end{bmatrix}

where

\begin{array}{l} \alpha =  \texttt{scale} \cdot \cos \texttt{angle} , \\ \beta =  \texttt{scale} \cdot \sin \texttt{angle} \end{array}

The transformation maps the rotation center to itself. If this is not the target, adjust the shift.


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

一个对向量 \vec{x}  平移 \vec{b} ,与旋转放大缩小 A 的仿射映射为

\vec{y} = A \vec{x} + \vec{b}.

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

\begin{bmatrix} \vec{y} \\ 1 \end{bmatrix} = \begin{bmatrix} A & \vec{b} \ \\ 0, \ldots, 0 & 1 \end{bmatrix} \begin{bmatrix} \vec{x} \\ 1 \end{bmatrix}

为了表示仿射变换,需要使用齐次坐标,即用三维向量 (xy, 1) 表示二维向量,对于高维来说也是如此。按照这种方法,就可以用矩阵乘法表示变换。 x' = x + t_xy' = y + t_y 变为

\begin{pmatrix} x' \\ y' \\ 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}

在矩阵中增加一列与一行,除右下角的元素为 1 外其它部分填充为 0,通过这种方法,所有的线性变换都可以转换为仿射变换。例如,上面的旋转矩阵变为

\begin{pmatrix} \cos \theta & - \sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{pmatrix}

通过这种方法,使用与前面一样的矩阵乘积可以将各种变换无缝地集成到一起


# -*- 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值