|
1 |
| -# More: http://ex2tron.wang |
| 1 | +# ex2tron's blog: |
| 2 | +# http://ex2tron.wang |
| 3 | + |
2 | 4 |
|
3 | 5 | import cv2
|
4 |
| -import numpy as np |
5 |
| -import matplotlib.pyplot as plt |
6 | 6 |
|
7 | 7 | img = cv2.imread('drawing.jpg')
|
8 | 8 |
|
|
15 | 15 | cv2.waitKey(0)
|
16 | 16 |
|
17 | 17 |
|
18 |
| -# 2.平移图片 |
| 18 | +# 2.翻转图片 |
| 19 | +import numpy as np |
| 20 | + |
| 21 | +# 参数2=0:垂直翻转(沿x轴),参数2>0: 水平翻转(沿y轴) |
| 22 | +# 参数2<0: 水平垂直翻转 |
| 23 | +dst = cv2.flip(img, -1) |
| 24 | +# np.hstack: 横向并排,对比显示 |
| 25 | +cv2.imshow('flip', np.hstack((img, dst))) # np.hstack: 横向并排,对比显示 |
| 26 | +cv2.waitKey(0) |
| 27 | + |
| 28 | + |
| 29 | +# 3.平移图片 |
19 | 30 | rows, cols = img.shape[:2]
|
20 | 31 | # 定义平移矩阵,需要是numpy的float32类型
|
21 | 32 | # x轴平移100,y轴平移50
|
|
26 | 37 | cv2.waitKey(0)
|
27 | 38 |
|
28 | 39 |
|
29 |
| -# 3.45°旋转图片并缩小一半 |
30 |
| -M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 0.5) |
| 40 | +# 4.45°顺时针旋转图片并缩小一半 |
| 41 | +M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -45, 0.5) |
31 | 42 | dst = cv2.warpAffine(img, M, (cols, rows))
|
32 | 43 |
|
33 | 44 | cv2.imshow('rotation', dst)
|
34 | 45 | cv2.waitKey(0)
|
35 |
| - |
36 |
| - |
37 |
| -# 4.翻转图片 |
38 |
| -# 参数2=0:垂直翻转(沿x轴),参数2>0: 水平翻转(沿y轴) |
39 |
| -# 参数2<0: 水平垂直翻转 |
40 |
| -dst = cv2.flip(img, 1) |
41 |
| -# np.hstack: 横向并排,对比显示 |
42 |
| -cv2.imshow('flip', np.hstack((img, dst))) # np.hstack: 横向并排,对比显示 |
43 |
| -cv2.waitKey(0) |
44 |
| - |
45 |
| - |
46 |
| -# 5.仿射变换 |
47 |
| -# 变换前的三个点 |
48 |
| -pts1 = np.float32([[50, 65], [150, 65], [210, 210]]) |
49 |
| -# 变换后的三个点 |
50 |
| -pts2 = np.float32([[50, 100], [150, 65], [100, 250]]) |
51 |
| - |
52 |
| -# 生成变换矩阵,维数:2*3 |
53 |
| -M = cv2.getAffineTransform(pts1, pts2) |
54 |
| -dst = cv2.warpAffine(img, M, (cols, rows)) |
55 |
| - |
56 |
| -plt.subplot(121), plt.imshow(img), plt.title('input') |
57 |
| -plt.subplot(122), plt.imshow(dst), plt.title('output') |
58 |
| -plt.show() |
0 commit comments