Skip to content

Commit 0685223

Browse files
committed
添加番外篇5,更改结构 2018年11月22日
1 parent 042a1ec commit 0685223

File tree

38 files changed

+77
-55
lines changed

38 files changed

+77
-55
lines changed

04. 图像基本操作/cv2_basic_operations.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45

56
img = cv2.imread('lena.jpg')
67

78
# 1.获取像素的值
8-
px = img[100, 100]
9-
print(px) # [119 108 201]
9+
px = img[100, 90]
10+
print(px) # [103 98 197]
1011

1112
# 只获取蓝色blue通道的值
12-
px_blue = img[100, 100, 0]
13-
print(px_blue) # 119
13+
px_blue = img[100, 90, 0]
14+
print(px_blue) # 103
1415

1516

1617
# 2.修改像素的值
17-
img[100, 100] = [255, 255, 255]
18-
print(img[100, 100]) # [255 255 255]
18+
img[100, 90] = [255, 255, 255]
19+
print(img[100, 90]) # [255 255 255]
1920

2021

2122
# 3.图片形状
22-
print(img.shape) # (263, 263, 3)
23-
# 形状中包括高度、宽度和通道数
23+
print(img.shape) # (263, 247, 3)
24+
# 形状中包括行数、列数和通道数
2425
height, width, channels = img.shape
2526
# img是灰度图的话:height, width = img.shape
2627

2728
# 总像素数
28-
print(img.size) # 263*263*3=207507
29+
print(img.size) # 263*247*3=194883
2930
# 数据类型
3031
print(img.dtype) # uint8
3132

04. 图像基本操作/cv2_exercise1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45

04. 图像基本操作/lena.jpg

-1.45 KB
Loading

05. 颜色空间转换/cv2_changing_color_space.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
4-
import numpy as np
55

66
img = cv2.imread('lena.jpg')
77

@@ -18,6 +18,8 @@
1818
print(flags)
1919

2020
# 蓝色的HSV值
21+
import numpy as np
22+
2123
blue = np.uint8([[[255, 0, 0]]])
2224
hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
2325
print(hsv_blue) # [[[120 255 255]]]

05. 颜色空间转换/cv2_exercises1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import numpy as np

06. 阈值分割/cv2_thresholding.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import matplotlib.pyplot as plt
Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
3+
24

35
import cv2
4-
import numpy as np
5-
import matplotlib.pyplot as plt
66

77
img = cv2.imread('drawing.jpg')
88

@@ -15,7 +15,18 @@
1515
cv2.waitKey(0)
1616

1717

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.平移图片
1930
rows, cols = img.shape[:2]
2031
# 定义平移矩阵,需要是numpy的float32类型
2132
# x轴平移100,y轴平移50
@@ -26,33 +37,9 @@
2637
cv2.waitKey(0)
2738

2839

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)
3142
dst = cv2.warpAffine(img, M, (cols, rows))
3243

3344
cv2.imshow('rotation', dst)
3445
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()

08. 绘图功能/cv2_drawing_functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import numpy as np

08. 绘图功能/cv2_exercise1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import numpy as np

09. 图像混合/cv2_image_blending.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import numpy as np

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# OpenCV-Python图像处理教程
1+
## OpenCV-Python图像处理教程(源码及素材)
22

33
> 教程地址:[ex2tron's Blog](http://ex2tron.wang)

番外篇01. 代码性能优化/cv2_optimization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import numpy as np

番外篇03. 滑动条/cv2_trackbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
import numpy as np

番外篇04. Otsu阈值法/cv2_otsu_thresholding.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
23

34
import cv2
45
from matplotlib import pyplot as plt

07. 图像几何变换/cv2_perspective_transformation.py renamed to 番外篇05. 仿射变换和透视变换原理/cv2_affine_perspective_transformation.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1-
# More: http://ex2tron.wang
1+
# ex2tron's blog:
2+
# http://ex2tron.wang
3+
24

35
import cv2
46
import numpy as np
57
import matplotlib.pyplot as plt
68

7-
img = cv2.imread('card.jpg')
9+
img = cv2.imread('drawing.jpg')
810
rows, cols = img.shape[:2]
911

12+
13+
# 1.仿射变换
14+
15+
# 变换前的三个点
16+
pts1 = np.float32([[50, 65], [150, 65], [210, 210]])
17+
# 变换后的三个点
18+
pts2 = np.float32([[50, 100], [150, 65], [100, 250]])
19+
20+
# 生成变换矩阵,维数:2*3
21+
M = cv2.getAffineTransform(pts1, pts2)
22+
dst = cv2.warpAffine(img, M, (cols, rows))
23+
24+
plt.subplot(121), plt.imshow(img), plt.title('input')
25+
plt.subplot(122), plt.imshow(dst), plt.title('output')
26+
plt.show()
27+
28+
29+
# 2.透视变换
30+
31+
img = cv2.imread('card.jpg')
32+
1033
# 原图中卡片的四个角点
1134
pts1 = np.float32([[148, 80], [437, 114], [94, 247], [423, 288]])
1235
# 变换后分别在左上、右上、左下、右下四个点
1336
pts2 = np.float32([[0, 0], [320, 0], [0, 178], [320, 178]])
1437

1538
# 生成透视变换矩阵
1639
M = cv2.getPerspectiveTransform(pts1, pts2)
17-
# 进行透视变换
40+
# 进行透视变换,参数3是目标图像大小
1841
dst = cv2.warpPerspective(img, M, (320, 178))
1942

2043
# matplotlib默认以RGB通道显示,所以需要用[:, :, ::-1]翻转一下
Loading

0 commit comments

Comments
 (0)