数字图像处理(9): 图像缩放、图像旋转、图像翻转 和 图像平移

目录

1 图像缩放- resize()

2 图像旋转- getRotationMatrix2D(), warpAffine()

3 图像翻转- flip()

4 图像平移- warpAffine()

参考资料


图像几何变换有图像缩放、图像旋转、图像翻转和图像平移等。

 

1 图像缩放- resize()

图像缩放主要调用 resize() 函数实现,具体如下:

result = cv2.resize(src, dsize[, result[. fx[, fy[, interpolation]]]])

其中,参数

src 表示原始图像;

dsize 表示缩放大小;

fx和fy 也可以表示缩放大小倍数,他们两个(dsize或fx/fy)设置一个即可实现图像缩放。例如:

(1)result = cv2.resize(src, (160,160))

(2)result = cv2.resize(src, None, fx=0.5, fy=0.5)

图像缩放:设({x_0},{y_0})是缩放后的坐标,(x,y)是缩放前的坐标,{s_x} 和 {s_y} 为缩放因子,则公式如下:

                                                                       [{x_0}\text{ }{\text{ }}{y_0}\text{ }{\text{ }}1] = [x\text{ }{\text{ }}y\text{ }{\text{ }}1]{\text{ }}\left[ \begin{gathered} {s_x}\text{ }{\text{ }}0\text{ }\text{ }{\text{ 0}} \hfill \\ 0{\text{ }}\text{ }\text{ }{s_y}\text{ }{\text{ 0}} \hfill \\ {\text{0 }}\text{ }\text{ }0\text{ }\text{ }{\text{ 1}} \hfill \\ \end{gathered} \right]

 

(1) cv2.resize(src, (200,100)) 设置的dsize是列数为200,行数为100

result = cv2.resize(src, (200,100))

代码如下:

# encoding:utf-8
import cv2
import numpy as np

# 读取图片
src = cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)

# 图像缩放
result = cv2.resize(src, (200,100))
print (result.shape)

# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", result)

# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:

 

(2)可以获取 原始图像像素\times乘以缩放系数 进行图像变换;

result = cv2.resize(src, (int(cols * 0.6), int(rows * 1.2)))

 

代码如下所示:

# encoding:utf-8
import cv2
import numpy as np

# 读取图片
src = cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)
rows, cols = src.shape[:2]
print
rows, cols

# 图像缩放 dsize(列,行)
result = cv2.resize(src, (int(cols * 0.6), int(rows * 1.2)))

# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", result)

# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:

 

(3)(fx,fy) 缩放倍数的方法对图像进行放大或缩小。

result = cv2.resize(src, None, fx=0.3, fy=0.3)

 

代码如下所示:

# encoding:utf-8
import cv2
import numpy as np

# 读取图片
src =cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)
rows, cols = src.shape[:2]
print
rows, cols

# 图像缩放
result = cv2.resize(src, None, fx=0.3, fy=0.3)

# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", result)

# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:(按例比 0.3 \times 0.3 缩小)


 

2 图像旋转- getRotationMatrix2D(), warpAffine()

图像旋转:设 ({x_0},{y_0}) 是旋转后的坐标,({x},{y}) 是旋转前的坐标,({ m},{n}) 是旋转中心,a 是旋转的角度,({ left},{top}) 是旋转后图像的左上角坐标,则公式如下:

                                     [{x_0}\text{ }{\text{ }}{y_0}\text{ }{\text{ }}1] = [x\text{ }{\text{ }}y\text{ }{\text{ }}1]{\text{ }}\left[ \begin{gathered} \text{ }\text{ } 1\text{ }\text{ }\text{ }{\text{ }}0\text{ }\text{ }{\text{ 0}} \hfill \\ \text{ }\text{ }0{\text{ }} - 1\text{ }{\text{ 0}} \hfill \\ {\text{ - }}m\text{ }{\text{ }}n\text{ }{\text{ 1}} \hfill \\ \end{gathered} \right]{\text{ }}\left[ \begin{gathered} \cos a{\text{ }} - \sin a{\text{ }}0 \hfill \\ \sin a\text{ }\text{ }{\text{ }}\cos a\text{ }{\text{ }}0 \hfill \\ \text{ }\text{ }{\text{ }}0{\text{ }}\text{ }\text{ }\text{ }\text{ }\text{ }\text{ }0\text{ }\text{ }\text{ }{\text{ }}1 \hfill \\ \end{gathered} \right]{\text{ }}\left[ \begin{gathered} \text{ }\text{ }1\text{ }\text{ }\text{ }\text{ }{\text{ }}0\text{ }\text{ }{\text{ }}0 \hfill \\ \text{ }\text{ }0{\text{ }} \text{ }- 1\text{ }{\text{ }}0 \hfill \\ left\text{ }{\text{ }}top{\text{ }}\text{ }1 \hfill \\ \end{gathered} \right]

 

图像旋转主要调用getRotationMatrix2D() 函数和 warpAffine() 函数实现,绕图像的中心旋转,具体如下:

M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)

其中,参数分别为:旋转中心、旋转度数、scale

rotated = cv2.warpAffine(src, M, (cols, rows))

其中,参数分别为:原始图像、旋转参数 和 原始图像宽高

 

(1)旋转30度

代码如下:

# encoding:utf-8
import cv2
import numpy as np

# 读取图片
src =  cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)

# 原图的高、宽 以及通道数
rows, cols, channel = src.shape

# 绕图像的中心旋转
# 参数:旋转中心 旋转度数 scale
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(src, M, (cols, rows))

# 显示图像
cv2.imshow("src", src)
cv2.imshow("rotated", rotated)

 

运行结果如下图所示:

 

(2)旋转90度

代码如下:

# encoding:utf-8
import cv2
import numpy as np

# 读取图片
src =  cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)

# 原图的高、宽 以及通道数
rows, cols, channel = src.shape

# 绕图像的中心旋转
# 参数:旋转中心 旋转度数 scale
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(src, M, (cols, rows))

# 显示图像
cv2.imshow("src", src)
cv2.imshow("rotated", rotated)

 

运行结果如下图所示:

 

(3)旋转180度

代码如下:

# encoding:utf-8
import cv2
import numpy as np

# 读取图片
src =  cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)

# 原图的高、宽 以及通道数
rows, cols, channel = src.shape

# 绕图像的中心旋转
# 参数:旋转中心 旋转度数 scale
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 180, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(src, M, (cols, rows))

# 显示图像
cv2.imshow("src", src)
cv2.imshow("rotated", rotated)

 

运行结果如下图所示:


 

3 图像翻转- flip()

图像翻转在OpenCV中调用函数 flip() 实现,函数用法如下:

dst = cv2.flip(src, flipCode)

其中,参数:

src 表示原始图像;

flipCode 表示翻转方向,如果flipCode为0,则以X轴为对称轴翻转,如果fliipCode>0则以Y轴为对称轴翻转,如果flipCode<0则在X轴、Y轴方向同时翻转。

 

代码如下:(注意一个窗口多张图像的用法

# encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图片
img = cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)
src = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 图像翻转
# 0以X轴为对称轴翻转 >0以Y轴为对称轴翻转 <0X轴Y轴翻转
img1 = cv2.flip(src, 0)
img2 = cv2.flip(src, 1)
img3 = cv2.flip(src, -1)

# 显示图形 (注意一个窗口多张图像的用法)
titles = ['Source', 'Image1', 'Image2', 'Image3']
images = [src, img1, img2, img3]
for i in range(4):
    plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:(注意一个窗口多张图像的用法


 

4 图像平移- warpAffine()

图像平移:设 ({x_0},{y_0}) 是缩放后的坐标,({x},{y}) 是缩放前的坐标,{d_x},{d_y} 为偏移量,则公式如下:

                                                             [{x_0}\text{ }{\text{ }}{y_0}\text{ }{\text{ }}1] = [x\text{ }{\text{ }}\text{ }y{\text{ }}\text{ }1]{\text{ }}\left[ \begin{gathered} \text{ }1{\text{ }}\text{ }\text{ }0\text{ }\text{ }{\text{ }}0 \hfill \\ \text{ }0\text{ }\text{ }{\text{ }}1\text{ }\text{ }{\text{ }}0 \hfill \\ {d_x}{\text{ }}\text{ }{d_y}{\text{ }}\text{ }1 \hfill \\ \end{gathered} \right]

 

图像平移首先定义平移矩阵M,再调用 warpAffine() 函数实现平移,函数用法如下:

M = np.float32([[1, 0, x], [0, 1, y]])

shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

 

代码如下:

# encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图片
img = cv2.imread("lena.tiff", cv2.IMREAD_UNCHANGED)
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 图像平移 下、上、右、左平移
M = np.float32([[1, 0, 0], [0, 1, 100]])
img1 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

M = np.float32([[1, 0, 0], [0, 1, -100]])
img2 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

M = np.float32([[1, 0, 100], [0, 1, 0]])
img3 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

M = np.float32([[1, 0, -100], [0, 1, 0]])
img4 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

# 显示图形
titles = ['Image1', 'Image2', 'Image3', 'Image4']
images = [img1, img2, img3, img4]
for i in range(4):
    plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

 

运行结果如下图所示:


 

参考资料

[1] https://blog.csdn.net/Eastmount/article/details/82454335

[2] Python+OpenCV图像处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechArtisan6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值