zoukankan      html  css  js  c++  java
  • 数学之路-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
    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	imes 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 ( 	exttt{dst}
ightarrow	exttt{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:

    	exttt{dst} (x,y) =  	exttt{src} ( 	exttt{M} _{11} x +  	exttt{M} _{12} y +  	exttt{M} _{13}, 	exttt{M} _{21} x +  	exttt{M} _{22} y +  	exttt{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:

    egin{bmatrix} alpha &  eta & (1- alpha )  cdot 	exttt{center.x} -  eta cdot 	exttt{center.y} \ - eta &  alpha &  eta cdot 	exttt{center.x} + (1- alpha )  cdot 	exttt{center.y} end{bmatrix}

    where

    egin{array}{l} alpha =  	exttt{scale} cdot cos 	exttt{angle} , \ eta =  	exttt{scale} cdot sin 	exttt{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}.

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

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

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

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

    在矩阵中添加一列与一行,除右下角的元素为 1 外其他部分填充为 0,通过这样的方法,全部的线性变换都能够转换为仿射变换。比如,上面的旋转矩阵变为

    egin{pmatrix} cos 	heta & - sin 	heta & 0 \ sin 	heta & cos 	heta & 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/

  • 相关阅读:
    Qt串口的使用记录
    CImage灰度化
    (记录)MSYS2+MINGW32编译ffmpeg过程
    Blas 基本函数功能
    blas中dgemm_的用法
    选择、插入、冒泡排序
    简单的页面
    中国大学排名
    爬bing 主页
    读书报告
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5369887.html
Copyright © 2011-2022 走看看