码迷,mamicode.com
首页 > 编程语言 > 详细

数学之路-python计算实战(9)-机器视觉-图像插值仿射

时间:2016-04-08 21:40:56      阅读:1655      评论:0      收藏:0      [点我收藏+]

标签:

  • 插值
  • 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 – 技术分享 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
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)-机器视觉-图像插值仿射

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/5369887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!