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

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

## 实现过程

### 读取原图并显示

import cv2
import numpy as np

cv2.imshow('original', img)

### 放大

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_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 the INTER_NEAREST method.
INTER_CUBIC a bicubic interpolation over 4x4 pixel neighborhood
INTER_LANCZOS4 a Lanczos interpolation over 8x8 pixel neighborhood

# 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)

$tx $和$ 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)

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.

### 旋转

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.

# 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

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()

• 本文已收录于以下专栏：

## opencv: 图像缩放(cv2.resize)

API cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst interpolation 选项 所用的插值方法 ...
• JNingWei
• 2017年10月12日 19:43
• 2670

## python opencv 图像尺寸变换

• u012192662
• 2013年12月13日 15:26
• 45446

## opencv resize （C/C++/Python）

• u012005313
• 2016年07月18日 20:04
• 15322

## opencv中用Resize和图像金字塔改变尺寸的区别

resize和图像金字塔(pydown、pyup)都可以改变图像的大小。具体操作和区别如下： resize C++: void resize(InputArray src, OutputA...
• maryhuan
• 2013年12月04日 16:36
• 4741

## 【opencv+python】图像处理之二、几何变换(仿射与投影)的应用

• a352611
• 2016年05月15日 20:34
• 8045

## caffe训练模型：(二)python+cv2 图像色彩变换

(1)将RGB图像转为HSV, LAB, GRAY, 顺变将不是224大小的图片resize一下 # -*- coding: utf-8 -*- import os import cv2 N = 0 ...
• summermaoz
• 2017年02月24日 18:22
• 2569

## resize()函数

• vicdd
• 2016年10月19日 11:06
• 660

## OpenCV中cvResize函数图象放缩

• Augusdi
• 2013年06月03日 10:54
• 35546

## Opencv3.0-python的那些事儿：（三）、Opencv的图像处理中的几何变换

# *-* coding: utf-8 *-* import cv2 import numpy as np from matplotlib import pyplot as plt ''' 第14...
• qingyuanluofeng
• 2016年06月03日 22:31
• 3914

## OpenCV：resize（）重新调整图像大小

OpenCV中，resize函数用的比较多。C++: void resize(InputArray src, //输入 OutputArray dst,//输出 ...
• sss_369
• 2016年10月31日 15:08
• 1177

举报原因： 您举报文章：python opencv 进行简单几何变换 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)