最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

python的cv2.warpAffine()和cv2.warpPerspective()解析对比

IT圈 admin 5浏览 0评论

python的cv2.warpAffine()和cv2.warpPerspective()解析对比

1、cv2.warpAffine()放射变换函数,可实现旋转,平移,缩放;变换后的平行线依旧平行

cv2.warpAffine(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None) --> dst

src:输入图像     dst:输出图像

M:2×3的变换矩阵

dsize:变换后输出图像尺寸

flag:插值方法

borderMode:边界像素外扩方式

borderValue:边界像素插值,默认用0填充

变换矩阵M可通过cv2.getAffineTransfrom(points1, points2)函数获得

变换矩阵的获取需要至少三组变换前后对应的点坐标,设取原图上的三个点组成矩阵points1,变换后的三个点组成的矩阵points2

如:

points1 = np.float32([ [30,30], [100,40], [40,100] ])
points2 = np.float32([ [60,60], [40,100], [80,20] ])M  =  cv2.getAffineTransform(points1, points2)=  array([[-0.33333333,  0.33333333, 60.   ],[ 0.66666667, -0.66666667, 60.   ]])Affine_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0])) 

  Affine_img是相对于原图img是以M变换后的图像,对于原图img上任何一点的坐标通过变换矩阵M变换后可在Affine_img上都能找到与之对应的坐标点,变换方法为:

先构造一个3×1的矩阵,则

2、cv2.warpPerspective()透视变换函数,可保持直线不变形,但是平行线可能不再平行

cv2.warpPerspective(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None) --> dst

其相关参数和cv2.warpAffine函数的类似,不再做介绍

它的变换矩阵可以通过cv2.getPerspectiveTransform()函数获得,其原理和cv2.getAffineTransfrom()相同,只是投射变换至少需要四组变换前后对应的点坐标,设取原图上的四个点组成矩阵points1,变换后的四个点组成的矩阵points2

如:

points1 = np.float32([ [30,30], [10,40], [40,10], [5,15] ])
points2 = np.float32([ [0,0], [400,0], [0,400], [400,400] ])M = cv2.getPerspectiveTransform(points1, points2)= array([[-9.08777969e+00, -4.54388985e+00,  4.08950086e+02],[-5.37005164e+00, -1.07401033e+01,  4.83304647e+02],[-1.15318417e-02, -1.35972461e-02,  1.00000000e+00]])Perspective_img = cv2.warpPerspective(img, M, (img.shape[1], img.shape[0])) 

Perspective_img是相对于原图img是以M变换后的图像,同理,对于原图img上任何一点的坐标通过变换矩阵M变换后可在Perspective_img上都能找到与之对应的坐标点,变换方法为:

先构造一个3×1的矩阵,则

 

另:旋转缩放变换矩阵M可通过cv2.getRotationMatrix2D(center, angle, scale)函数获得,再通过cv2.warpAffine函数对图像进行旋转缩放变换

center:旋转中心坐标;  angle:旋转角度;  scale:缩放尺度

python的cv2.warpAffine()和cv2.warpPerspective()解析对比

1、cv2.warpAffine()放射变换函数,可实现旋转,平移,缩放;变换后的平行线依旧平行

cv2.warpAffine(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None) --> dst

src:输入图像     dst:输出图像

M:2×3的变换矩阵

dsize:变换后输出图像尺寸

flag:插值方法

borderMode:边界像素外扩方式

borderValue:边界像素插值,默认用0填充

变换矩阵M可通过cv2.getAffineTransfrom(points1, points2)函数获得

变换矩阵的获取需要至少三组变换前后对应的点坐标,设取原图上的三个点组成矩阵points1,变换后的三个点组成的矩阵points2

如:

points1 = np.float32([ [30,30], [100,40], [40,100] ])
points2 = np.float32([ [60,60], [40,100], [80,20] ])M  =  cv2.getAffineTransform(points1, points2)=  array([[-0.33333333,  0.33333333, 60.   ],[ 0.66666667, -0.66666667, 60.   ]])Affine_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0])) 

  Affine_img是相对于原图img是以M变换后的图像,对于原图img上任何一点的坐标通过变换矩阵M变换后可在Affine_img上都能找到与之对应的坐标点,变换方法为:

先构造一个3×1的矩阵,则

2、cv2.warpPerspective()透视变换函数,可保持直线不变形,但是平行线可能不再平行

cv2.warpPerspective(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None) --> dst

其相关参数和cv2.warpAffine函数的类似,不再做介绍

它的变换矩阵可以通过cv2.getPerspectiveTransform()函数获得,其原理和cv2.getAffineTransfrom()相同,只是投射变换至少需要四组变换前后对应的点坐标,设取原图上的四个点组成矩阵points1,变换后的四个点组成的矩阵points2

如:

points1 = np.float32([ [30,30], [10,40], [40,10], [5,15] ])
points2 = np.float32([ [0,0], [400,0], [0,400], [400,400] ])M = cv2.getPerspectiveTransform(points1, points2)= array([[-9.08777969e+00, -4.54388985e+00,  4.08950086e+02],[-5.37005164e+00, -1.07401033e+01,  4.83304647e+02],[-1.15318417e-02, -1.35972461e-02,  1.00000000e+00]])Perspective_img = cv2.warpPerspective(img, M, (img.shape[1], img.shape[0])) 

Perspective_img是相对于原图img是以M变换后的图像,同理,对于原图img上任何一点的坐标通过变换矩阵M变换后可在Perspective_img上都能找到与之对应的坐标点,变换方法为:

先构造一个3×1的矩阵,则

 

另:旋转缩放变换矩阵M可通过cv2.getRotationMatrix2D(center, angle, scale)函数获得,再通过cv2.warpAffine函数对图像进行旋转缩放变换

center:旋转中心坐标;  angle:旋转角度;  scale:缩放尺度

发布评论

评论列表 (0)

  1. 暂无评论