python opencv 进行简单几何变换

16-python opencv 进行简单几何变换


概述

本节实现的是使用OpenCV里自带的函数,对图像进行简单的几何变换。

  • 放大
  • 缩小
  • 平移
  • 旋转

实现过程

读取原图并显示

不再赘述。

import cv2
import numpy as np

# read the original
img = cv2.imread('../test2.jpg')
cv2.imshow('original', img)

放大

利用OpenCV自带的resize()函数实现放大与缩小。其声明为:

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst

其中各个参数的意义如下:

  • src – input image.
  • dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.
  • dsize –output image size; if it equals zero, it is computed as:
    dsize = Size(round(fx × src.cols), round(fy × src.rows))
  • fx –scale factor along the horizontal axis; when it equals 0, it is computed as
  • fy –scale factor along the vertical axis; when it equals 0, it is computed as
  • interpolation –interpolation method:
参数意义
INTER_NEARESTa nearest-neighbor interpolation
INTER_LINEARa bilinear interpolation (used by default)
INTER_AREAresampling 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 the INTER_NEAREST method.
INTER_CUBICa bicubic interpolation over 4x4 pixel neighborhood
INTER_LANCZOS4a Lanczos interpolation over 8x8 pixel neighborhood

本文将原图放大至原来的2倍。

# expand
rows, cols, channels = img.shape
img_ex = cv2.resize(img, (2*cols, 2*rows), interpolation=cv2.INTER_CUBIC)
cv2.imshow('expand', img_ex)

缩小

这里将原图缩小为原来的一半。

# zoom
img_zo = cv2.resize(img, (cols/2, rows/2), interpolation=cv2.INTER_AREA)
cv2.imshow('zoom', img_zo)

平移

平移可以由平移矩阵描述:

[1001txty](4) (4) [ 1 0 t x 0 1 t y ]

其中

$tx $ t x
ty
$分别为向右和向下平移的距离。这里我们利用np.array()创建这个矩阵,然后调用warpAffine来实现这个变换,并保持图像的大小不变。

# trans
M = np.array([[1, 0, 50],[0, 1, 50]], np.float32)
img_tr =cv2.warpAffine(img, M, img.shape[:2])
cv2.imshow('trans', img_tr)

其中warpAffine()的声明如下:

cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → dst

各个参数的意义如下:

  • src – input image.
  • dst – output image that has the size dsize and the same type as src .
  • M – 2 × 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.
  • borderMode – pixel extrapolation method (see borderInterpolate()); 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.

旋转

利用getRotationMatrix2D()获得旋转矩阵,其声明为

cv2.getRotationMatrix2D(center, angle, scale) → retval

各个参数的意义:

  • 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.
  • retval – The output affine transformation, 2x3 floating-point matrix.

然后再利用warpAffine()函数进行变换。

# Rotation
M=cv2.getRotationMatrix2D((cols/2,rows/2), 45, 1)
img_ro =cv2.warpAffine(img, M, img.shape[:2])
cv2.imshow('rotation', img_ro)

源代码

程序的源代码如下:

# created by Huang Lu
# 2016/8/26 17:35
# Department of EE, Tsinghua Univ.

import cv2
import numpy as np

# read the original
img = cv2.imread('../test2.jpg')
cv2.imshow('original', img)

# expand
rows, cols, channels = img.shape
img_ex = cv2.resize(img, (2*cols, 2*rows), interpolation=cv2.INTER_CUBIC)
cv2.imshow('expand', img_ex)

# zoom
img_zo = cv2.resize(img, (cols/2, rows/2), interpolation=cv2.INTER_AREA)
cv2.imshow('zoom', img_zo)

# trans
M = np.array([[1, 0, 50],[0, 1, 50]], np.float32)
img_tr =cv2.warpAffine(img, M, img.shape[:2])
cv2.imshow('trans', img_tr)

# Rotation
M=cv2.getRotationMatrix2D((cols/2,rows/2), 45, 1)
img_ro =cv2.warpAffine(img, M, img.shape[:2])
cv2.imshow('rotation', img_ro)

# wait the key and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()

也可以参考我的GitHub上的,点击这里

运行结果

在命令行进入该源程序所在目录后,运行python main.py后即可显示结果。显示结果如下:

结果

参考

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
1. 缩放 缩放指的是将原始图像按比例缩小或放大。OpenCV中提供了resize()函数来实现缩放操作。 语法: dst = cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) 参数说明: - src:原始图像。 - dsize:输出图像大小。 - fx:水平方向缩放比例。 - fy:垂直方向缩放比例。 - interpolation:插值方法。常用的有cv2.INTER_LINEAR(双线性插值)和cv2.INTER_NEAREST(最近邻插值)。 示例代码: import cv2 img = cv2.imread('lena.png') # 缩小图像 dst = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR) cv2.imshow('dst', dst) cv2.waitKey(0) cv2.destroyAllWindows() 2. 平移 平移指的是将原始图像沿着水平或垂直方向移动一定的距离。OpenCV中提供了warpAffine()函数来实现平移操作。 语法: dst = cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) 参数说明: - src:原始图像。 - M:变换矩阵,可以通过cv2.getAffineTransform()或cv2.getPerspectiveTransform()函数获取。 - dsize:输出图像大小。 - flags:插值方法和变换标志。常用的有cv2.INTER_LINEAR和cv2.WARP_FILL_OUTLIERS。 - borderMode:边界填充方法。常用的有cv2.BORDER_CONSTANT和cv2.BORDER_REPLICATE。 - borderValue:边界填充颜色。 示例代码: import cv2 import numpy as np img = cv2.imread('lena.png') rows, cols = img.shape[:2] # 定义平移矩阵 M = np.float32([[1, 0, 100], [0, 1, 50]]) # 平移图像 dst = cv2.warpAffine(img, M, (cols, rows)) cv2.imshow('dst', dst) cv2.waitKey(0) cv2.destroyAllWindows() 3. 翻转 翻转指的是将原始图像沿着水平或垂直方向翻转。OpenCV中提供了flip()函数来实现翻转操作。 语法: dst = cv2.flip(src, flipCode[, dst]) 参数说明: - src:原始图像。 - flipCode:翻转方式。0表示沿x轴翻转(水平翻转),1表示沿y轴翻转(垂直翻转),-1表示沿x轴和y轴同时翻转(水平垂直翻转)。 示例代码: import cv2 img = cv2.imread('lena.png') # 水平翻转 dst1 = cv2.flip(img, 0) # 垂直翻转 dst2 = cv2.flip(img, 1) # 水平垂直翻转 dst3 = cv2.flip(img, -1) cv2.imshow('dst1', dst1) cv2.imshow('dst2', dst2) cv2.imshow('dst3', dst3) cv2.waitKey(0) cv2.destroyAllWindows()

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值