├── README.md ├── chapter03-based-opencv ├── Lena.png ├── chapter03_01.py ├── chapter03_02.py ├── chapter03_03.py ├── chapter03_04.py ├── chapter03_05.py ├── chapter03_06.py ├── chapter03_07.py ├── chapter03_08.py ├── chapter03_09.py ├── chapter03_10.py ├── chapter03_11.py ├── chapter03_12.py ├── chapter03_13.py ├── chapter03_14.py ├── chapter03_15.py ├── flower.png ├── lena-hd.png ├── people.png └── scenery.png ├── chapter04-logical-operation ├── Lena.png ├── chapter04_01.py ├── chapter04_02.py ├── chapter04_03.py ├── chapter04_04.py ├── chapter04_05.py ├── chapter04_06.py ├── chapter04_07.py ├── chapter04_08.py ├── chapter04_09.py ├── chapter04_10.py ├── chapter04_11.py ├── chapter04_12.py ├── chapter04_13.py ├── chapter04_14.py ├── chapter04_15.py ├── chapter04_16.py ├── chapter04_17.py ├── chapter04_18.py ├── chapter04_19.py ├── chapter04_20.py ├── chapter04_21.py ├── chapter04_22.py ├── chapter04_23.py ├── flower.png ├── lena-hd.png ├── na.png ├── scenery.png └── test.jpg ├── chapter05-geometric-trans ├── canny.jpg ├── chapter05-01-py.py ├── chapter05-02-py.py ├── chapter05-03-sf.py ├── chapter05-04-sf.py ├── chapter05-05-sf.py ├── chapter05-06-xz.py ├── chapter05-07-xz.py ├── chapter05-08-jx.py ├── chapter05-09-fs.py ├── chapter05-10-ts.py ├── chapter05-11-ts.py ├── chapter05-12-all.py ├── test.bmp └── test01.jpg ├── chapter06-lh&cy ├── chapter06_01.py ├── chapter06_02.py ├── chapter06_03.py ├── chapter06_04.py ├── chapter06_05.py ├── chapter06_06.py ├── chapter06_07.py ├── chapter06_08.py ├── chapter06_09.py ├── chapter06_10.py ├── figure_1.png ├── figure_2.png ├── lena.png ├── lena2.png ├── nv.png ├── people.png ├── sava.png └── scenery.png ├── chapter07-dys ├── 2016-.png ├── 2016.png ├── 2017.png ├── 2018.png ├── 2019.png ├── 2020.png ├── 2021.png ├── 2023.png ├── chapter07_01.py ├── chapter07_02.py ├── chapter07_03.py ├── chapter07_04.py ├── chapter07_05.py ├── chapter07_06.py ├── chapter07_07.py ├── chapter07_08.py ├── chapter07_09.py ├── chapter07_10.py ├── chapter07_11(best).py ├── chapter07_12(best).py ├── chapter07_13.py ├── chapter07_14.py ├── chapter07_15.py ├── chapter07_16.py ├── chapter07_17.py ├── chapter07_18.py ├── chapter07_19.py ├── figure_1.png ├── figure_13.png ├── figure_2.png ├── figure_4.png ├── figure_dd.png ├── figure_duishu.png ├── miao.jpg └── miao.png ├── chapter08-histogram ├── Figure_1.png ├── Figure_2.png ├── Figure_3.png ├── Figure_4.png ├── Lena.png ├── chapter08-01.py ├── chapter08-02.py ├── chapter08-03.py ├── chapter08-04.py ├── chapter08-05.py ├── chapter08-06.py ├── chapter08-07.py ├── chapter08-08.py ├── chapter08-09.py ├── chapter08-10.py ├── chapter08-11.py ├── chapter08-12.py ├── chapter08-13.py ├── chapter08-14.py ├── chapter08-15-3d.py ├── dark.png ├── day.png ├── lena.bmp ├── yxz.png ├── yxz_jhh.png └── yxz_sw.png ├── chapter09-ImageEnhancement ├── 2016-.png ├── Ice.jpg ├── chapter09-01.py ├── chapter09-02.py ├── chapter09-03.py ├── chapter09-04.py ├── chapter09-05.py ├── figure_1.png ├── lena.bmp ├── test.png ├── test01.png ├── test02.png ├── yxz.jpg ├── zmIce.jpg ├── zmIce1.jpg └── zmIce2.jpg ├── chapter10-smoth ├── chapter10-01-blur.py ├── chapter10-02-boxFilter.py ├── chapter10-03-boxFilter.py ├── chapter10-04-gauss.py ├── chapter10-05-median.py ├── chapter10-06-bilateral.py ├── chapter10-07-all.py ├── chapter10-08-all2.py ├── chapter10-09-noise.py ├── figure_gaosi.png ├── figure_shuangbian.png ├── figure_zhongzhi.png ├── lena-yt.png ├── lena.jpg ├── lena.png ├── lena2.png ├── picture.bmp ├── te.png └── testyxz.jpg ├── chapter11-ruihua ├── Lena.png ├── chapter11-01-roberts.py ├── chapter11-02-prewitt.py ├── chapter11-03-sobel.py ├── chapter11-04-laplacian.py ├── chapter11-05-laplacian.py ├── chapter11-06-scharr.py ├── chapter11-07-canny.py ├── chapter11-08-LOG.py ├── chapter11-09-all.py ├── figure_1.png ├── figure_2.png ├── figure_3.png ├── figure_4.png ├── figure_5.png ├── figure_6.png ├── figure_7.png ├── figure_8.png └── figure_all.png ├── chapter12-morphology ├── chapter12_01_erode.py ├── chapter12_02_dilate.py ├── chapter12_03_open.py ├── chapter12_04_open.py ├── chapter12_05_close.py ├── chapter12_06_gradient.py ├── chapter12_07_tophat.py ├── chapter12_08_3D.py ├── chapter12_09_blackhat.py ├── chapter12_all.py ├── figure_1.png ├── test01.jpg ├── test01.png ├── test02.png ├── test03.png ├── test04.png ├── test05.png └── test06.png ├── chapter13-Special-effects ├── chapter13_01.py ├── chapter13_02.py ├── chapter13_03.py ├── chapter13_04.py ├── chapter13_05_sumiao .py ├── chapter13_06_huaijiu.py ├── chapter13_07_guang.py ├── chapter13_08_liunian.py ├── chapter13_09_sb.py ├── chapter13_10_kt.py ├── chapter13_11_lj.py ├── chapter13_12_jh.py ├── chapter13_13_gs.py ├── dd.jpg ├── figure_1.png ├── figure_2.png ├── figure_3.png ├── nv-cartoon.png ├── nv.png ├── scenery.png ├── sketch_example.jpg ├── table.png └── table2.png ├── chapter14-ImageSegmentation ├── chapter14-01-yzfg.py ├── chapter14-02-byjc.py ├── chapter14-03-lk.py ├── chapter14-04-peoloe-get.py ├── chapter14-05-kmeans.py ├── chapter14-06-kmeans(best).py ├── chapter14-07-shift.py ├── chapter14-08-shift(best).py ├── chapter14-09-fsl.py ├── chapter14-10-water.py ├── chapter14-11-water-final.py ├── chapter14-12-mstc.py ├── chapter14-13-mstc(best).py ├── chapter14-14-word(best).py ├── figure_1.png ├── figure_11.png ├── figure_12.png ├── figure_13.png ├── figure_15.png ├── figure_16.png ├── figure_17.png ├── figure_2.png ├── figure_3.png ├── figure_4.png ├── figure_6.png ├── figure_9.png ├── hua.png ├── scenery.png ├── test.png ├── test01.png ├── test02.jpg └── word.png └── chapter15-fft&hough ├── Lena.bmp ├── Lena.png ├── Na.png ├── QQ截图20190423145246.png ├── chapter15_01.py ├── chapter15_02.py ├── chapter15_03.py ├── chapter15_04.py ├── chapter15_05.py ├── chapter15_06.py ├── chapter15_07.py ├── chapter15_08.py ├── chapter15_09.py ├── chapter15_10.py ├── chapter15_11.py ├── eyes.png ├── figure_1.png ├── figure_10.png ├── figure_11.png ├── figure_12.png ├── figure_13.png ├── figure_2.png ├── figure_3.png ├── figure_4.png ├── figure_5.png ├── figure_6.png ├── figure_7.png ├── figure_8.png ├── figure_9.png ├── figure_99.png ├── judge.png ├── lines.png ├── test.png ├── test01.png └── test02.png /README.md: -------------------------------------------------------------------------------- 1 | # Python-Book3-ImageProcessing 2 | 该资源为作者《Python中的图像处理》书籍所有源代码,已修改为Python3实现,希望对您有所帮助,一起加油。 3 | 4 | 5 | - **第一章 绪论**
6 | 1.1 数字图像处理
7 | 1.2 Python
8 | 1.3 OpenCV
9 | 1.4 章节安排
10 | - **第二章 Python基础**
11 | 2.1 Python简介
12 | 2.2 基础语法
13 | -- 2.2.1 输出语句
14 | -- 2.2.2 注释
15 | -- 2.2.3 变量及赋值
16 | -- 2.2.4 输入语句
17 | 2.3 数据类型
18 | 2.4 基本语句
19 | -- 2.4.1 条件语句
20 | -- 2.4.2 循环语句
21 | 2.5 基本操作
22 | 2.6 本章小结
23 | - **第三章 数字图像处理基础**
24 | 3.1 数字图像处理概述
25 | 3.2 像素及常见图像分类
26 | 3.3 图像信号数字化处理
27 | 3.4 OpenCV安装配置
28 | 3.5 OpenCV初识及常见数据类型
29 | -- 3.5.1 OpenCV显示图像
30 | -- 3.5.2 常见数据类型
31 | 3.6 Numpy和Matplotlib库介绍
32 | -- 3.6.1 Numpy库
33 | -- 3.6.2 Matplotlib库
34 | 3.7 几何图形绘制
35 | -- 3.7.1 绘制直线
36 | -- 3.7.2 绘制矩形
37 | -- 3.7.3 绘制圆形
38 | -- 3.7.4 绘制椭圆
39 | -- 3.7.5 绘制多边形
40 | -- 3.7.6 绘制文字
41 | 3.8 本章小结
42 | - **第四章 Python图像处理入门**
43 | 4.1 OpenCV读取显示图像
44 | 4.2 OpenCV读取修改像素
45 | 4.3 OpenCV创建复制保存图像
46 | 4.4 获取图像属性及通道
47 | -- 4.4.1 图像属性
48 | -- 4.4.2 图像通道处理
49 | 4.5 图像算数与逻辑运算
50 | -- 4.5.1 图像加法运算
51 | -- 4.5.2 图像减法运算
52 | -- 4.5.3 图像与运算
53 | -- 4.5.4 图像或运算
54 | -- 4.5.5 图像异或运算
55 | -- 4.5.6 图像非运算
56 | 4.6 图像融合处理
57 | 4.7 获取图像ROI区域
58 | 4.8 图像类型转换
59 | 4.9 本章小结
60 | - **第五章 Python图像几何变换**
61 | 5.1 图像几何变换概述
62 | 5.2 图像平移变换
63 | 5.3 图像缩放变换
64 | 5.4 图像旋转变换
65 | 5.5 图像镜像变换
66 | 5.6 图像仿射变换
67 | 5.7 图像透视变换
68 | 5.8 本章小结
69 | - **第六章 Python图像量化及采样处理**
70 | 6.1 图像量化处理 114
71 | -- 6.1.1 概述
72 | -- 6.1.2 操作
73 | -- 6.1.3 K-Means聚类量化处理
74 | 6.2 图像采样处理
75 | -- 6.2.1 概述
76 | -- 6.2.2 操作
77 | -- 6.2.3 局部马赛克处理
78 | 6.3 图像金字塔
79 | -- 6.3.1 图像向下取样
80 | -- 6.3.2 图像向上取样
81 | 6.4 本章小结
82 | - **第七章 Python图像的点运算处理**
83 | 7.1 图像点运算的概述
84 | 7.2 图像灰度化处理
85 | 7.3 图像的灰度线性变换
86 | 7.4 图像的灰度非线性变换
87 | 7.5 图像阈值化处理
88 | -- 7.5.1 固定阈值化处理
89 | -- 7.5.2 自适应阈值化处理
90 | 7.6 本章小结
91 | - **第八章 Python直方图统计**
92 | 8.1 图像直方图概述
93 | 8.2 Matplotlib绘制直方图
94 | 8.3 OpenCV绘制直方图
95 | 8.4 掩膜直方图
96 | 8.5 图像灰度变换直方图对比
97 | 8.6 图像H-S直方图
98 | 8.7 直方图判断黑夜白天
99 | 8.8 本章小结
100 | - **第九章 Python图像增强**
101 | 9.1 图像增强概述
102 | 9.2 直方图均衡化
103 | -- 9.2.1 原理知识
104 | -- 9.2.2 代码实现
105 | 9.3 局部直方图均衡化
106 | 9.4 自动色彩均衡化
107 | 9.5 本章小结
108 | - **第十章 Python图像平滑**
109 | 10.1 图像平滑概述
110 | 10.2 均值滤波
111 | 10.3 方框滤波
112 | 10.4 高斯滤波
113 | 10.5 中值滤波
114 | 10.6 双边滤波
115 | 10.7 本章小结
116 | - **第十一章 Python图像锐化及边缘检测**
117 | 11.1 原理概述
118 | 11.1.1 一阶微分算子
119 | 11.1.2 二阶微分算子
120 | 11.2 Roberts算子
121 | 11.3 Prewitt算子
122 | 11.4 Sobel算子
123 | 11.5 Laplacian算子
124 | 11.6 Scharr算子
125 | 11.7 Canny算子
126 | 11.8 LOG算子
127 | 11.9 本章小结
128 | - **第十二章 Python图像形态学处理**
129 | 12.1 数学形态学概述
130 | 12.2 图像腐蚀
131 | 12.3 图像膨胀
132 | 12.4 图像开运算
133 | 12.5 图像闭运算
134 | 12.6 图像梯度运算
135 | 12.7 图像顶帽运算
136 | 12.8 图像底帽运算
137 | 12.9 本章小结
138 | - **第十三章 Python图像特效处理**
139 | 13.1 图像毛玻璃特效
140 | 13.2 图像浮雕特效
141 | 13.3 图像油漆特效
142 | 13.4 图像素描特效
143 | 13.5 图像怀旧特效
144 | 13.6 图像光照特效
145 | 13.7 图像流年特效
146 | 13.8 图像水波特效
147 | 13.9 图像卡通特效
148 | 13.10 图像滤镜特效
149 | 13.11 图像直方图均衡化特效
150 | 13.12 图像模糊特效
151 | 13.13 本章小结
152 | - **第十四章 Python图像分割**
153 | 14.1 图像分割概述
154 | 14.2 基于阈值的图像分割
155 | 14.3 基于边缘检测的图像分割
156 | 14.4 基于纹理背景的图像分割
157 | 14.5 基于K-Means聚类的区域分割
158 | 14.6 基于均值漂移算法的图像分割
159 | 14.7 基于分水岭算法的图像分割
160 | 14.8 图像漫水填充分割
161 | 14.9 文字区域定位及提取案例
162 | 14.10 本章小结
163 | - **第十五章 Python傅里叶变换与霍夫变换**
164 | 15.1 图像傅里叶变换概述
165 | 15.2 图像傅里叶变换操作
166 | -- 15.2.1 Numpy实现傅里叶变换
167 | -- 15.2.2 Numpy实现傅里叶逆变换
168 | -- 15.2.3 OpenCV实现傅里叶变换
169 | -- 15.2.4 OpenCV实现傅里叶逆变换
170 | 15.3 基于傅里叶变换的高通滤波和低通滤波
171 | 15.4 图像霍夫变换概述
172 | 15.5 图像霍夫线变换操作
173 | 15.6 图像霍夫圆变换操作
174 | 15.7 本章小结
175 | - **第十六章 Python图像分类**
176 | 16.1 图像分类概述
177 | 16.2 常见的分类算法
178 | -- 16.2.1 朴素贝叶斯分类算法
179 | -- 16.2.2 KNN分类算法
180 | -- 16.2.3 SVM分类算法
181 | -- 16.2.4 随机森林分类算法
182 | -- 16.2.5 神经网络分类算法
183 | 16.3 基于朴素贝叶斯算法的图像分类
184 | 16.4 基于KNN算法的图像分类
185 | 16.5 基于神经网络算法的图像分类
186 | 16.6 本章小结
187 | 188 | 189 | -------------------------------------------------------------------------------- /chapter03-based-opencv/Lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter03-based-opencv/Lena.png -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_01.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | 4 | #读取图片 5 | img = cv2.imread("Lena.png") 6 | 7 | #显示图像 8 | cv2.imshow("Demo", img) 9 | 10 | #等待显示 11 | cv2.waitKey(0) 12 | cv2.destroyAllWindows() 13 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_02.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | 4 | #读取图片 5 | img = cv2.imread("Lena.png") 6 | 7 | #显示图像 8 | cv2.imshow("Demo", img) 9 | 10 | #无限期等待输入 11 | k=cv2.waitKey(0) 12 | 13 | #如果输入ESC按键退出 14 | if k==27: 15 | cv2.destroyAllWindows() 16 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_03.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import numpy as np 3 | 4 | #定义一维数组 5 | a = np.array([2, 0, 1, 5, 8, 3]) 6 | print('原始数据:', a) 7 | 8 | #输出最大、最小值及形状 9 | print('最小值:', a.min()) 10 | print('最大值:', a.max()) 11 | print('形状', a.shape) 12 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_04.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #显示图像 9 | cv2.imshow("image", img) 10 | 11 | #等待显示 12 | cv2.waitKey(0) 13 | cv2.destroyAllWindows() 14 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_05.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | #生成随机数表示点的坐标 6 | x = np.random.randn(200) 7 | y = np.random.randn(200) 8 | 9 | #生成随机点的大小及颜色 10 | size = 50*np.random.randn(200) 11 | colors = np.random.rand(200) 12 | 13 | #用来正常显示中文标签 14 | plt.rc('font', family='SimHei', size=13) 15 | 16 | #用来正常显示负号 17 | plt.rcParams['axes.unicode_minus'] = False 18 | 19 | #绘制散点图 20 | plt.scatter(x, y, s=size, c=colors) 21 | 22 | #设置x、y轴名称 23 | plt.xlabel(u"x坐标") 24 | plt.ylabel(u"y坐标") 25 | 26 | #绘制标题 27 | plt.title(u"Matplotlib绘制散点图") 28 | 29 | #显示图像 30 | plt.show() 31 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_06.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取图像 7 | img1 = cv2.imread('lena.png') 8 | img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) 9 | 10 | img2 = cv2.imread('people.png') 11 | img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) 12 | 13 | img3 = cv2.imread('flower.png') 14 | img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB) 15 | 16 | img4 = cv2.imread('scenery.png') 17 | img4 = cv2.cvtColor(img4, cv2.COLOR_BGR2RGB) 18 | 19 | #显示四张图像 20 | titles = ['lena', 'people', 'flower', 'scenery'] 21 | images = [img1, img2, img3, img4] 22 | for i in range(4): 23 | plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray') 24 | plt.title(titles[i]) 25 | plt.xticks([]),plt.yticks([]) 26 | plt.show() 27 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_07.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制直线 9 | cv2.line(img, (0,0), (255,255), (55,255,155), 5) 10 | 11 | #显示图像 12 | cv2.imshow("line", img) 13 | 14 | #等待显示 15 | cv2.waitKey(0) 16 | cv2.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_08.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制直线 9 | i = 0 10 | while i<255: 11 | cv2.line(img, (0,i), (255,255-i), (55,255,155), 5) 12 | i = i + 1 13 | 14 | #显示图像 15 | cv2.imshow("line", img) 16 | 17 | #等待显示 18 | cv2.waitKey(0) 19 | cv2.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_09.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制矩形 9 | cv2.rectangle(img, (20,20), (150,250), (255,0,0), 2) 10 | 11 | #显示图像 12 | cv2.imshow("rectangle", img) 13 | 14 | #等待显示 15 | cv2.waitKey(0) 16 | cv2.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_10.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制圆形 9 | cv2.circle(img, (100,100), 50, (255,255,0), 4) 10 | 11 | #显示图像 12 | cv2.imshow("circle", img) 13 | 14 | #等待显示 15 | cv2.waitKey(0) 16 | cv2.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_11.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制椭圆 9 | #椭圆中心(120,100) 长轴和短轴为(100,50) 10 | #偏转角度为20 11 | #圆弧起始角的角度0 圆弧终结角的角度360 12 | #颜色(255,0,255) 线条粗细2 13 | cv2.ellipse(img, (120, 100), (100, 50), 20, 0, 360, (255, 0, 255), 2) 14 | 15 | #显示图像 16 | cv2.imshow("ellipse", img) 17 | 18 | #等待显示 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_12.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制椭圆 9 | cv2.ellipse(img, (120, 120), (120, 80), 40, 0, 360, (255, 0, 255), -1) 10 | 11 | #显示图像 12 | cv2.imshow("ellipse", img) 13 | 14 | #等待显示 15 | cv2.waitKey(0) 16 | cv2.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_13.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制多边形 9 | pts = np.array([[10,80], [120,80], [120,200], [30,250]]) 10 | cv2.polylines(img, [pts], True, (255, 255, 255), 5) 11 | 12 | #显示图像 13 | cv2.imshow("ellipse", img) 14 | 15 | #等待显示 16 | cv2.waitKey(0) 17 | cv2.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_14.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((512,512,3), np.uint8) 7 | 8 | #绘制多边形 9 | pts = np.array([[50, 190], [380, 420], [255, 50], [120, 420], [450, 190]]) 10 | cv2.polylines(img, [pts], True, (0, 255, 255), 10) 11 | 12 | #显示图像 13 | cv2.imshow("ellipse", img) 14 | 15 | #等待显示 16 | cv2.waitKey(0) 17 | cv2.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /chapter03-based-opencv/chapter03_15.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #创建黑色图像 6 | img = np.zeros((256,256,3), np.uint8) 7 | 8 | #绘制文字 9 | font = cv2.FONT_HERSHEY_SIMPLEX 10 | cv2.putText(img, 'I love Python!!!', 11 | (10, 100), font, 0.6, (255, 255, 0), 2) 12 | 13 | #显示图像 14 | cv2.imshow("polylines", img) 15 | 16 | #等待显示 17 | cv2.waitKey(0) 18 | cv2.destroyAllWindows() 19 | -------------------------------------------------------------------------------- /chapter03-based-opencv/flower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter03-based-opencv/flower.png -------------------------------------------------------------------------------- /chapter03-based-opencv/lena-hd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter03-based-opencv/lena-hd.png -------------------------------------------------------------------------------- /chapter03-based-opencv/people.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter03-based-opencv/people.png -------------------------------------------------------------------------------- /chapter03-based-opencv/scenery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter03-based-opencv/scenery.png -------------------------------------------------------------------------------- /chapter04-logical-operation/Lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter04-logical-operation/Lena.png -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_01.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #显示图像 9 | cv2.imshow("Demo", img) 10 | 11 | #等待显示 12 | cv2.waitKey(0) 13 | cv2.destroyAllWindows() 14 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_02.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #读取像素 9 | test = img[88,142] 10 | print("读取的像素值:", test) 11 | 12 | #修改像素 13 | img[88,142] = [255, 255, 255] 14 | print("修改后的像素值:", test) 15 | 16 | #分别获取BGR通道像素 17 | blue = img[88,142,0] 18 | print("蓝色分量", blue) 19 | green = img[88,142,1] 20 | print("绿色分量", green) 21 | red = img[88,142,2] 22 | print("红色分量", red) 23 | 24 | #显示图像 25 | cv2.imshow("Demo", img) 26 | 27 | #等待显示 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_03.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #该区域设置为白色 9 | img[100:200, 150:250] = [255,255,255] 10 | 11 | #显示图像 12 | cv2.imshow("Demo", img) 13 | 14 | #等待显示 15 | cv2.waitKey(0) 16 | cv2.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_04.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | print(type(img)) 9 | 10 | #Numpy读取像素 11 | print(img.item(78, 100, 0)) 12 | print(img.item(78, 100, 1)) 13 | print(img.item(78, 100, 2)) 14 | 15 | #Numpy修改像素 16 | img.itemset((78, 100, 0), 100) 17 | img.itemset((78, 100, 1), 100) 18 | img.itemset((78, 100, 2), 100) 19 | print(img.item(78, 100, 0)) 20 | print(img.item(78, 100, 1)) 21 | print(img.item(78, 100, 2)) 22 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_05.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | 9 | #创建空图像 10 | emptyImage = np.zeros(img.shape, np.uint8) 11 | 12 | #复制图像 13 | emptyImage2 = img.copy() 14 | 15 | #显示图像 16 | cv2.imshow("Demo1", img) 17 | cv2.imshow("Demo2", emptyImage) 18 | cv2.imshow("Demo3", emptyImage2) 19 | 20 | #等待显示 21 | cv2.waitKey(0) 22 | cv2.destroyAllWindows() 23 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_06.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图像 7 | img = cv2.imread("Lena.png") 8 | 9 | #显示图像 10 | cv2.imshow("Demo", img) 11 | 12 | #保存图像 13 | cv2.imwrite("dst1.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 5]) 14 | cv2.imwrite("dst2.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) 15 | cv2.imwrite("dst3.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0]) 16 | cv2.imwrite("dst4.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9]) 17 | 18 | #等待显示 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_07.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #获取图像形状 9 | print(img.shape) 10 | 11 | #显示图像 12 | cv2.imshow("Demo", img) 13 | 14 | #等待显示 15 | cv2.waitKey(0) 16 | cv2.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_08.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #获取图像形状 9 | print(img.shape) 10 | 11 | #获取像素数目 12 | print(img.size) 13 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_09.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #获取图像形状 9 | print(img.shape) 10 | 11 | #获取像素数目 12 | print(img.size) 13 | 14 | #获取图像数据类型 15 | print(img.dtype) 16 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_10.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | 9 | #拆分通道 10 | b, g, r = cv2.split(img) 11 | 12 | #b = img[:, :, 0] 13 | #g = img[:, :, 1] 14 | #r = img[:, :, 2] 15 | 16 | #显示原始图像 17 | cv2.imshow("B", b) 18 | cv2.imshow("G", g) 19 | cv2.imshow("R", r) 20 | 21 | #等待显示 22 | cv2.waitKey(0) 23 | cv2.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_11.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | 9 | #拆分通道 10 | b, g, r = cv2.split(img) 11 | 12 | #合并通道 13 | m = cv2.merge([b, g, r]) 14 | cv2.imshow("Merge", m) 15 | 16 | #等待显示 17 | cv2.waitKey(0) 18 | cv2.destroyAllWindows() 19 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_12.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | rows, cols, chn = img.shape 9 | 10 | #拆分通道 11 | b = cv2.split(img)[0] 12 | 13 | #设置g、r通道为0 14 | g = np.zeros((rows,cols), dtype=img.dtype) 15 | r = np.zeros((rows,cols), dtype=img.dtype) 16 | 17 | #合并通道 18 | m = cv2.merge([b, g, r]) 19 | cv2.imshow("Merge", m) 20 | 21 | #等待显示 22 | cv2.waitKey(0) 23 | cv2.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_13.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import cv2 3 | import numpy as np 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #图像各像素加100 9 | m = np.ones(img.shape, dtype="uint8")*100 10 | 11 | #OpenCV加法运算 12 | result = cv2.add(img, m) 13 | 14 | #显示图像 15 | cv2.imshow("original", img) 16 | cv2.imshow("result", result) 17 | 18 | #等待显示 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_14.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | 9 | #图像各像素减50 10 | m = np.ones(img.shape, dtype="uint8")*50 11 | 12 | #OpenCV减法运算 13 | result = cv2.subtract(img, m) 14 | 15 | #显示图像 16 | cv2.imshow("original", img) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_15.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png", cv2.IMREAD_GRAYSCALE) 8 | 9 | #获取图像宽和高 10 | rows, cols = img.shape[:2] 11 | print(rows, cols) 12 | 13 | #画圆形 14 | circle = np.zeros((rows, cols), dtype="uint8") 15 | cv2.circle(circle, (int(rows/2.0), int(cols/2)), 80, 255, -1) 16 | print(circle.shape) 17 | print(img.size, circle.size) 18 | 19 | #OpenCV图像与运算 20 | result = cv2.bitwise_and(img, circle) 21 | 22 | #显示图像 23 | cv2.imshow("original", img) 24 | cv2.imshow("circle", circle) 25 | cv2.imshow("result", result) 26 | 27 | #等待显示 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_16.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png", cv2.IMREAD_GRAYSCALE) 8 | 9 | #获取图像宽和高 10 | rows, cols = img.shape[:2] 11 | print(rows, cols) 12 | 13 | #画圆形 14 | circle = np.zeros((rows, cols), dtype="uint8") 15 | cv2.circle(circle, (int(rows/2), int(cols/2)), 80, 255, -1) 16 | print(circle.shape) 17 | print(img.size, circle.size) 18 | 19 | #OpenCV图像或运算 20 | result = cv2.bitwise_or(img, circle) 21 | 22 | #显示图像 23 | cv2.imshow("original", img) 24 | cv2.imshow("circle", circle) 25 | cv2.imshow("result", result) 26 | 27 | #等待显示 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_17.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png", cv2.IMREAD_GRAYSCALE) 8 | 9 | #获取图像宽和高 10 | rows, cols = img.shape[:2] 11 | print(rows, cols) 12 | 13 | #画圆形 14 | circle = np.zeros((rows, cols), dtype="uint8") 15 | cv2.circle(circle, (int(rows/2), int(cols/2)), 80, 255, -1) 16 | print(circle.shape) 17 | print(img.size, circle.size) 18 | 19 | #OpenCV图像异或运算 20 | result = cv2.bitwise_xor(img, circle) 21 | 22 | #显示图像 23 | cv2.imshow("original", img) 24 | cv2.imshow("circle", circle) 25 | cv2.imshow("result", result) 26 | 27 | #等待显示 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_18.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import cv2 3 | import numpy as np 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png", cv2.IMREAD_GRAYSCALE) 7 | 8 | #OpenCV图像非运算 9 | result = cv2.bitwise_not(img) 10 | 11 | #显示图像 12 | cv2.imshow("original", img) 13 | cv2.imshow("result", result) 14 | 15 | #等待显示 16 | cv2.waitKey(0) 17 | cv2.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_19.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | src1 = cv2.imread('lena-hd.png') 9 | src2 = cv2.imread('na.png') 10 | 11 | #图像融合 12 | #result = cv2.addWeighted(src1, 1, src2, 1, 0) 13 | result = cv2.addWeighted(src1, 0.6, src2, 0.8, 10) 14 | 15 | #显示图像 16 | cv2.imshow("src1", src1) 17 | cv2.imshow("src2", src2) 18 | cv2.imshow("result", result) 19 | 20 | #等待显示 21 | cv2.waitKey(0) 22 | cv2.destroyAllWindows() 23 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_20.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #读取图片 6 | img = cv2.imread("Lena.png") 7 | 8 | #定义200×200矩阵 3对应BGR 9 | face = np.ones((200, 200, 3)) 10 | 11 | #显示原始图像 12 | cv2.imshow("Demo", img) 13 | 14 | #显示ROI区域 15 | face = img[150:350, 150:350] 16 | cv2.imshow("face", face) 17 | 18 | #等待显示 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_21.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | img = cv2.imread("Lena.png") 8 | test = cv2.imread("test.jpg") 9 | 10 | #定义150×150矩阵 3对应BGR 11 | face = np.ones((150, 150, 3)) 12 | 13 | #显示ROI区域 14 | face = img[170:320, 170:320] 15 | cv2.imshow("Demo", face) 16 | 17 | test[220:370, 220:370] = face 18 | cv2.imshow("Result", test) 19 | 20 | #等待显示 21 | cv2.waitKey(0) 22 | cv2.destroyAllWindows() 23 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_22.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取图片 7 | src = cv2.imread('Lena.png') 8 | 9 | #图像类型转换 10 | result = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) 11 | 12 | #显示图像 13 | cv2.imshow("src", src) 14 | cv2.imshow("result", result) 15 | 16 | #等待显示 17 | cv2.waitKey(0) 18 | cv2.destroyAllWindows() 19 | -------------------------------------------------------------------------------- /chapter04-logical-operation/chapter04_23.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By: Eastmount CSDN 2021-01-26 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img_BGR = cv2.imread('na.png') 9 | 10 | #BGR转换为RGB 11 | img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB) 12 | 13 | #灰度化处理 14 | img_GRAY = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2GRAY) 15 | 16 | #BGR转HSV 17 | img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV) 18 | 19 | #BGR转YCrCb 20 | img_YCrCb = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YCrCb) 21 | 22 | #BGR转HLS 23 | img_HLS = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HLS) 24 | 25 | #BGR转XYZ 26 | img_XYZ = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2XYZ) 27 | 28 | #BGR转LAB 29 | img_LAB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2LAB) 30 | 31 | #BGR转YUV 32 | img_YUV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YUV) 33 | 34 | #调用matplotlib显示处理结果 35 | titles = ['BGR', 'RGB', 'GRAY', 'HSV', 'YCrCb', 'HLS', 'XYZ', 'LAB', 'YUV'] 36 | images = [img_BGR, img_RGB, img_GRAY, img_HSV, img_YCrCb, 37 | img_HLS, img_XYZ, img_LAB, img_YUV] 38 | for i in range(9): 39 | plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray') 40 | plt.title(titles[i]) 41 | plt.xticks([]),plt.yticks([]) 42 | plt.show() 43 | -------------------------------------------------------------------------------- /chapter04-logical-operation/flower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter04-logical-operation/flower.png -------------------------------------------------------------------------------- /chapter04-logical-operation/lena-hd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter04-logical-operation/lena-hd.png -------------------------------------------------------------------------------- /chapter04-logical-operation/na.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter04-logical-operation/na.png -------------------------------------------------------------------------------- /chapter04-logical-operation/scenery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter04-logical-operation/scenery.png -------------------------------------------------------------------------------- /chapter04-logical-operation/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter04-logical-operation/test.jpg -------------------------------------------------------------------------------- /chapter05-geometric-trans/canny.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter05-geometric-trans/canny.jpg -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-01-py.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test.bmp') 8 | 9 | #图像平移矩阵 10 | M = np.float32([[1, 0, 100], [0, 1, 50]]) 11 | 12 | #获取原始图像列数和行数 13 | rows, cols = src.shape[:2] 14 | 15 | #图像平移 16 | result = cv2.warpAffine(src, M, (cols, rows)) 17 | 18 | #显示图像 19 | cv2.imshow("original", src) 20 | cv2.imshow("result", result) 21 | 22 | #等待显示 23 | cv2.waitKey(0) 24 | cv2.destroyAllWindows() 25 | 26 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-02-py.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('test.bmp') 9 | image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #图像平移 12 | #垂直方向 向下平移100 13 | M = np.float32([[1, 0, 0], [0, 1, 100]]) 14 | img1 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) 15 | 16 | #垂直方向 向上平移100 17 | M = np.float32([[1, 0, 0], [0, 1, -100]]) 18 | img2 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) 19 | 20 | #水平方向 向右平移100 21 | M = np.float32([[1, 0, 100], [0, 1, 0]]) 22 | img3 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) 23 | 24 | #水平方向 向左平移100 25 | M = np.float32([[1, 0, -100], [0, 1, 0]]) 26 | img4 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) 27 | 28 | #循环显示图形 29 | titles = [ 'Image1', 'Image2', 'Image3', 'Image4'] 30 | images = [img1, img2, img3, img4] 31 | for i in range(4): 32 | plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray') 33 | plt.title(titles[i]) 34 | plt.xticks([]),plt.yticks([]) 35 | plt.show() 36 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-03-sf.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test.bmp') 8 | 9 | #图像缩放 10 | result = cv2.resize(src, (200,100)) 11 | print(result.shape) 12 | 13 | #显示图像 14 | cv2.imshow("original", src) 15 | cv2.imshow("result", result) 16 | 17 | #等待显示 18 | cv2.waitKey(0) 19 | cv2.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-04-sf.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test.bmp') 8 | rows, cols = src.shape[:2] 9 | print(rows, cols) 10 | 11 | #图像缩放 dsize(列,行) 12 | result = cv2.resize(src, (int(cols*0.6), int(rows*1.2))) 13 | 14 | #显示图像 15 | cv2.imshow("src", src) 16 | cv2.imshow("result", result) 17 | 18 | #等待显示 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-05-sf.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test.bmp') 8 | rows, cols = src.shape[:2] 9 | print(rows, cols) 10 | 11 | #图像缩放 12 | result = cv2.resize(src, None, fx=0.3, fy=0.3) 13 | 14 | #显示图像 15 | cv2.imshow("src", src) 16 | cv2.imshow("result", result) 17 | 18 | #等待显示 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-06-xz.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test.bmp') 8 | 9 | #源图像的高、宽 以及通道数 10 | rows, cols, channel = src.shape 11 | 12 | #绕图像的中心旋转 13 | M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1) #旋转中心 旋转度数 scale 14 | rotated = cv2.warpAffine(src, M, (cols, rows)) #原始图像 旋转参数 元素图像宽高 15 | 16 | #显示图像 17 | cv2.imshow("src", src) 18 | cv2.imshow("rotated", rotated) 19 | 20 | #等待显示 21 | cv2.waitKey(0) 22 | cv2.destroyAllWindows() 23 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-07-xz.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test.bmp') 8 | 9 | #源图像的高、宽 以及通道数 10 | rows, cols, channel = src.shape 11 | 12 | #绕图像的中心旋转 13 | #函数参数:旋转中心 旋转度数 scale 14 | M = cv2.getRotationMatrix2D((cols/2, rows/2), -90, 1) 15 | 16 | #函数参数:原始图像 旋转参数 元素图像宽高 17 | rotated = cv2.warpAffine(src, M, (cols, rows)) 18 | 19 | #显示图像 20 | cv2.imshow("src", src) 21 | cv2.imshow("rotated", rotated) 22 | 23 | #等待显示 24 | cv2.waitKey(0) 25 | cv2.destroyAllWindows() 26 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-08-jx.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('test.bmp') 9 | src = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #图像翻转 12 | img1 = cv2.flip(src, 0) #参数=0以X轴为对称轴翻转 13 | img2 = cv2.flip(src, 1) #参数>0以Y轴为对称轴翻转 14 | img3 = cv2.flip(src, -1) #参数<0X轴和Y轴翻转 15 | 16 | #显示图形 17 | titles = ['Source', 'Image1', 'Image2', 'Image3'] 18 | images = [src, img1, img2, img3] 19 | for i in range(4): 20 | plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray') 21 | plt.title(titles[i]) 22 | plt.xticks([]),plt.yticks([]) 23 | plt.show() 24 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-09-fs.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | src = cv2.imread('test.bmp') 9 | 10 | #获取图像大小 11 | rows, cols = src.shape[:2] 12 | 13 | #设置图像仿射变换矩阵 14 | pos1 = np.float32([[50,50], [200,50], [50,200]]) 15 | pos2 = np.float32([[10,100], [200,50], [100,250]]) 16 | M = cv2.getAffineTransform(pos1, pos2) 17 | 18 | #图像仿射变换 19 | result = cv2.warpAffine(src, M, (cols, rows)) 20 | 21 | #显示图像 22 | cv2.imshow("original", src) 23 | cv2.imshow("result", result) 24 | 25 | #等待显示 26 | cv2.waitKey(0) 27 | cv2.destroyAllWindows() 28 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-10-ts.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | src = cv2.imread('test01.jpg') 9 | 10 | #获取图像大小 11 | rows, cols = src.shape[:2] 12 | 13 | #设置图像透视变换矩阵 14 | pos1 = np.float32([[114, 82], [287, 156], [8, 322], [216, 333]]) 15 | pos2 = np.float32([[0, 0], [188, 0], [0, 262], [188, 262]]) 16 | M = cv2.getPerspectiveTransform(pos1, pos2) 17 | 18 | #图像透视变换 19 | result = cv2.warpPerspective(src, M, (190, 272)) 20 | 21 | #显示图像 22 | cv2.imshow("original", src) 23 | cv2.imshow("result", result) 24 | 25 | #等待显示 26 | cv2.waitKey(0) 27 | cv2.destroyAllWindows() 28 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-11-ts.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | src = cv2.imread('test01.jpg') 9 | 10 | #获取图像大小 11 | rows, cols = src.shape[:2] 12 | 13 | #将源图像高斯模糊 14 | img = cv2.GaussianBlur(src, (3,3), 0) 15 | #进行灰度化处理 16 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 17 | 18 | #边缘检测(检测出图像的边缘信息) 19 | edges = cv2.Canny(gray,50,250,apertureSize = 3) 20 | cv2.imwrite("canny.jpg", edges) 21 | 22 | #通过霍夫变换得到A4纸边缘 23 | lines = cv2.HoughLinesP(edges,1,np.pi/180,50,minLineLength=90,maxLineGap=10) 24 | 25 | #下面输出的四个点分别为四个顶点 26 | for x1,y1,x2,y2 in lines[0]: 27 | print((x1,y1),(x2,y2)) 28 | for x1,y1,x2,y2 in lines[1]: 29 | print((x1,y1),(x2,y2)) 30 | 31 | #绘制边缘 32 | for x1,y1,x2,y2 in lines[0]: 33 | cv2.line(gray, (x1,y1), (x2,y2), (0,0,255), 1) 34 | 35 | #根据四个顶点设置图像透视变换矩阵 36 | pos1 = np.float32([[114, 82], [287, 156], [8, 322], [216, 333]]) 37 | pos2 = np.float32([[0, 0], [188, 0], [0, 262], [188, 262]]) 38 | M = cv2.getPerspectiveTransform(pos1, pos2) 39 | 40 | #图像透视变换 41 | result = cv2.warpPerspective(src, M, (190, 272)) 42 | 43 | #显示图像 44 | cv2.imshow("original", src) 45 | cv2.imshow("result", result) 46 | 47 | #等待显示 48 | cv2.waitKey(0) 49 | cv2.destroyAllWindows() 50 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/chapter05-12-all.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-01 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('test.bmp') 9 | image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #图像平移矩阵 12 | M = np.float32([[1, 0, 100], [0, 1, 80]]) 13 | rows, cols = image.shape[:2] 14 | img1 = cv2.warpAffine(image, M, (cols, rows)) 15 | 16 | #图像缩小 17 | img2 = cv2.resize(image, (200,100)) 18 | 19 | #图像放大 20 | img3 = cv2.resize(image, None, fx=1.1, fy=1.1) 21 | 22 | #绕图像的中心旋转 23 | #源图像的高、宽 以及通道数 24 | rows, cols, channel = image.shape 25 | #函数参数:旋转中心 旋转度数 scale 26 | M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1) 27 | #函数参数:原始图像 旋转参数 元素图像宽高 28 | img4 = cv2.warpAffine(image, M, (cols, rows)) 29 | 30 | #图像翻转 31 | img5 = cv2.flip(image, 0) #参数=0以X轴为对称轴翻转 32 | img6 = cv2.flip(image, 1) #参数>0以Y轴为对称轴翻转 33 | 34 | #图像的仿射 35 | pts1 = np.float32([[50,50],[200,50],[50,200]]) 36 | pts2 = np.float32([[10,100],[200,50],[100,250]]) 37 | M = cv2.getAffineTransform(pts1,pts2) 38 | img7 = cv2.warpAffine(image, M, (rows,cols)) 39 | 40 | #图像的透射 41 | pts1 = np.float32([[56,65],[238,52],[28,237],[239,240]]) 42 | pts2 = np.float32([[0,0],[200,0],[0,200],[200,200]]) 43 | M = cv2.getPerspectiveTransform(pts1,pts2) 44 | img8 = cv2.warpPerspective(image,M,(200,200)) 45 | 46 | 47 | #循环显示图形 48 | titles = [ 'source', 'shift', 'reduction', 'enlarge', 'rotation', 'flipX', 'flipY', 'affine', 'transmission'] 49 | images = [image, img1, img2, img3, img4, img5, img6, img7, img8] 50 | for i in range(9): 51 | plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray') 52 | plt.title(titles[i]) 53 | plt.xticks([]),plt.yticks([]) 54 | plt.show() 55 | 56 | 57 | -------------------------------------------------------------------------------- /chapter05-geometric-trans/test.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter05-geometric-trans/test.bmp -------------------------------------------------------------------------------- /chapter05-geometric-trans/test01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter05-geometric-trans/test01.jpg -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_01.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('lena.png') 9 | 10 | #获取图像高度和宽度 11 | height = img.shape[0] 12 | width = img.shape[1] 13 | 14 | #创建一幅图像 15 | new_img = np.zeros((height, width, 3), np.uint8) 16 | 17 | #图像量化操作 量化等级为2 18 | for i in range(height): 19 | for j in range(width): 20 | for k in range(3): #对应BGR三分量 21 | if img[i, j][k] < 128: 22 | gray = 0 23 | else: 24 | gray = 128 25 | new_img[i, j][k] = np.uint8(gray) 26 | 27 | #显示图像 28 | cv2.imshow("src", img) 29 | cv2.imshow("Quantization", new_img) 30 | 31 | #等待显示 32 | cv2.waitKey(0) 33 | cv2.destroyAllWindows() 34 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_02.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('lena.png') 9 | 10 | #获取图像高度和宽度 11 | height = img.shape[0] 12 | width = img.shape[1] 13 | 14 | #创建一幅图像 15 | new_img1 = np.zeros((height, width, 3), np.uint8) 16 | new_img2 = np.zeros((height, width, 3), np.uint8) 17 | new_img3 = np.zeros((height, width, 3), np.uint8) 18 | 19 | #图像量化等级为2的量化处理 20 | for i in range(height): 21 | for j in range(width): 22 | for k in range(3): #对应BGR三分量 23 | if img[i, j][k] < 128: 24 | gray = 0 25 | else: 26 | gray = 128 27 | new_img1[i, j][k] = np.uint8(gray) 28 | 29 | #图像量化等级为4的量化处理 30 | for i in range(height): 31 | for j in range(width): 32 | for k in range(3): #对应BGR三分量 33 | if img[i, j][k] < 64: 34 | gray = 0 35 | elif img[i, j][k] < 128: 36 | gray = 64 37 | elif img[i, j][k] < 192: 38 | gray = 128 39 | else: 40 | gray = 192 41 | new_img2[i, j][k] = np.uint8(gray) 42 | 43 | #图像量化等级为8的量化处理 44 | for i in range(height): 45 | for j in range(width): 46 | for k in range(3): #对应BGR三分量 47 | if img[i, j][k] < 32: 48 | gray = 0 49 | elif img[i, j][k] < 64: 50 | gray = 32 51 | elif img[i, j][k] < 96: 52 | gray = 64 53 | elif img[i, j][k] < 128: 54 | gray = 96 55 | elif img[i, j][k] < 160: 56 | gray = 128 57 | elif img[i, j][k] < 192: 58 | gray = 160 59 | elif img[i, j][k] < 224: 60 | gray = 192 61 | else: 62 | gray = 224 63 | new_img3[i, j][k] = np.uint8(gray) 64 | 65 | #用来正常显示中文标签 66 | plt.rcParams['font.sans-serif']=['SimHei'] 67 | 68 | #显示图像 69 | titles = ['(a) 原始图像', '(b) 量化-L2', '(c) 量化-L4', '(d) 量化-L8'] 70 | images = [img, new_img1, new_img2, new_img3] 71 | for i in range(4): 72 | plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray'), 73 | plt.title(titles[i]) 74 | plt.xticks([]),plt.yticks([]) 75 | plt.show() 76 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_03.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('nv.png') 9 | 10 | #图像二维像素转换为一维 11 | data = img.reshape((-1,3)) 12 | data = np.float32(data) 13 | 14 | #定义中心 (type,max_iter,epsilon) 15 | criteria = (cv2.TERM_CRITERIA_EPS + 16 | cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 17 | 18 | #设置标签 19 | flags = cv2.KMEANS_RANDOM_CENTERS 20 | 21 | #K-Means聚类 聚集成4类 22 | compactness, labels, centers = cv2.kmeans(data, 4, None, criteria, 10, flags) 23 | 24 | 25 | #图像转换回uint8二维类型 26 | centers = np.uint8(centers) 27 | res = centers[labels.flatten()] 28 | dst = res.reshape((img.shape)) 29 | 30 | #图像转换为RGB显示 31 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 32 | dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB) 33 | 34 | 35 | #用来正常显示中文标签 36 | plt.rcParams['font.sans-serif']=['SimHei'] 37 | 38 | #显示图像 39 | titles = ['原始图像', '聚类量化 K=4'] 40 | images = [img, dst] 41 | for i in range(2): 42 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray'), 43 | plt.title(titles[i]) 44 | plt.xticks([]),plt.yticks([]) 45 | plt.show() 46 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_04.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('lena.png') 9 | 10 | #获取图像高度和宽度 11 | height = img.shape[0] 12 | width = img.shape[1] 13 | 14 | #采样转换成16*16区域 15 | numHeight = int(height/16) 16 | numWidth = int(width/16) 17 | 18 | #创建一幅图像 19 | new_img = np.zeros((height, width, 3), np.uint8) 20 | 21 | #图像循环采样16*16区域 22 | for i in range(16): 23 | #获取Y坐标 24 | y = i*numHeight 25 | for j in range(16): 26 | #获取X坐标 27 | x = j*numWidth 28 | #获取填充颜色 左上角像素点 29 | b = img[y, x][0] 30 | g = img[y, x][1] 31 | r = img[y, x][2] 32 | 33 | #循环设置小区域采样 34 | for n in range(numHeight): 35 | for m in range(numWidth): 36 | new_img[y+n, x+m][0] = np.uint8(b) 37 | new_img[y+n, x+m][1] = np.uint8(g) 38 | new_img[y+n, x+m][2] = np.uint8(r) 39 | 40 | #显示图像 41 | cv2.imshow("src", img) 42 | cv2.imshow("Sampling", new_img) 43 | 44 | #等待显示 45 | cv2.waitKey(0) 46 | cv2.destroyAllWindows() 47 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_05.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('scenery.png') 9 | 10 | #获取图像高度和宽度 11 | height = img.shape[0] 12 | width = img.shape[1] 13 | 14 | #采样转换成8*8区域 15 | numHeight = int(height/8) 16 | numwidth = int(width/8) 17 | 18 | #创建一幅图像 19 | new_img = np.zeros((height, width, 3), np.uint8) 20 | 21 | #图像循环采样8*8区域 22 | for i in range(8): 23 | #获取Y坐标 24 | y = i*numHeight 25 | for j in range(8): 26 | #获取X坐标 27 | x = j*numwidth 28 | #获取填充颜色 左上角像素点 29 | b = img[y, x][0] 30 | g = img[y, x][1] 31 | r = img[y, x][2] 32 | 33 | #循环设置小区域采样 34 | for n in range(numHeight): 35 | for m in range(numwidth): 36 | new_img[y+n, x+m][0] = np.uint8(b) 37 | new_img[y+n, x+m][1] = np.uint8(g) 38 | new_img[y+n, x+m][2] = np.uint8(r) 39 | 40 | #显示图像 41 | cv2.imshow("src", img) 42 | cv2.imshow("Sampling", new_img) 43 | 44 | #等待显示 45 | cv2.waitKey(0) 46 | cv2.destroyAllWindows() 47 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_06.py: -------------------------------------------------------------------------------- 1 | # -- coding:utf-8 -- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | im = cv2.imread('people.png', 1) 9 | 10 | #设置鼠标左键开启 11 | en = False 12 | 13 | #鼠标事件 14 | def draw(event, x, y, flags, param): 15 | global en 16 | #鼠标左键按下开启en值 17 | if event==cv2.EVENT_LBUTTONDOWN: 18 | en = True 19 | #鼠标左键按下并且移动 20 | elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_LBUTTONDOWN: 21 | #调用函数打马赛克 22 | if en: 23 | drawMask(y,x) 24 | #鼠标左键弹起结束操作 25 | elif event==cv2.EVENT_LBUTTONUP: 26 | en = False 27 | 28 | #图像局部采样操作 29 | def drawMask(x, y, size=10): 30 | #size*size采样处理 31 | m = int(x / size * size) 32 | n = int(y / size * size) 33 | print(m, n) 34 | #10*10区域设置为同一像素值 35 | for i in range(size): 36 | for j in range(size): 37 | im[m+i][n+j] = im[m][n] 38 | 39 | #打开对话框 40 | cv2.namedWindow('image') 41 | 42 | #调用draw函数设置鼠标操作 43 | cv2.setMouseCallback('image', draw) 44 | 45 | #循环处理 46 | while(1): 47 | cv2.imshow('image', im) 48 | #按ESC键退出 49 | if cv2.waitKey(10)&0xFF==27: 50 | break 51 | #按s键保存图片 52 | elif cv2.waitKey(10)&0xFF==115: 53 | cv2.imwrite('sava.png', im) 54 | 55 | #退出窗口 56 | cv2.destroyAllWindows() 57 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_07.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('nv.png') 9 | 10 | #图像向下取样 11 | r = cv2.pyrDown(img) 12 | 13 | #显示图像 14 | cv2.imshow('original', img) 15 | cv2.imshow('PyrDown', r) 16 | cv2.waitKey() 17 | cv2.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_08.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取原始图像 7 | img = cv2.imread('nv.png') 8 | 9 | #图像向下取样 10 | r1 = cv2.pyrDown(img) 11 | r2 = cv2.pyrDown(r1) 12 | r3 = cv2.pyrDown(r2) 13 | 14 | #显示图像 15 | cv2.imshow('original', img) 16 | cv2.imshow('PyrDown1', r1) 17 | cv2.imshow('PyrDown2', r2) 18 | cv2.imshow('PyrDown3', r3) 19 | cv2.waitKey() 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_09.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # BY:Eastmount CSDN 2020-11-10 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('lena.png') 9 | 10 | #图像向上取样 11 | r = cv2.pyrUp(img) 12 | 13 | #显示图像 14 | cv2.imshow('original', img) 15 | cv2.imshow('PyrUp', r) 16 | cv2.waitKey() 17 | cv2.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /chapter06-lh&cy/chapter06_10.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取原始图像 7 | img = cv2.imread('lena2.png') 8 | 9 | #图像向上取样 10 | r1 = cv2.pyrUp(img) 11 | r2 = cv2.pyrUp(r1) 12 | r3 = cv2.pyrUp(r2) 13 | 14 | #显示图像 15 | cv2.imshow('original', img) 16 | cv2.imshow('PyrUp1', r1) 17 | cv2.imshow('PyrUp2', r2) 18 | cv2.imshow('PyrUp3', r3) 19 | cv2.waitKey() 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter06-lh&cy/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/figure_1.png -------------------------------------------------------------------------------- /chapter06-lh&cy/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/figure_2.png -------------------------------------------------------------------------------- /chapter06-lh&cy/lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/lena.png -------------------------------------------------------------------------------- /chapter06-lh&cy/lena2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/lena2.png -------------------------------------------------------------------------------- /chapter06-lh&cy/nv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/nv.png -------------------------------------------------------------------------------- /chapter06-lh&cy/people.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/people.png -------------------------------------------------------------------------------- /chapter06-lh&cy/sava.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/sava.png -------------------------------------------------------------------------------- /chapter06-lh&cy/scenery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter06-lh&cy/scenery.png -------------------------------------------------------------------------------- /chapter07-dys/2016-.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2016-.png -------------------------------------------------------------------------------- /chapter07-dys/2016.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2016.png -------------------------------------------------------------------------------- /chapter07-dys/2017.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2017.png -------------------------------------------------------------------------------- /chapter07-dys/2018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2018.png -------------------------------------------------------------------------------- /chapter07-dys/2019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2019.png -------------------------------------------------------------------------------- /chapter07-dys/2020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2020.png -------------------------------------------------------------------------------- /chapter07-dys/2021.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2021.png -------------------------------------------------------------------------------- /chapter07-dys/2023.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/2023.png -------------------------------------------------------------------------------- /chapter07-dys/chapter07_01.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | import cv2 3 | import numpy as np 4 | 5 | #读取原始图片 6 | src = cv2.imread('miao.png') 7 | 8 | #图像灰度化处理 9 | grayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 10 | 11 | #显示图像 12 | cv2.imshow("src", src) 13 | cv2.imshow("result", grayImage) 14 | 15 | #等待显示 16 | cv2.waitKey(0) 17 | cv2.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_02.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img_BGR = cv2.imread('miao.png') 9 | 10 | #BGR转换为RGB 11 | img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB) 12 | 13 | #灰度化处理 14 | img_GRAY = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2GRAY) 15 | 16 | #BGR转HSV 17 | img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV) 18 | 19 | #BGR转YCrCb 20 | img_YCrCb = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YCrCb) 21 | 22 | #BGR转HLS 23 | img_HLS = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HLS) 24 | 25 | #BGR转XYZ 26 | img_XYZ = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2XYZ) 27 | 28 | #BGR转LAB 29 | img_LAB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2LAB) 30 | 31 | #BGR转YUV 32 | img_YUV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YUV) 33 | 34 | #调用matplotlib显示处理结果 35 | titles = ['BGR', 'RGB', 'GRAY', 'HSV', 'YCrCb', 'HLS', 'XYZ', 'LAB', 'YUV'] 36 | images = [img_BGR, img_RGB, img_GRAY, img_HSV, img_YCrCb, 37 | img_HLS, img_XYZ, img_LAB, img_YUV] 38 | for i in range(9): 39 | plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray') 40 | plt.title(titles[i]) 41 | plt.xticks([]),plt.yticks([]) 42 | plt.show() 43 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_03.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('miao.png') 9 | 10 | #获取图像高度和宽度 11 | height = img.shape[0] 12 | width = img.shape[1] 13 | 14 | #创建一幅图像 15 | grayimg = np.zeros((height, width, 3), np.uint8) 16 | 17 | #图像最大值灰度处理 18 | for i in range(height): 19 | for j in range(width): 20 | #获取图像R G B最大值 21 | gray = max(img[i,j][0], img[i,j][1], img[i,j][2]) 22 | #灰度图像素赋值 gray=max(R,G,B) 23 | grayimg[i,j] = np.uint8(gray) 24 | 25 | #显示图像 26 | cv2.imshow("src", img) 27 | cv2.imshow("gray", grayimg) 28 | 29 | #等待显示 30 | cv2.waitKey(0) 31 | cv2.destroyAllWindows() 32 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_04.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('miao.png') 9 | 10 | #获取图像高度和宽度 11 | height = img.shape[0] 12 | width = img.shape[1] 13 | 14 | #创建一幅图像 15 | grayimg = np.zeros((height, width, 3), np.uint8) 16 | print grayimg 17 | 18 | #图像平均灰度处理方法 19 | for i in range(height): 20 | for j in range(width): 21 | #灰度值为RGB三个分量的平均值 22 | gray = (int(img[i,j][0]) + int(img[i,j][1]) + int(img[i,j][2])) / 3 23 | grayimg[i,j] = np.uint8(gray) 24 | 25 | #显示图像 26 | cv2.imshow("src", img) 27 | cv2.imshow("gray", grayimg) 28 | 29 | #等待显示 30 | cv2.waitKey(0) 31 | cv2.destroyAllWindows() 32 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_05.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取原始图像 7 | img = cv2.imread('miao.png') 8 | 9 | #获取图像高度和宽度 10 | height = img.shape[0] 11 | width = img.shape[1] 12 | 13 | #创建一幅图像 14 | grayimg = np.zeros((height, width, 3), np.uint8) 15 | print(grayimg) 16 | 17 | #图像平均灰度处理方法 18 | for i in range(height): 19 | for j in range(width): 20 | #灰度加权平均法 21 | gray = 0.30 * img[i,j][0] + 0.59 * img[i,j][1] + 0.11 * img[i,j][2] 22 | grayimg[i,j] = np.uint8(gray) 23 | 24 | #显示图像 25 | cv2.imshow("src", img) 26 | cv2.imshow("gray", grayimg) 27 | 28 | #等待显示 29 | cv2.waitKey(0) 30 | cv2.destroyAllWindows() 31 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_06.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('miao.png') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | 17 | #创建一幅图像 18 | result = np.zeros((height, width, 3), np.uint8) 19 | 20 | #图像灰度上移变换 DB=DA+50 21 | for i in range(height): 22 | for j in range(width): 23 | 24 | if (int(grayImage[i,j]+50) > 255): 25 | gray = 255 26 | else: 27 | gray = int(grayImage[i,j]+50) 28 | 29 | result[i,j] = np.uint8(gray) 30 | 31 | #显示图像 32 | cv2.imshow("Gray Image", grayImage) 33 | cv2.imshow("Result", result) 34 | 35 | #等待显示 36 | cv2.waitKey(0) 37 | cv2.destroyAllWindows() 38 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_07.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('miao.png') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | 17 | #创建一幅图像 18 | result = np.zeros((height, width, 3), np.uint8) 19 | 20 | #图像对比度增强变换 DB=DA*1.5 21 | for i in range(height): 22 | for j in range(width): 23 | 24 | if (int(grayImage[i,j]*1.5) > 255): 25 | gray = 255 26 | else: 27 | gray = int(grayImage[i,j]*1.5) 28 | 29 | result[i,j] = np.uint8(gray) 30 | 31 | #显示图像 32 | cv2.imshow("Gray Image", grayImage) 33 | cv2.imshow("Result", result) 34 | 35 | #等待显示 36 | cv2.waitKey(0) 37 | cv2.destroyAllWindows() 38 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_08.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('miao.png') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | 17 | #创建一幅图像 18 | result = np.zeros((height, width, 3), np.uint8) 19 | 20 | #图像对比度减弱变换 DB=DA*0.8 21 | for i in range(height): 22 | for j in range(width): 23 | gray = int(grayImage[i,j]*0.8) 24 | result[i,j] = np.uint8(gray) 25 | 26 | #显示图像 27 | cv2.imshow("Gray Image", grayImage) 28 | cv2.imshow("Result", result) 29 | 30 | #等待显示 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_09.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取原始图像 7 | img = cv2.imread('miao.png') 8 | 9 | #图像灰度转换 10 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 11 | 12 | #获取图像高度和宽度 13 | height = grayImage.shape[0] 14 | width = grayImage.shape[1] 15 | 16 | #创建一幅图像 17 | result = np.zeros((height, width), np.uint8) 18 | 19 | #图像灰度反色变换 DB=255-DA 20 | for i in range(height): 21 | for j in range(width): 22 | gray = 255 - grayImage[i,j] 23 | result[i,j] = np.uint8(gray) 24 | 25 | #显示图像 26 | cv2.imshow("Gray Image", grayImage) 27 | cv2.imshow("Result", result) 28 | 29 | #等待显示 30 | cv2.waitKey(0) 31 | cv2.destroyAllWindows() 32 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_10.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('miao.png') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | 17 | #创建一幅图像 18 | result = np.zeros((height, width), np.uint8) 19 | 20 | #图像灰度非线性变换:DB=DA×DA/255 21 | for i in range(height): 22 | for j in range(width): 23 | gray = int(grayImage[i,j])*int(grayImage[i,j]) / 255 24 | result[i,j] = np.uint8(gray) 25 | 26 | #显示图像 27 | cv2.imshow("Gray Image", grayImage) 28 | cv2.imshow("Result", result) 29 | 30 | #等待显示 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_11(best).py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import cv2 5 | 6 | #绘制曲线 7 | def log_plot(c): 8 | x = np.arange(0, 256, 0.01) 9 | y = c * np.log(1 + x) 10 | plt.plot(x, y, 'r', linewidth=1) 11 | plt.rcParams['font.sans-serif']=['SimHei'] #正常显示中文标签 12 | plt.title(u'对数变换函数') 13 | plt.xlim(0, 255), plt.ylim(0, 255) 14 | plt.show() 15 | 16 | #对数变换 17 | def log(c, img): 18 | output = c * np.log(1.0 + img) 19 | output = np.uint8(output + 0.5) 20 | return output 21 | 22 | #读取原始图像 23 | img = cv2.imread('2016-.png') 24 | 25 | #绘制对数变换曲线 26 | log_plot(42) 27 | 28 | #图像灰度对数变换 29 | output = log(42, img) 30 | 31 | #显示图像 32 | cv2.imshow('Input', img) 33 | cv2.imshow('Output', output) 34 | cv2.waitKey(0) 35 | cv2.destroyAllWindows() 36 | 37 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_12(best).py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import cv2 5 | 6 | #绘制曲线 7 | def gamma_plot(c, v): 8 | x = np.arange(0, 256, 0.01) 9 | y = c*x**v 10 | plt.plot(x, y, 'r', linewidth=1) 11 | plt.rcParams['font.sans-serif']=['SimHei'] #正常显示中文标签 12 | plt.title('伽马变换函数') 13 | plt.xlim([0, 255]), plt.ylim([0, 255]) 14 | plt.show() 15 | 16 | #伽玛变换 17 | def gamma(img, c, v): 18 | lut = np.zeros(256, dtype=np.float32) 19 | for i in range(256): 20 | lut[i] = c * i ** v 21 | output_img = cv2.LUT(img, lut) #像素灰度值的映射 22 | output_img = np.uint8(output_img+0.5) 23 | return output_img 24 | 25 | #读取原始图像 26 | img = cv2.imread('2019.png') 27 | 28 | #绘制伽玛变换曲线 29 | gamma_plot(0.00000005, 4.0) 30 | 31 | #图像灰度伽玛变换 32 | output = gamma(img, 0.00000005, 4.0) 33 | 34 | #显示图像 35 | cv2.imshow('Imput', img) 36 | cv2.imshow('Output', output) 37 | cv2.waitKey(0) 38 | cv2.destroyAllWindows() 39 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_13.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('miao.png') 8 | 9 | #灰度图像处理 10 | GrayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 11 | 12 | #二进制阈值化处理 13 | r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_BINARY) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", b) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_14.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('miao.png') 8 | 9 | #灰度图像处理 10 | grayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 11 | 12 | #反二进制阈值化处理 13 | r, b = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY_INV) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", b) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_15.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('miao.png') 8 | 9 | #灰度图像处理 10 | grayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 11 | 12 | #截断阈值化处理 13 | r, b = cv2.threshold(grayImage, 127, 255, cv2.THRESH_TRUNC) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", b) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_16.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('miao.png') 8 | 9 | #灰度图像处理 10 | grayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 11 | 12 | #阈值化为0处理 13 | r, b = cv2.threshold(grayImage, 127, 255, cv2.THRESH_TOZERO) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", b) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_17.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('miao.png') 8 | 9 | #灰度图像处理 10 | GrayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 11 | 12 | #二进制阈值化处理 13 | r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TOZERO_INV) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", b) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_18.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img=cv2.imread('miao.png') 9 | grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 10 | 11 | #阈值化处理 12 | ret,thresh1=cv2.threshold(grayImage,127,255,cv2.THRESH_BINARY) 13 | ret,thresh2=cv2.threshold(grayImage,127,255,cv2.THRESH_BINARY_INV) 14 | ret,thresh3=cv2.threshold(grayImage,127,255,cv2.THRESH_TRUNC) 15 | ret,thresh4=cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO) 16 | ret,thresh5=cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO_INV) 17 | 18 | #显示结果 19 | titles = ['Gray Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] 20 | images = [grayImage, thresh1, thresh2, thresh3, thresh4, thresh5] 21 | for i in range(6): 22 | plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') 23 | plt.title(titles[i]) 24 | plt.xticks([]),plt.yticks([]) 25 | plt.show() 26 | -------------------------------------------------------------------------------- /chapter07-dys/chapter07_19.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | #By:Eastmount CSDN 2020-11-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | import matplotlib 7 | 8 | #读取图像 9 | img = cv2.imread('miao.png') 10 | 11 | #图像灰度化处理 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #固定值阈值化处理 15 | r, thresh1 = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY) 16 | 17 | #自适应阈值化处理 方法一 18 | thresh2 = cv2.adaptiveThreshold(grayImage, 255, 19 | cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) 20 | 21 | #自适应阈值化处理 方法二 22 | thresh3 = cv2.adaptiveThreshold(grayImage, 255, 23 | cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) 24 | 25 | #设置字体 26 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 27 | 28 | #显示图像 29 | titles = ['灰度图像', '全局阈值', '自适应平均阈值', '自适应高斯阈值'] 30 | #显示图像 31 | titles = ['Gray Image', 32 | 'Global Thresholding', 33 | 'Adaptive Mean Thresholding', 34 | 'Adaptive Gaussian Thresholding'] 35 | images = [grayImage, thresh1, thresh2, thresh3] 36 | for i in range(4): 37 | plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray') 38 | plt.title(titles[i]) 39 | plt.xticks([]),plt.yticks([]) 40 | plt.show() 41 | -------------------------------------------------------------------------------- /chapter07-dys/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/figure_1.png -------------------------------------------------------------------------------- /chapter07-dys/figure_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/figure_13.png -------------------------------------------------------------------------------- /chapter07-dys/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/figure_2.png -------------------------------------------------------------------------------- /chapter07-dys/figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/figure_4.png -------------------------------------------------------------------------------- /chapter07-dys/figure_dd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/figure_dd.png -------------------------------------------------------------------------------- /chapter07-dys/figure_duishu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/figure_duishu.png -------------------------------------------------------------------------------- /chapter07-dys/miao.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/miao.jpg -------------------------------------------------------------------------------- /chapter07-dys/miao.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter07-dys/miao.png -------------------------------------------------------------------------------- /chapter08-histogram/Figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/Figure_1.png -------------------------------------------------------------------------------- /chapter08-histogram/Figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/Figure_2.png -------------------------------------------------------------------------------- /chapter08-histogram/Figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/Figure_3.png -------------------------------------------------------------------------------- /chapter08-histogram/Figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/Figure_4.png -------------------------------------------------------------------------------- /chapter08-histogram/Lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/Lena.png -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-01.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | src = cv2.imread('lena.bmp') 9 | 10 | #绘制直方图 11 | #plt.hist(src.ravel(), 256) 12 | plt.hist(src.ravel(), bins=256, density=1, facecolor='green', alpha=0.75) 13 | plt.xlabel("x") 14 | plt.ylabel("y") 15 | plt.show() 16 | 17 | #显示原始图像 18 | cv2.imshow("src", src) 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-02.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | src = cv2.imread('Lena.png') 9 | 10 | #获取BGR三个通道的像素值 11 | b, g, r = cv2.split(src) 12 | print(r,g,b) 13 | 14 | #绘制直方图 15 | plt.figure("Lena") 16 | #蓝色分量 17 | plt.hist(b.ravel(), bins=256, density=1, facecolor='b', edgecolor='b', alpha=0.75) 18 | #绿色分量 19 | plt.hist(g.ravel(), bins=256, density=1, facecolor='g', edgecolor='g', alpha=0.75) 20 | #红色分量 21 | plt.hist(r.ravel(), bins=256, density=1, facecolor='r', edgecolor='r', alpha=0.75) 22 | plt.xlabel("x") 23 | plt.ylabel("y") 24 | plt.show() 25 | 26 | #显示原始图像 27 | cv2.imshow("src", src) 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-03.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | import matplotlib 7 | 8 | #读取图像 9 | src = cv2.imread('Lena.png') 10 | 11 | #转换为RGB图像 12 | img_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) 13 | 14 | #获取BGR三个通道的像素值 15 | b, g, r = cv2.split(src) 16 | print(r,g,b) 17 | 18 | plt.figure(figsize=(8, 6)) 19 | 20 | #设置字体 21 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 22 | 23 | #原始图像 24 | plt.subplot(221) 25 | plt.imshow(img_rgb) 26 | plt.axis('off') 27 | plt.title("(a)原图像") 28 | 29 | #绘制蓝色分量直方图 30 | plt.subplot(222) 31 | plt.hist(b.ravel(), bins=256, density=1, facecolor='b', edgecolor='b', alpha=0.75) 32 | plt.xlabel("x") 33 | plt.ylabel("y") 34 | plt.title("(b)蓝色分量直方图") 35 | 36 | #绘制绿色分量直方图 37 | plt.subplot(223) 38 | plt.hist(g.ravel(), bins=256, density=1, facecolor='g', edgecolor='g', alpha=0.75) 39 | plt.xlabel("x") 40 | plt.ylabel("y") 41 | plt.title("(c)绿色分量直方图") 42 | 43 | #绘制红色分量直方图 44 | plt.subplot(224) 45 | plt.hist(r.ravel(), bins=256, density=1, facecolor='r', edgecolor='r', alpha=0.75) 46 | plt.xlabel("x") 47 | plt.ylabel("y") 48 | plt.title("(d)红色分量直方图") 49 | plt.show() 50 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-04.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | import matplotlib 7 | 8 | #读取图像 9 | src = cv2.imread('lena.bmp') 10 | 11 | #计算256灰度级的图像直方图 12 | hist = cv2.calcHist([src], [0], None, [256], [0,255]) 13 | 14 | #输出直方图大小、形状、数量 15 | print(hist.size) 16 | print(hist.shape) 17 | print(hist) 18 | 19 | #设置字体 20 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 21 | 22 | #显示原始图像和绘制的直方图 23 | plt.subplot(121) 24 | plt.imshow(src, 'gray') 25 | plt.axis('off') 26 | plt.title("(a)Lena灰度图像") 27 | 28 | plt.subplot(122) 29 | plt.plot(hist, color='r') 30 | plt.xlabel("x") 31 | plt.ylabel("y") 32 | plt.title("(b)直方图曲线") 33 | plt.show() 34 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-05.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | import matplotlib 7 | 8 | #读取图像 9 | src = cv2.imread('Lena.png') 10 | 11 | #转换为RGB图像 12 | img_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) 13 | 14 | #计算直方图 15 | histb = cv2.calcHist([src], [0], None, [256], [0,255]) 16 | histg = cv2.calcHist([src], [1], None, [256], [0,255]) 17 | histr = cv2.calcHist([src], [2], None, [256], [0,255]) 18 | 19 | #设置字体 20 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 21 | 22 | #显示原始图像和绘制的直方图 23 | plt.subplot(121) 24 | plt.imshow(img_rgb, 'gray') 25 | plt.axis('off') 26 | plt.title("(a)Lena原始图像") 27 | 28 | plt.subplot(122) 29 | plt.plot(histb, color='b') 30 | plt.plot(histg, color='g') 31 | plt.plot(histr, color='r') 32 | plt.xlabel("x") 33 | plt.ylabel("y") 34 | plt.title("(b)直方图曲线") 35 | plt.show() 36 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-06.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | import matplotlib 7 | 8 | #读取图像 9 | img = cv2.imread('yxz.png') 10 | 11 | #转换为RGB图像 12 | img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 13 | 14 | #设置掩膜 15 | mask = np.zeros(img.shape[:2], np.uint8) 16 | mask[100:300, 100:300] = 255 17 | masked_img = cv2.bitwise_and(img, img, mask=mask) 18 | 19 | #图像直方图计算 20 | hist_full = cv2.calcHist([img], [0], None, [256], [0,256]) #通道[0]-灰度图 21 | 22 | #图像直方图计算(含掩膜) 23 | hist_mask = cv2.calcHist([img], [0], mask, [256], [0,256]) 24 | 25 | plt.figure(figsize=(8, 6)) 26 | 27 | #设置字体 28 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 29 | 30 | #原始图像 31 | plt.subplot(221) 32 | plt.imshow(img_rgb, 'gray') 33 | plt.axis('off') 34 | plt.title("(a)原始图像") 35 | 36 | #绘制掩膜 37 | plt.subplot(222) 38 | plt.imshow(mask, 'gray') 39 | plt.axis('off') 40 | plt.title("(b)掩膜") 41 | 42 | #绘制掩膜设置后的图像 43 | plt.subplot(223) 44 | plt.imshow(masked_img, 'gray') 45 | plt.axis('off') 46 | plt.title("(c)图像掩膜处理") 47 | 48 | #绘制直方图 49 | plt.subplot(224) 50 | plt.plot(hist_full) 51 | plt.plot(hist_mask) 52 | plt.title("(d)直方图曲线") 53 | plt.xlabel("x") 54 | plt.ylabel("y") 55 | plt.show() 56 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-07.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.bmp') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | result = np.zeros((height, width), np.uint8) 17 | 18 | #图像灰度上移变换 DB=DA+50 19 | for i in range(height): 20 | for j in range(width): 21 | if (int(grayImage[i,j]+50) > 255): 22 | gray = 255 23 | else: 24 | gray = int(grayImage[i,j]+50) 25 | 26 | result[i,j] = np.uint8(gray) 27 | 28 | #计算原图的直方图 29 | hist = cv2.calcHist([img], [0], None, [256], [0,255]) 30 | 31 | #计算灰度变换的直方图 32 | hist_res = cv2.calcHist([result], [0], None, [256], [0,255]) 33 | 34 | #原始图像 35 | plt.figure(figsize=(8, 6)) 36 | plt.subplot(221), plt.imshow(img, 'gray'), plt.title("(a)"), plt.axis('off') 37 | 38 | #绘制掩膜 39 | plt.subplot(222), plt.plot(hist), plt.title("(b)"), plt.xlabel("x"), plt.ylabel("y") 40 | 41 | #绘制掩膜设置后的图像 42 | plt.subplot(223), plt.imshow(result, 'gray'), plt.title("(c)"), plt.axis('off') 43 | 44 | #绘制直方图 45 | plt.subplot(224), plt.plot(hist_res), plt.title("(d)"), plt.xlabel("x"), plt.ylabel("y") 46 | plt.show() 47 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-08.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.bmp') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | result = np.zeros((height, width), np.uint8) 17 | 18 | #图像对比度减弱变换 DB=DA×0.8 19 | for i in range(height): 20 | for j in range(width): 21 | gray = int(grayImage[i,j]*0.8) 22 | result[i,j] = np.uint8(gray) 23 | 24 | #计算原图的直方图 25 | hist = cv2.calcHist([img], [0], None, [256], [0,255]) 26 | 27 | #计算灰度变换的直方图 28 | hist_res = cv2.calcHist([result], [0], None, [256], [0,255]) 29 | 30 | #原始图像 31 | plt.figure(figsize=(8, 6)) 32 | plt.subplot(221), plt.imshow(img, 'gray'), plt.title("(a)"), plt.axis('off') 33 | 34 | #绘制掩膜 35 | plt.subplot(222), plt.plot(hist), plt.title("(b)"), plt.xlabel("x"), plt.ylabel("y") 36 | 37 | #绘制掩膜设置后的图像 38 | plt.subplot(223), plt.imshow(result, 'gray'), plt.title("(c)"), plt.axis('off') 39 | 40 | #绘制直方图 41 | plt.subplot(224), plt.plot(hist_res), plt.title("(d)"), plt.xlabel("x"), plt.ylabel("y") 42 | plt.show() 43 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-09.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.bmp') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | result = np.zeros((height, width), np.uint8) 17 | 18 | #图像灰度反色变换 DB=255-DA 19 | for i in range(height): 20 | for j in range(width): 21 | gray = 255 - grayImage[i,j] 22 | result[i,j] = np.uint8(gray) 23 | 24 | #计算原图的直方图 25 | hist = cv2.calcHist([img], [0], None, [256], [0,255]) 26 | 27 | #计算灰度变换的直方图 28 | hist_res = cv2.calcHist([result], [0], None, [256], [0,255]) 29 | 30 | #原始图像 31 | plt.figure(figsize=(8, 6)) 32 | plt.subplot(221), plt.imshow(img, 'gray'), plt.title("(a)"), plt.axis('off') 33 | 34 | #绘制掩膜 35 | plt.subplot(222), plt.plot(hist), plt.title("(b)"), plt.xlabel("x"), plt.ylabel("y") 36 | 37 | #绘制掩膜设置后的图像 38 | plt.subplot(223), plt.imshow(result, 'gray'), plt.title("(c)"), plt.axis('off') 39 | 40 | #绘制直方图 41 | plt.subplot(224), plt.plot(hist_res), plt.title("(d)"), plt.xlabel("x"), plt.ylabel("y") 42 | plt.show() 43 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-10.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.bmp') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #获取图像高度和宽度 14 | height = grayImage.shape[0] 15 | width = grayImage.shape[1] 16 | result = np.zeros((height, width), np.uint8) 17 | 18 | #图像灰度对数变换 19 | for i in range(height): 20 | for j in range(width): 21 | gray = 42 * np.log(1.0 + grayImage[i,j]) 22 | result[i,j] = np.uint8(gray) 23 | 24 | #计算原图的直方图 25 | hist = cv2.calcHist([img], [0], None, [256], [0,255]) 26 | 27 | #计算灰度变换的直方图 28 | hist_res = cv2.calcHist([result], [0], None, [256], [0,255]) 29 | 30 | #原始图像 31 | plt.figure(figsize=(8, 6)) 32 | plt.subplot(221), plt.imshow(img, 'gray'), plt.title("(a)"), plt.axis('off') 33 | 34 | #绘制原始图像直方图 35 | plt.subplot(222), plt.plot(hist), plt.title("(b)"), plt.xlabel("x"), plt.ylabel("y") 36 | 37 | #灰度变换后的图像 38 | plt.subplot(223), plt.imshow(result, 'gray'), plt.title("(c)"), plt.axis('off') 39 | 40 | #灰度变换图像的直方图 41 | plt.subplot(224), plt.plot(hist_res), plt.title("(d)"), plt.xlabel("x"), plt.ylabel("y") 42 | plt.show() 43 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-11.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.bmp') 9 | 10 | #图像灰度转换 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #二进制阈值化处理 14 | r, result = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY) 15 | 16 | #计算原图的直方图 17 | hist = cv2.calcHist([img], [0], None, [256], [0,256]) 18 | 19 | #计算阈值化处理的直方图 20 | hist_res = cv2.calcHist([result], [0], None, [256], [0,256]) 21 | 22 | #原始图像 23 | plt.figure(figsize=(8, 6)) 24 | plt.subplot(221), plt.imshow(img, 'gray'), plt.title("(a)"), plt.axis('off') 25 | 26 | #绘制原始图像直方图 27 | plt.subplot(222), plt.plot(hist), plt.title("(b)"), plt.xlabel("x"), plt.ylabel("y") 28 | 29 | #阈值化处理后的图像 30 | plt.subplot(223), plt.imshow(result, 'gray'), plt.title("(c)"), plt.axis('off') 31 | 32 | #阈值化处理图像的直方图 33 | plt.subplot(224), plt.plot(hist_res), plt.title("(d)"), plt.xlabel("x"), plt.ylabel("y") 34 | plt.show() 35 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-12.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('Lena.png') 9 | 10 | #转换为RGB图像 11 | img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 12 | 13 | #图像HSV转换 14 | hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) 15 | 16 | #计算H-S直方图 17 | hist = cv2.calcHist(hsv, [0,1], None, [180,256], [0,180,0,256]) 18 | 19 | #原始图像 20 | plt.figure(figsize=(8, 6)) 21 | plt.subplot(121), plt.imshow(img_rgb, 'gray'), plt.title("(a)"), plt.axis('off') 22 | 23 | #绘制H-S直方图 24 | plt.subplot(122), plt.imshow(hist, interpolation='nearest'), plt.title("(b)") 25 | plt.xlabel("x"), plt.ylabel("y") 26 | plt.show() 27 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-13.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #函数: 获取图像的灰度平均值 8 | def fun_mean(img, height, width): 9 | sum_img = 0 10 | for i in range(height): 11 | for j in range(width): 12 | sum_img = sum_img + int(img[i,j]) 13 | mean = sum_img / (height * width) 14 | return mean 15 | 16 | #函数: 获取中位数 17 | def fun_median(data): 18 | length = len(data) 19 | data.sort() 20 | if (length % 2)== 1: 21 | z = length // 2 22 | y = data[z] 23 | else: 24 | y = (int(data[length//2]) + int(data[length//2-1])) / 2 25 | return y 26 | 27 | #读取图像 28 | img = cv2.imread('lena.bmp') 29 | 30 | #图像灰度转换 31 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 32 | 33 | #获取图像高度和宽度 34 | height = grayImage.shape[0] 35 | width = grayImage.shape[1] 36 | 37 | #计算图像的灰度平均值 38 | mean = fun_mean(grayImage, height, width) 39 | print("灰度平均值:", mean) 40 | 41 | #计算图像的灰度中位数 42 | value = grayImage.ravel() #获取所有像素值 43 | median = fun_median(value) 44 | print("灰度中值:", median) 45 | 46 | #计算图像的灰度标准差 47 | std = np.std(value, ddof = 1) 48 | print("灰度标准差", std) 49 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-14.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-02-05 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #函数: 判断黑夜或白天 8 | def func_judge(img): 9 | #获取图像高度和宽度 10 | height = grayImage.shape[0] 11 | width = grayImage.shape[1] 12 | piexs_sum = height * width 13 | dark_sum = 0 #偏暗像素个数 14 | dark_prop = 0 #偏暗像素所占比例 15 | 16 | for i in range(height): 17 | for j in range(width): 18 | if img[i, j] < 50: #阈值为50 19 | dark_sum += 1 20 | 21 | #计算比例 22 | print(dark_sum) 23 | print(piexs_sum) 24 | dark_prop = dark_sum * 1.0 / piexs_sum 25 | if dark_prop >=0.8: 26 | print("This picture is dark!", dark_prop) 27 | else: 28 | print("This picture is bright!", dark_prop) 29 | 30 | #读取图像 31 | img = cv2.imread('day.png') 32 | 33 | #转换为RGB图像 34 | img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 35 | 36 | #图像灰度转换 37 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 38 | 39 | #计算256灰度级的图像直方图 40 | hist = cv2.calcHist([grayImage], [0], None, [256], [0,255]) 41 | 42 | #判断黑夜或白天 43 | func_judge(grayImage) 44 | 45 | #显示原始图像和绘制的直方图 46 | plt.subplot(121), plt.imshow(img_rgb, 'gray'), plt.axis('off'), plt.title("(a)") 47 | plt.subplot(122), plt.plot(hist, color='r'), plt.xlabel("x"), plt.ylabel("y"), plt.title("(b)") 48 | plt.show() 49 | -------------------------------------------------------------------------------- /chapter08-histogram/chapter08-15-3d.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | import cv2 as cv 4 | import matplotlib.pyplot as plt 5 | from mpl_toolkits.mplot3d import Axes3D 6 | from matplotlib import cm 7 | from matplotlib.ticker import LinearLocator, FormatStrFormatter 8 | 9 | #读取图像 10 | img = cv.imread("yxz.png") 11 | img = cv.cvtColor(img,cv.COLOR_BGR2GRAY) 12 | imgd = np.array(img) #image类转numpy 13 | 14 | #准备数据 15 | sp = img.shape 16 | h = int(sp[0]) #图像高度(rows) 17 | w = int(sp[1]) #图像宽度(colums) of image 18 | 19 | #绘图初始处理 20 | fig = plt.figure(figsize=(16,12)) 21 | ax = fig.gca(projection="3d") 22 | 23 | x = np.arange(0, w, 1) 24 | y = np.arange(0, h, 1) 25 | x, y = np.meshgrid(x,y) 26 | z = imgd 27 | surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) 28 | 29 | #自定义z轴 30 | ax.set_zlim(-10, 255) 31 | ax.zaxis.set_major_locator(LinearLocator(10)) #设置z轴网格线的疏密 32 | #将z的value字符串转为float并保留2位小数 33 | ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 34 | 35 | # 设置坐标轴的label和标题 36 | ax.set_xlabel('x', size=15) 37 | ax.set_ylabel('y', size=15) 38 | ax.set_zlabel('z', size=15) 39 | ax.set_title("surface plot", weight='bold', size=20) 40 | 41 | #添加右侧的色卡条 42 | fig.colorbar(surf, shrink=0.6, aspect=8) 43 | plt.show() 44 | -------------------------------------------------------------------------------- /chapter08-histogram/dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/dark.png -------------------------------------------------------------------------------- /chapter08-histogram/day.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/day.png -------------------------------------------------------------------------------- /chapter08-histogram/lena.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/lena.bmp -------------------------------------------------------------------------------- /chapter08-histogram/yxz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/yxz.png -------------------------------------------------------------------------------- /chapter08-histogram/yxz_jhh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/yxz_jhh.png -------------------------------------------------------------------------------- /chapter08-histogram/yxz_sw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter08-histogram/yxz_sw.png -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/2016-.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/2016-.png -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/Ice.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/Ice.jpg -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/chapter09-01.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-03-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('test.png') 9 | 10 | #灰度转换 11 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #直方图均衡化处理 14 | result = cv2.equalizeHist(gray) 15 | 16 | #显示图像 17 | cv2.imshow("Input", gray) 18 | cv2.imshow("Result", result) 19 | cv2.waitKey(0) 20 | cv2.destroyAllWindows() 21 | 22 | -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/chapter09-02.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-03-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.bmp') 9 | 10 | #灰度转换 11 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #直方图均衡化处理 14 | result = cv2.equalizeHist(gray) 15 | 16 | #显示图像 17 | plt.subplot(221) 18 | plt.imshow(gray, cmap=plt.cm.gray), plt.axis("off"), plt.title('(a)') 19 | plt.subplot(222) 20 | plt.imshow(result, cmap=plt.cm.gray), plt.axis("off"), plt.title('(b)') 21 | plt.subplot(223) 22 | plt.hist(img.ravel(), 256), plt.title('(c)') 23 | plt.subplot(224) 24 | plt.hist(result.ravel(), 256), plt.title('(d)') 25 | plt.show() 26 | -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/chapter09-03.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-03-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('yxz.jpg') 9 | 10 | # 彩色图像均衡化 需要分解通道 对每一个通道均衡化 11 | (b, g, r) = cv2.split(img) 12 | bH = cv2.equalizeHist(b) 13 | gH = cv2.equalizeHist(g) 14 | rH = cv2.equalizeHist(r) 15 | 16 | # 合并每一个通道 17 | result = cv2.merge((bH, gH, rH)) 18 | cv2.imshow("Input", img) 19 | cv2.imshow("Result", result) 20 | 21 | #等待显示 22 | cv2.waitKey(0) 23 | cv2.destroyAllWindows() 24 | 25 | #绘制直方图 26 | plt.figure("Hist") 27 | #蓝色分量 28 | plt.hist(bH.ravel(), bins=256, normed=1, facecolor='b', edgecolor='b') 29 | #绿色分量 30 | plt.hist(gH.ravel(), bins=256, normed=1, facecolor='g', edgecolor='g') 31 | #红色分量 32 | plt.hist(rH.ravel(), bins=256, normed=1, facecolor='r', edgecolor='r') 33 | plt.xlabel("x") 34 | plt.ylabel("y") 35 | plt.show() 36 | -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/chapter09-04.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-03-12 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.bmp') 9 | 10 | #灰度转换 11 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #局部直方图均衡化处理 14 | clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(10,10)) 15 | 16 | #将灰度图像和局部直方图相关联, 把直方图均衡化应用到灰度图 17 | result = clahe.apply(gray) 18 | 19 | #显示图像 20 | plt.subplot(221) 21 | plt.imshow(gray, cmap=plt.cm.gray), plt.axis("off"), plt.title('(a)') 22 | plt.subplot(222) 23 | plt.imshow(result, cmap=plt.cm.gray), plt.axis("off"), plt.title('(b)') 24 | plt.subplot(223) 25 | plt.hist(img.ravel(), 256), plt.title('(c)') 26 | plt.subplot(224) 27 | plt.hist(result.ravel(), 256), plt.title('(d)') 28 | plt.show() 29 | -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/chapter09-05.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-03-12 3 | # 参考zmshy2128老师文章 4 | import cv2 5 | import numpy as np 6 | import math 7 | import matplotlib.pyplot as plt 8 | 9 | #线性拉伸处理 10 | #去掉最大最小0.5%的像素值 线性拉伸至[0,1] 11 | def stretchImage(data, s=0.005, bins = 2000): 12 | ht = np.histogram(data, bins); 13 | d = np.cumsum(ht[0])/float(data.size) 14 | lmin = 0; lmax=bins-1 15 | while lmin=s: 17 | break 18 | lmin+=1 19 | while lmax>=0: 20 | if d[lmax]<=1-s: 21 | break 22 | lmax-=1 23 | return np.clip((data-ht[1][lmin])/(ht[1][lmax]-ht[1][lmin]), 0,1) 24 | 25 | #根据半径计算权重参数矩阵 26 | g_para = {} 27 | def getPara(radius = 5): 28 | global g_para 29 | m = g_para.get(radius, None) 30 | if m is not None: 31 | return m 32 | size = radius*2+1 33 | m = np.zeros((size, size)) 34 | for h in range(-radius, radius+1): 35 | for w in range(-radius, radius+1): 36 | if h==0 and w==0: 37 | continue 38 | m[radius+h, radius+w] = 1.0/math.sqrt(h**2+w**2) 39 | m /= m.sum() 40 | g_para[radius] = m 41 | return m 42 | 43 | #常规的ACE实现 44 | def zmIce(I, ratio=4, radius=300): 45 | para = getPara(radius) 46 | height,width = I.shape 47 | 48 | #Python3报错 列表append 49 | """ 50 | #TypeError: can only concatenate list (not "range") to list 51 | #增加max 52 | print(range(height),type(range(height))) 53 | print([0]*radius,type([0]*radius)) 54 | print([height-1]*radius,type([height-1]*radius)) 55 | zh = [0]*radius + max(range(height)) + [height-1]*radius 56 | zw = [0]*radius + max(range(width)) + [width -1]*radius 57 | #[0,0,0] + [0,1,2,3] + [3,3,3] 58 | #[0, 0, 0, 0, 1, 2, 3, 3, 3, 3] 59 | """ 60 | 61 | zh = [] 62 | zw = [] 63 | n = 0 64 | while n < radius: 65 | zh.append(0) 66 | zw.append(0) 67 | n += 1 68 | for n in range(height): 69 | zh.append(n) 70 | for n in range(width): 71 | zw.append(n) 72 | n = 0 73 | while n < radius: 74 | zh.append(height-1) 75 | zw.append(width-1) 76 | n += 1 77 | 78 | #print(zh) 79 | #print(zw) 80 | 81 | Z = I[np.ix_(zh, zw)] 82 | res = np.zeros(I.shape) 83 | for h in range(radius*2+1): 84 | for w in range(radius*2+1): 85 | if para[h][w] == 0: 86 | continue 87 | res += (para[h][w] * np.clip((I-Z[h:h+height, w:w+width])*ratio, -1, 1)) 88 | return res 89 | 90 | #单通道ACE快速增强实现 91 | def zmIceFast(I, ratio, radius): 92 | print(I) 93 | height, width = I.shape[:2] 94 | if min(height, width) <=2: 95 | return np.zeros(I.shape)+0.5 96 | Rs = cv2.resize(I, (int((width+1)/2), int((height+1)/2))) 97 | Rf = zmIceFast(Rs, ratio, radius) #递归调用 98 | Rf = cv2.resize(Rf, (width, height)) 99 | Rs = cv2.resize(Rs, (width, height)) 100 | 101 | return Rf+zmIce(I,ratio, radius)-zmIce(Rs,ratio,radius) 102 | 103 | #rgb三通道分别增强 ratio是对比度增强因子 radius是卷积模板半径 104 | def zmIceColor(I, ratio=4, radius=3): 105 | res = np.zeros(I.shape) 106 | for k in range(3): 107 | res[:,:,k] = stretchImage(zmIceFast(I[:,:,k], ratio, radius)) 108 | return res 109 | 110 | #主函数 111 | if __name__ == '__main__': 112 | img = cv2.imread('test02.png') 113 | res = zmIceColor(img/255.0)*255 114 | cv2.imwrite('Ice.jpg', res) 115 | -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/figure_1.png -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/lena.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/lena.bmp -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/test.png -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/test01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/test01.png -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/test02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/test02.png -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/yxz.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/yxz.jpg -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/zmIce.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/zmIce.jpg -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/zmIce1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/zmIce1.jpg -------------------------------------------------------------------------------- /chapter09-ImageEnhancement/zmIce2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter09-ImageEnhancement/zmIce2.jpg -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-01-blur.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #均值滤波 12 | result = cv2.blur(source, (10,10)) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = ['原始图像', '均值滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-02-boxFilter.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #方框滤波 12 | result = cv2.boxFilter(source, -1, (3,3), normalize=0) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = ['原始图像', '方框滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-03-boxFilter.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #方框滤波 12 | result = cv2.boxFilter(source, -1, (3,3), normalize=1) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = ['原始图像', '方框滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-04-gauss.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #高斯滤波 12 | result = cv2.GaussianBlur(source, (7,7), 1.8) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = ['原始图像', '高斯滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-05-median.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('lena.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #中值滤波 12 | result = cv2.medianBlur(source, 3) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = ['原始图像', '中值滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-06-bilateral.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('te.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #双边滤波 12 | result = cv2.bilateralFilter(source, 15, 150, 150) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = ['原始图像', '双边滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-07-all.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('te.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #均值滤波 12 | result1 = cv2.blur(source, (5,5)) 13 | result2 = cv2.blur(source, (10,10)) 14 | 15 | #方框滤波 16 | result3 = cv2.boxFilter(source, -1, (5,5), normalize=1) 17 | result4 = cv2.boxFilter(source, -1, (2,2), normalize=0) 18 | 19 | #高斯滤波 20 | result5 = cv2.GaussianBlur(source, (3,3), 0) 21 | result6 = cv2.GaussianBlur(source, (15,15), 0) 22 | 23 | #中值滤波 24 | result7 = cv2.medianBlur(source, 3) 25 | 26 | #高斯双边滤波 27 | result8 =cv2.bilateralFilter(source, 15, 150, 150) 28 | 29 | #显示图形 30 | titles = ['Source', 'Blur 5*5', 'Blur 10*10', 'BoxFilter 5*5', 31 | 'BoxFilter 2*2', 'GaussianBlur 3*3', 'GaussianBlur 15*15', 32 | 'medianBlur', 'bilateralFilter'] 33 | images = [source, result1, result2, result3, 34 | result4, result5, result6, result7, result8] 35 | for i in range(9): 36 | plt.subplot(3,3,i+1), plt.imshow(images[i], 'gray') 37 | plt.title(titles[i]) 38 | plt.xticks([]),plt.yticks([]) 39 | plt.show() 40 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-08-all2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # By:Eastmount CSDN 2021-06-07 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('test01_yn.png') 9 | source = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #中值滤波 12 | result1 = cv2.medianBlur(source, 3) 13 | 14 | #高斯双边滤波 15 | result2 =cv2.bilateralFilter(source, 15, 150, 150) 16 | 17 | #均值迁移 18 | result3 = cv2.pyrMeanShiftFiltering(source, 20, 50) 19 | 20 | #用来正常显示中文标签 21 | plt.rcParams['font.sans-serif']=['SimHei'] 22 | 23 | #显示图形 24 | titles = ['原始图像', '中值滤波', '双边滤波', '均值迁移'] 25 | images = [source, result1, result2, result3] 26 | for i in range(4): 27 | plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray') 28 | plt.title(titles[i]) 29 | plt.xticks([]),plt.yticks([]) 30 | plt.show() 31 | -------------------------------------------------------------------------------- /chapter10-smoth/chapter10-09-noise.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | 5 | #读取图片 6 | img = cv2.imread("yn.png", cv2.IMREAD_UNCHANGED) 7 | rows, cols, chn = img.shape 8 | 9 | #加噪声 10 | for i in range(5000): 11 | x = np.random.randint(0, rows) 12 | y = np.random.randint(0, cols) 13 | img[x,y,:] = 255 14 | 15 | cv2.imshow("noise", img) 16 | 17 | #等待显示 18 | cv2.waitKey(0) 19 | cv2.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /chapter10-smoth/figure_gaosi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/figure_gaosi.png -------------------------------------------------------------------------------- /chapter10-smoth/figure_shuangbian.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/figure_shuangbian.png -------------------------------------------------------------------------------- /chapter10-smoth/figure_zhongzhi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/figure_zhongzhi.png -------------------------------------------------------------------------------- /chapter10-smoth/lena-yt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/lena-yt.png -------------------------------------------------------------------------------- /chapter10-smoth/lena.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/lena.jpg -------------------------------------------------------------------------------- /chapter10-smoth/lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/lena.png -------------------------------------------------------------------------------- /chapter10-smoth/lena2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/lena2.png -------------------------------------------------------------------------------- /chapter10-smoth/picture.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/picture.bmp -------------------------------------------------------------------------------- /chapter10-smoth/te.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/te.png -------------------------------------------------------------------------------- /chapter10-smoth/testyxz.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter10-smoth/testyxz.jpg -------------------------------------------------------------------------------- /chapter11-ruihua/Lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/Lena.png -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-01-roberts.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #Roberts算子 15 | kernelx = np.array([[-1,0],[0,1]], dtype=int) 16 | kernely = np.array([[0,-1],[1,0]], dtype=int) 17 | x = cv2.filter2D(grayImage, cv2.CV_16S, kernelx) 18 | y = cv2.filter2D(grayImage, cv2.CV_16S, kernely) 19 | #转uint8 20 | absX = cv2.convertScaleAbs(x) 21 | absY = cv2.convertScaleAbs(y) 22 | Roberts = cv2.addWeighted(absX,0.5,absY,0.5,0) 23 | 24 | #用来正常显示中文标签 25 | plt.rcParams['font.sans-serif']=['SimHei'] 26 | 27 | #显示图形 28 | titles = [u'原始图像', u'Roberts算子'] 29 | images = [lenna_img, Roberts] 30 | for i in range(2): 31 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 32 | plt.title(titles[i]) 33 | plt.xticks([]),plt.yticks([]) 34 | plt.show() 35 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-02-prewitt.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #Prewitt算子 15 | kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int) 16 | kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int) 17 | x = cv2.filter2D(grayImage, cv2.CV_16S, kernelx) 18 | y = cv2.filter2D(grayImage, cv2.CV_16S, kernely) 19 | #转uint8 20 | absX = cv2.convertScaleAbs(x) 21 | absY = cv2.convertScaleAbs(y) 22 | Prewitt = cv2.addWeighted(absX,0.5,absY,0.5,0) 23 | 24 | #用来正常显示中文标签 25 | plt.rcParams['font.sans-serif']=['SimHei'] 26 | 27 | #显示图形 28 | titles = [u'原始图像', u'Prewitt算子'] 29 | images = [lenna_img, Prewitt] 30 | for i in range(2): 31 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 32 | plt.title(titles[i]) 33 | plt.xticks([]),plt.yticks([]) 34 | plt.show() 35 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-03-sobel.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #Sobel算子 15 | x = cv2.Sobel(grayImage, cv2.CV_16S, 1, 0) #对x求一阶导 16 | y = cv2.Sobel(grayImage, cv2.CV_16S, 0, 1) #对y求一阶导 17 | absX = cv2.convertScaleAbs(x) 18 | absY = cv2.convertScaleAbs(y) 19 | Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 20 | 21 | #用来正常显示中文标签 22 | plt.rcParams['font.sans-serif']=['SimHei'] 23 | 24 | #显示图形 25 | titles = [u'原始图像', u'Sobel算子'] 26 | images = [lenna_img, Sobel] 27 | for i in range(2): 28 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 29 | plt.title(titles[i]) 30 | plt.xticks([]),plt.yticks([]) 31 | plt.show() 32 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-04-laplacian.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #拉普拉斯算法 15 | dst = cv2.Laplacian(grayImage, cv2.CV_16S, ksize = 3) 16 | Laplacian = cv2.convertScaleAbs(dst) 17 | 18 | #用来正常显示中文标签 19 | plt.rcParams['font.sans-serif']=['SimHei'] 20 | 21 | #显示图形 22 | titles = [u'原始图像', u'Laplacian算子'] 23 | images = [lenna_img, Laplacian] 24 | for i in range(2): 25 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 26 | plt.title(titles[i]) 27 | plt.xticks([]),plt.yticks([]) 28 | plt.show() 29 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-05-laplacian.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #高斯滤波 15 | gaussianBlur = cv2.GaussianBlur(grayImage, (3,3), 0) 16 | 17 | #阈值处理 18 | ret, binary = cv2.threshold(gaussianBlur, 127, 255, cv2.THRESH_BINARY) 19 | 20 | #Roberts算子 21 | kernelx = np.array([[-1,0],[0,1]], dtype=int) 22 | kernely = np.array([[0,-1],[1,0]], dtype=int) 23 | x = cv2.filter2D(binary, cv2.CV_16S, kernelx) 24 | y = cv2.filter2D(binary, cv2.CV_16S, kernely) 25 | absX = cv2.convertScaleAbs(x) 26 | absY = cv2.convertScaleAbs(y) 27 | Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 28 | 29 | #Prewitt算子 30 | kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int) 31 | kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int) 32 | x = cv2.filter2D(binary, cv2.CV_16S, kernelx) 33 | y = cv2.filter2D(binary, cv2.CV_16S, kernely) 34 | absX = cv2.convertScaleAbs(x) 35 | absY = cv2.convertScaleAbs(y) 36 | Prewitt = cv2.addWeighted(absX,0.5,absY,0.5,0) 37 | 38 | #Sobel算子 39 | x = cv2.Sobel(binary, cv2.CV_16S, 1, 0) 40 | y = cv2.Sobel(binary, cv2.CV_16S, 0, 1) 41 | absX = cv2.convertScaleAbs(x) 42 | absY = cv2.convertScaleAbs(y) 43 | Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 44 | 45 | #拉普拉斯算法 46 | dst = cv2.Laplacian(binary, cv2.CV_16S, ksize = 3) 47 | Laplacian = cv2.convertScaleAbs(dst) 48 | 49 | #效果图 50 | titles = ['Source Image', 'Binary Image', 'Roberts Image', 51 | 'Prewitt Image','Sobel Image', 'Laplacian Image'] 52 | images = [lenna_img, binary, Roberts, Prewitt, Sobel, Laplacian] 53 | for i in np.arange(6): 54 | plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') 55 | plt.title(titles[i]) 56 | plt.xticks([]),plt.yticks([]) 57 | plt.show() 58 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-06-scharr.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | # Scharr算子 15 | x = cv2.Scharr(grayImage, cv2.CV_32F, 1, 0) #X方向 16 | y = cv2.Scharr(grayImage, cv2.CV_32F, 0, 1) #Y方向 17 | absX = cv2.convertScaleAbs(x) 18 | absY = cv2.convertScaleAbs(y) 19 | Scharr = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 20 | 21 | #用来正常显示中文标签 22 | plt.rcParams['font.sans-serif']=['SimHei'] 23 | 24 | #显示图形 25 | titles = [u'原始图像', u'Scharr算子'] 26 | images = [lenna_img, Scharr] 27 | for i in range(2): 28 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 29 | plt.title(titles[i]) 30 | plt.xticks([]),plt.yticks([]) 31 | plt.show() 32 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-07-canny.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #高斯滤波降噪 15 | gaussian = cv2.GaussianBlur(grayImage, (3,3), 0) 16 | 17 | #Canny算子 18 | Canny = cv2.Canny(gaussian, 50, 150) 19 | 20 | #用来正常显示中文标签 21 | plt.rcParams['font.sans-serif']=['SimHei'] 22 | 23 | #显示图形 24 | titles = [u'原始图像', u'Canny算子'] 25 | images = [lenna_img, Canny] 26 | for i in range(2): 27 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 28 | plt.title(titles[i]) 29 | plt.xticks([]),plt.yticks([]) 30 | plt.show() 31 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-08-LOG.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('lena.png') 9 | lenna_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #先通过高斯滤波降噪 15 | gaussian = cv2.GaussianBlur(grayImage, (3,3), 0) 16 | 17 | #再通过拉普拉斯算子做边缘检测 18 | dst = cv2.Laplacian(gaussian, cv2.CV_16S, ksize = 3) 19 | LOG = cv2.convertScaleAbs(dst) 20 | 21 | #用来正常显示中文标签 22 | plt.rcParams['font.sans-serif']=['SimHei'] 23 | 24 | #显示图形 25 | titles = [u'原始图像', u'LOG算子'] 26 | images = [lenna_img, LOG] 27 | for i in range(2): 28 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 29 | plt.title(titles[i]) 30 | plt.xticks([]),plt.yticks([]) 31 | plt.show() 32 | -------------------------------------------------------------------------------- /chapter11-ruihua/chapter11-09-all.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-19 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('nv.png') 9 | lenna_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #高斯滤波 15 | gaussianBlur = cv2.GaussianBlur(grayImage, (3,3), 0) 16 | 17 | #阈值处理 18 | ret, binary = cv2.threshold(gaussianBlur, 127, 255, cv2.THRESH_BINARY) 19 | 20 | #Roberts算子 21 | kernelx = np.array([[-1,0],[0,1]], dtype=int) 22 | kernely = np.array([[0,-1],[1,0]], dtype=int) 23 | x = cv2.filter2D(binary, cv2.CV_16S, kernelx) 24 | y = cv2.filter2D(binary, cv2.CV_16S, kernely) 25 | absX = cv2.convertScaleAbs(x) 26 | absY = cv2.convertScaleAbs(y) 27 | Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 28 | 29 | #Prewitt算子 30 | kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int) 31 | kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int) 32 | x = cv2.filter2D(binary, cv2.CV_16S, kernelx) 33 | y = cv2.filter2D(binary, cv2.CV_16S, kernely) 34 | absX = cv2.convertScaleAbs(x) 35 | absY = cv2.convertScaleAbs(y) 36 | Prewitt = cv2.addWeighted(absX,0.5,absY,0.5,0) 37 | 38 | #Sobel算子 39 | x = cv2.Sobel(binary, cv2.CV_16S, 1, 0) 40 | y = cv2.Sobel(binary, cv2.CV_16S, 0, 1) 41 | absX = cv2.convertScaleAbs(x) 42 | absY = cv2.convertScaleAbs(y) 43 | Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 44 | 45 | #拉普拉斯算法 46 | dst = cv2.Laplacian(binary, cv2.CV_16S, ksize = 3) 47 | Laplacian = cv2.convertScaleAbs(dst) 48 | 49 | 50 | # Scharr算子 51 | x = cv2.Scharr(gaussianBlur, cv2.CV_32F, 1, 0) #X方向 52 | y = cv2.Scharr(gaussianBlur, cv2.CV_32F, 0, 1) #Y方向 53 | absX = cv2.convertScaleAbs(x) 54 | absY = cv2.convertScaleAbs(y) 55 | Scharr = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 56 | 57 | #Canny算子 58 | Canny = cv2.Canny(gaussianBlur, 50, 150) 59 | 60 | #先通过高斯滤波降噪 61 | gaussian = cv2.GaussianBlur(grayImage, (3,3), 0) 62 | 63 | #再通过拉普拉斯算子做边缘检测 64 | dst = cv2.Laplacian(gaussian, cv2.CV_16S, ksize = 3) 65 | LOG = cv2.convertScaleAbs(dst) 66 | 67 | 68 | #效果图 69 | titles = ['Source Image', 'Binary Image', 'Roberts Image', 70 | 'Prewitt Image','Sobel Image', 'Laplacian Image', 71 | 'Scharr Image', 'Canny Image', 'LOG Image'] 72 | images = [lenna_img, binary, Roberts, 73 | Prewitt, Sobel, Laplacian, 74 | Scharr, Canny, LOG] 75 | for i in np.arange(9): 76 | plt.subplot(3,3,i+1),plt.imshow(images[i],'gray') 77 | plt.title(titles[i]) 78 | plt.xticks([]),plt.yticks([]) 79 | plt.show() 80 | -------------------------------------------------------------------------------- /chapter11-ruihua/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_1.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_2.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_3.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_4.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_5.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_6.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_7.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_8.png -------------------------------------------------------------------------------- /chapter11-ruihua/figure_all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter11-ruihua/figure_all.png -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_01_erode.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test01.jpg', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((5,5), np.uint8) 11 | 12 | #图像腐蚀处理 13 | erosion = cv2.erode(src, kernel, iterations=9) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", erosion) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_02_dilate.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test02.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((5,5), np.uint8) 11 | 12 | #图像膨胀处理 13 | erosion = cv2.dilate(src, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", erosion) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_03_open.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test01.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((5,5), np.uint8) 11 | 12 | #图像开运算 13 | result = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_04_open.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test01.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((10,10), np.uint8) 11 | 12 | #图像开运算 13 | result = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_05_close.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test03.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((10,10), np.uint8) 11 | 12 | #图像闭运算 13 | result = cv2.morphologyEx(src, cv2.MORPH_CLOSE, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_06_gradient.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test04.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((10,10), np.uint8) 11 | 12 | #图像梯度运算 13 | result = cv2.morphologyEx(src, cv2.MORPH_GRADIENT, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | 23 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_07_tophat.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test06.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((10,10), np.uint8) 11 | 12 | #图像顶帽运算 13 | result = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | 23 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_08_3D.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | #By:Eastmount CSDN 2021-07-30 3 | import numpy as np 4 | import cv2 as cv 5 | import matplotlib.pyplot as plt 6 | from mpl_toolkits.mplot3d import Axes3D 7 | from matplotlib import cm 8 | from matplotlib.ticker import LinearLocator, FormatStrFormatter 9 | 10 | #读取图像 11 | img = cv.imread("test06.png") 12 | img = cv.cvtColor(img,cv.COLOR_BGR2GRAY) 13 | 14 | #图像黑帽运算 15 | kernel = np.ones((10,10), np.uint8) 16 | result = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel) 17 | 18 | #image类转numpy 19 | imgd = np.array(result) #img 原图 20 | 21 | #准备数据 22 | sp = result.shape 23 | h = int(sp[0]) #图像高度(rows) 24 | w = int(sp[1]) #图像宽度(colums) of image 25 | 26 | #绘图初始处理 27 | fig = plt.figure(figsize=(8,6)) 28 | ax = fig.gca(projection="3d") 29 | 30 | x = np.arange(0, w, 1) 31 | y = np.arange(0, h, 1) 32 | x, y = np.meshgrid(x,y) 33 | z = imgd 34 | surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) 35 | 36 | #自定义z轴 37 | ax.set_zlim(-10, 255) 38 | ax.zaxis.set_major_locator(LinearLocator(10)) #设置z轴网格线的疏密 39 | #将z的value字符串转为float并保留2位小数 40 | ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 41 | 42 | # 设置坐标轴的label和标题 43 | ax.set_xlabel('x', size=15) 44 | ax.set_ylabel('y', size=15) 45 | ax.set_zlabel('z', size=15) 46 | ax.set_title("surface plot", weight='bold', size=20) 47 | 48 | #添加右侧的色卡条 49 | fig.colorbar(surf, shrink=0.6, aspect=8) 50 | plt.show() 51 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_09_blackhat.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | 6 | #读取图片 7 | src = cv2.imread('test06.png', cv2.IMREAD_UNCHANGED) 8 | 9 | #设置卷积核 10 | kernel = np.ones((10, 10), np.uint8) 11 | 12 | #图像黑帽运算 13 | result = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel) 14 | 15 | #显示图像 16 | cv2.imshow("src", src) 17 | cv2.imshow("result", result) 18 | 19 | #等待显示 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter12-morphology/chapter12_all.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | #By:Eastmount CSDN 2021-07-30 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | src = cv2.imread('test01_yn.png', cv2.IMREAD_UNCHANGED) 9 | img = cv2.cvtColor(src,cv2.COLOR_BGR2RGB) 10 | 11 | # 转化为灰度图 12 | Grayimg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) 13 | 14 | # 1、消除椒盐噪声: 15 | # 中值滤波器 16 | median = cv2.medianBlur(Grayimg, 5) 17 | # 消除噪声图 18 | cv2.imshow("median-image", median) 19 | 20 | # 2、直方图均衡化: 21 | equalize = cv2.equalizeHist(median) 22 | cv2.imshow('hist', equalize) 23 | 24 | # 3、二值化处理: 25 | # 阈值为140 26 | ret, binary = cv2.threshold(equalize, 127, 255,cv2.THRESH_BINARY) 27 | cv2.imshow("binary-image",binary) 28 | cv2.waitKey(0) 29 | 30 | 31 | #设置卷积核 32 | kernel = np.ones((10,10), np.uint8) 33 | close = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel) 34 | 35 | #图像开运算 36 | kernel = np.ones((10,10), np.uint8) 37 | open1 = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) 38 | 39 | #显示图像 40 | cv2.imshow("src", src) 41 | cv2.imshow("result", close) 42 | 43 | #等待显示 44 | cv2.waitKey(0) 45 | cv2.destroyAllWindows() 46 | 47 | #图像开运算 48 | kernel = np.ones((10,10), np.uint8) 49 | gradient = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel) 50 | 51 | # Sobel算子 XY方向求梯度 cv2.CV_8U 52 | x = cv2.Sobel(close, cv2.CV_32F, 1, 0, ksize = 3) #X方向 53 | y = cv2.Sobel(close, cv2.CV_32F, 0, 1, ksize = 3) #Y方向 54 | #absX = cv2.convertScaleAbs(x) # 转回uint8 55 | #absY = cv2.convertScaleAbs(y) 56 | #Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 57 | gradient = cv2.subtract(x, y) 58 | sobel = cv2.convertScaleAbs(gradient) 59 | cv2.imshow('Sobel', sobel) 60 | cv2.waitKey(0) 61 | 62 | #循环显示图形 63 | titles = [ 'source', 'gray', 'median', 'equalize', 'binary', 'close', 'open', 'gradient', 'sobel'] 64 | images = [img, Grayimg, median, equalize, binary, close, open1, gradient, sobel] 65 | for i in range(9): 66 | plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray') 67 | plt.title(titles[i]) 68 | plt.xticks([]),plt.yticks([]) 69 | plt.show() 70 | 71 | -------------------------------------------------------------------------------- /chapter12-morphology/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/figure_1.png -------------------------------------------------------------------------------- /chapter12-morphology/test01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test01.jpg -------------------------------------------------------------------------------- /chapter12-morphology/test01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test01.png -------------------------------------------------------------------------------- /chapter12-morphology/test02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test02.png -------------------------------------------------------------------------------- /chapter12-morphology/test03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test03.png -------------------------------------------------------------------------------- /chapter12-morphology/test04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test04.png -------------------------------------------------------------------------------- /chapter12-morphology/test05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test05.png -------------------------------------------------------------------------------- /chapter12-morphology/test06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter12-morphology/test06.png -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_01.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import cv2 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | #读取原始图像 7 | src = cv2.imread('scenery.png') 8 | 9 | #新建目标图像 10 | dst = np.zeros_like(src) 11 | 12 | #获取图像行和列 13 | rows, cols = src.shape[:2] 14 | 15 | #定义偏移量和随机数 16 | offsets = 5 17 | random_num = 0 18 | 19 | #毛玻璃效果: 像素点邻域内随机像素点的颜色替代当前像素点的颜色 20 | for y in range(rows - offsets): 21 | for x in range(cols - offsets): 22 | random_num = np.random.randint(0,offsets) 23 | dst[y,x] = src[y + random_num,x + random_num] 24 | 25 | #显示图像 26 | cv2.imshow('src',src) 27 | cv2.imshow('dst',dst) 28 | 29 | cv2.waitKey() 30 | cv2.destroyAllWindows() 31 | 32 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_02.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | img = cv2.imread('scenery.png', 1) 8 | 9 | #获取图像的高度和宽度 10 | height, width = img.shape[:2] 11 | 12 | #图像灰度处理 13 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 14 | 15 | #创建目标图像 16 | dstImg = np.zeros((height,width,1),np.uint8) 17 | 18 | #浮雕特效算法:newPixel = grayCurrentPixel - grayNextPixel + 150 19 | for i in range(0,height): 20 | for j in range(0,width-1): 21 | grayCurrentPixel = int(gray[i,j]) 22 | grayNextPixel = int(gray[i,j+1]) 23 | newPixel = grayCurrentPixel - grayNextPixel + 150 24 | if newPixel > 255: 25 | newPixel = 255 26 | if newPixel < 0: 27 | newPixel = 0 28 | dstImg[i,j] = newPixel 29 | 30 | #显示图像 31 | cv2.imshow('src', img) 32 | cv2.imshow('dst',dstImg) 33 | 34 | #等待显示 35 | cv2.waitKey() 36 | cv2.destroyAllWindows() 37 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_03.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | src = cv2.imread('scenery.png') 8 | 9 | #图像灰度处理 10 | gray = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) 11 | 12 | #自定义卷积核 13 | kernel = np.array([[-1,-1,-1],[-1,10,-1],[-1,-1,-1]]) 14 | #kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]]) 15 | 16 | #图像浮雕效果 17 | output = cv2.filter2D(gray, -1, kernel) 18 | 19 | #显示图像 20 | cv2.imshow('Original Image', src) 21 | cv2.imshow('Emboss_1',output) 22 | 23 | #等待显示 24 | cv2.waitKey() 25 | cv2.destroyAllWindows() 26 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_04.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | img = cv2.imread('scenery.png') 8 | 9 | #图像灰度处理 10 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 11 | 12 | #高斯滤波降噪 13 | gaussian = cv2.GaussianBlur(gray, (5,5), 0) 14 | 15 | #Canny算子 16 | canny = cv2.Canny(gaussian, 50, 150) 17 | 18 | #阈值化处理 19 | ret, result = cv2.threshold(canny, 100, 255, cv2.THRESH_BINARY_INV) 20 | 21 | #显示图像 22 | cv2.imshow('src', img) 23 | cv2.imshow('result', result) 24 | 25 | cv2.waitKey() 26 | cv2.destroyAllWindows() 27 | 28 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_05_sumiao .py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | def dodgeNaive(image, mask): 7 | # determine the shape of the input image 8 | width, height = image.shape[:2] 9 | 10 | # prepare output argument with same size as image 11 | blend = np.zeros((width, height), np.uint8) 12 | 13 | for col in range(width): 14 | for row in range(height): 15 | # do for every pixel 16 | if mask[col, row] == 255: 17 | # avoid division by zero 18 | blend[col, row] = 255 19 | else: 20 | # shift image pixel value by 8 bits 21 | # divide by the inverse of the mask 22 | tmp = (image[col, row] << 8) / (255 - mask) 23 | # print('tmp={}'.format(tmp.shape)) 24 | # make sure resulting value stays within bounds 25 | if tmp.any() > 255: 26 | tmp = 255 27 | blend[col, row] = tmp 28 | 29 | return blend 30 | 31 | def dodgeV2(image, mask): 32 | return cv2.divide(image, 255 - mask, scale=256) 33 | 34 | def burnV2(image, mask): 35 | return 255 - cv2.divide(255 - image, 255 - mask, scale=256) 36 | 37 | def rgb_to_sketch(src_image_name, dst_image_name): 38 | img_rgb = cv2.imread(src_image_name) 39 | img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) 40 | # 读取图片时直接转换操作 41 | # img_gray = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE) 42 | 43 | img_gray_inv = 255 - img_gray 44 | img_blur = cv2.GaussianBlur(img_gray_inv, ksize=(21, 21), 45 | sigmaX=0, sigmaY=0) 46 | img_blend = dodgeV2(img_gray, img_blur) 47 | 48 | cv2.imshow('original', img_rgb) 49 | cv2.imshow('gray', img_gray) 50 | cv2.imshow('gray_inv', img_gray_inv) 51 | cv2.imshow('gray_blur', img_blur) 52 | cv2.imshow("pencil sketch", img_blend) 53 | cv2.waitKey(0) 54 | cv2.destroyAllWindows() 55 | cv2.imwrite(dst_image_name, img_blend) 56 | 57 | 58 | if __name__ == '__main__': 59 | src_image_name = 'scenery.png' 60 | dst_image_name = 'sketch_example.jpg' 61 | rgb_to_sketch(src_image_name, dst_image_name) 62 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_06_huaijiu.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | img = cv2.imread('nv.png') 8 | 9 | #获取图像行和列 10 | rows, cols = img.shape[:2] 11 | 12 | #新建目标图像 13 | dst = np.zeros((rows, cols, 3), dtype="uint8") 14 | 15 | #图像怀旧特效 16 | for i in range(rows): 17 | for j in range(cols): 18 | B = 0.272*img[i,j][2] + 0.534*img[i,j][1] + 0.131*img[i,j][0] 19 | G = 0.349*img[i,j][2] + 0.686*img[i,j][1] + 0.168*img[i,j][0] 20 | R = 0.393*img[i,j][2] + 0.769*img[i,j][1] + 0.189*img[i,j][0] 21 | if B>255: 22 | B = 255 23 | if G>255: 24 | G = 255 25 | if R>255: 26 | R = 255 27 | dst[i,j] = np.uint8((B, G, R)) 28 | 29 | #显示图像 30 | cv2.imshow('src', img) 31 | cv2.imshow('dst', dst) 32 | 33 | cv2.waitKey() 34 | cv2.destroyAllWindows() 35 | 36 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_07_guang.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import math 5 | import numpy as np 6 | 7 | #读取原始图像 8 | img = cv2.imread('nv.png') 9 | 10 | #获取图像行和列 11 | rows, cols = img.shape[:2] 12 | 13 | #设置中心点 14 | centerX = rows / 2 15 | centerY = cols / 2 16 | print(centerX, centerY) 17 | radius = min(centerX, centerY) 18 | print(radius) 19 | 20 | #设置光照强度 21 | strength = 200 22 | 23 | #新建目标图像 24 | dst = np.zeros((rows, cols, 3), dtype="uint8") 25 | 26 | #图像光照特效 27 | for i in range(rows): 28 | for j in range(cols): 29 | #计算当前点到光照中心的距离(平面坐标系中两点之间的距离) 30 | distance = math.pow((centerY-j), 2) + math.pow((centerX-i), 2) 31 | #获取原始图像 32 | B = img[i,j][0] 33 | G = img[i,j][1] 34 | R = img[i,j][2] 35 | if (distance < radius * radius): 36 | #按照距离大小计算增强的光照值 37 | result = (int)(strength*( 1.0 - math.sqrt(distance) / radius )) 38 | B = img[i,j][0] + result 39 | G = img[i,j][1] + result 40 | R = img[i,j][2] + result 41 | #判断边界 防止越界 42 | B = min(255, max(0, B)) 43 | G = min(255, max(0, G)) 44 | R = min(255, max(0, R)) 45 | dst[i,j] = np.uint8((B, G, R)) 46 | else: 47 | dst[i,j] = np.uint8((B, G, R)) 48 | 49 | #显示图像 50 | cv2.imshow('src', img) 51 | cv2.imshow('dst', dst) 52 | 53 | cv2.waitKey() 54 | cv2.destroyAllWindows() 55 | 56 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_08_liunian.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import math 5 | import numpy as np 6 | 7 | #读取原始图像 8 | img = cv2.imread('nv.png') 9 | 10 | #获取图像行和列 11 | rows, cols = img.shape[:2] 12 | 13 | #新建目标图像 14 | dst = np.zeros((rows, cols, 3), dtype="uint8") 15 | 16 | #图像流年特效 17 | for i in range(rows): 18 | for j in range(cols): 19 | #B通道的数值开平方乘以参数12 20 | B = math.sqrt(img[i,j][0]) * 12 21 | G = img[i,j][1] 22 | R = img[i,j][2] 23 | if B>255: 24 | B = 255 25 | dst[i,j] = np.uint8((B, G, R)) 26 | 27 | #显示图像 28 | cv2.imshow('src', img) 29 | cv2.imshow('dst', dst) 30 | 31 | cv2.waitKey() 32 | cv2.destroyAllWindows() 33 | 34 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_09_sb.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | #该效果有点丑,读者可以设置透明度进一步优化,可能效果会好看些。 4 | import cv2 5 | import math 6 | import numpy as np 7 | 8 | #读取原始图像 9 | img = cv2.imread('nv.png') 10 | 11 | #获取图像行和列 12 | rows, cols = img.shape[:2] 13 | 14 | #新建目标图像 15 | dst = np.zeros((rows, cols, 3), dtype="uint8") 16 | 17 | #定义水波特效参数 18 | wavelength = 20 19 | amplitude = 30 20 | phase = math.pi / 4 21 | 22 | #获取中心点 23 | centreX = 0.5 24 | centreY = 0.5 25 | radius = min(rows, cols) / 2 26 | 27 | #设置水波覆盖面积 28 | icentreX = cols*centreX 29 | icentreY = rows*centreY 30 | 31 | #图像水波特效 32 | for i in range(rows): 33 | for j in range(cols): 34 | dx = j - icentreX 35 | dy = i - icentreY 36 | distance = dx*dx + dy*dy 37 | 38 | if distance>radius*radius: 39 | x = j 40 | y = i 41 | else: 42 | #计算水波区域 43 | distance = math.sqrt(distance) 44 | 45 | amount = amplitude * math.sin(distance / wavelength * 2*math.pi - phase) 46 | amount = amount * (radius-distance) / radius 47 | amount = amount * wavelength / (distance+0.0001) 48 | x = j + dx * amount 49 | y = i + dy * amount 50 | 51 | #边界判断 52 | if x<0: 53 | x = 0 54 | if x>=cols-1: 55 | x = cols - 2 56 | if y<0: 57 | y = 0 58 | if y>=rows-1: 59 | y = rows - 2 60 | 61 | p = x - int(x) 62 | q = y - int(y) 63 | 64 | #图像水波赋值 65 | dst[i, j, :] = (1-p)*(1-q)*img[int(y),int(x),:] + p*(1-q)*img[int(y),int(x),:] 66 | + (1-p)*q*img[int(y),int(x),:] + p*q*img[int(y),int(x),:] 67 | 68 | #显示图像 69 | cv2.imshow('src', img) 70 | cv2.imshow('dst', dst) 71 | 72 | cv2.waitKey() 73 | cv2.destroyAllWindows() 74 | 75 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_10_kt.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | img = cv2.imread('nv.png') 8 | 9 | #定义双边滤波的数目 10 | num_bilateral = 7 11 | 12 | #用高斯金字塔降低取样 13 | img_color = img 14 | 15 | #双边滤波处理 16 | for i in range(num_bilateral): 17 | img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7) 18 | 19 | #灰度图像转换 20 | img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) 21 | 22 | #中值滤波处理 23 | img_blur = cv2.medianBlur(img_gray, 7) 24 | 25 | #边缘检测及自适应阈值化处理 26 | img_edge = cv2.adaptiveThreshold(img_blur, 255, 27 | cv2.ADAPTIVE_THRESH_MEAN_C, 28 | cv2.THRESH_BINARY, 29 | blockSize=9, 30 | C=2) 31 | 32 | #转换回彩色图像 33 | img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) 34 | 35 | #与运算 36 | img_cartoon = cv2.bitwise_and(img_color, img_edge) 37 | 38 | #显示图像 39 | cv2.imshow('src', img) 40 | cv2.imshow('dst', img_cartoon) 41 | cv2.waitKey() 42 | cv2.destroyAllWindows() 43 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_11_lj.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import cv2 3 | import math 4 | import numpy as np 5 | 6 | #获取滤镜颜色 7 | def getBGR(img, table, i, j): 8 | #获取图像颜色 9 | b, g, r = img[i][j] 10 | #计算标准颜色表中颜色的位置坐标 11 | x = int(g/4 + int(b/32) * 64) 12 | y = int(r/4 + int((b%32) / 4) * 64) 13 | #返回滤镜颜色表中对应的颜色 14 | return lj_map[x][y] 15 | 16 | #读取原始图像 17 | img = cv2.imread('nv.png') 18 | lj_map = cv2.imread('table.png') 19 | 20 | #获取图像行和列 21 | rows, cols = img.shape[:2] 22 | 23 | #新建目标图像 24 | dst = np.zeros((rows, cols, 3), dtype="uint8") 25 | 26 | #循环设置滤镜颜色 27 | for i in range(rows): 28 | for j in range(cols): 29 | dst[i][j] = getBGR(img, lj_map, i, j) 30 | 31 | #显示图像 32 | cv2.imshow('src', img) 33 | cv2.imshow('dst', dst) 34 | 35 | cv2.waitKey() 36 | cv2.destroyAllWindows() 37 | 38 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_12_jh.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | img = cv2.imread('nv.png') 8 | 9 | #获取图像行和列 10 | rows, cols = img.shape[:2] 11 | 12 | #新建目标图像 13 | dst = np.zeros((rows, cols, 3), dtype="uint8") 14 | 15 | #提取三个颜色通道 16 | (b, g, r) = cv2.split(img) 17 | 18 | #彩色图像均衡化 19 | bH = cv2.equalizeHist(b) 20 | gH = cv2.equalizeHist(g) 21 | rH = cv2.equalizeHist(r) 22 | 23 | #合并通道 24 | dst = cv2.merge((bH, gH, rH)) 25 | 26 | #显示图像 27 | cv2.imshow('src', img) 28 | cv2.imshow('dst', dst) 29 | cv2.waitKey() 30 | cv2.destroyAllWindows() 31 | -------------------------------------------------------------------------------- /chapter13-Special-effects/chapter13_13_gs.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | #By:Eastmount CSDN 2020-12-22 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图片 8 | img = cv2.imread('scenery.png') 9 | source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #高斯滤波 12 | result = cv2.GaussianBlur(source, (11,11), 0) 13 | 14 | #用来正常显示中文标签 15 | plt.rcParams['font.sans-serif']=['SimHei'] 16 | 17 | #显示图形 18 | titles = [u'原始图像', u'高斯滤波'] 19 | images = [source, result] 20 | for i in range(2): 21 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 22 | plt.title(titles[i]) 23 | plt.xticks([]),plt.yticks([]) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /chapter13-Special-effects/dd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/dd.jpg -------------------------------------------------------------------------------- /chapter13-Special-effects/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/figure_1.png -------------------------------------------------------------------------------- /chapter13-Special-effects/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/figure_2.png -------------------------------------------------------------------------------- /chapter13-Special-effects/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/figure_3.png -------------------------------------------------------------------------------- /chapter13-Special-effects/nv-cartoon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/nv-cartoon.png -------------------------------------------------------------------------------- /chapter13-Special-effects/nv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/nv.png -------------------------------------------------------------------------------- /chapter13-Special-effects/scenery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/scenery.png -------------------------------------------------------------------------------- /chapter13-Special-effects/sketch_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/sketch_example.jpg -------------------------------------------------------------------------------- /chapter13-Special-effects/table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/table.png -------------------------------------------------------------------------------- /chapter13-Special-effects/table2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter13-Special-effects/table2.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-01-yzfg.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img=cv2.imread('scenery.png') 9 | grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 10 | 11 | #阈值化处理 12 | ret,thresh1=cv2.threshold(grayImage,127,255,cv2.THRESH_BINARY) 13 | ret,thresh2=cv2.threshold(grayImage,127,255,cv2.THRESH_BINARY_INV) 14 | ret,thresh3=cv2.threshold(grayImage,127,255,cv2.THRESH_TRUNC) 15 | ret,thresh4=cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO) 16 | ret,thresh5=cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO_INV) 17 | 18 | #显示结果 19 | titles = ['Gray Image','BINARY','BINARY_INV','TRUNC', 20 | 'TOZERO','TOZERO_INV'] 21 | images = [grayImage, thresh1, thresh2, thresh3, thresh4, thresh5] 22 | for i in range(6): 23 | plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') 24 | plt.title(titles[i]) 25 | plt.xticks([]),plt.yticks([]) 26 | plt.show() 27 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-02-byjc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('scenery.png') 9 | rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #阈值处理 15 | ret, binary = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY) 16 | 17 | #Roberts算子 18 | kernelx = np.array([[-1,0],[0,1]], dtype=int) 19 | kernely = np.array([[0,-1],[1,0]], dtype=int) 20 | x = cv2.filter2D(binary, cv2.CV_16S, kernelx) 21 | y = cv2.filter2D(binary, cv2.CV_16S, kernely) 22 | absX = cv2.convertScaleAbs(x) 23 | absY = cv2.convertScaleAbs(y) 24 | Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 25 | 26 | #Prewitt算子 27 | kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int) 28 | kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int) 29 | x = cv2.filter2D(binary, cv2.CV_16S, kernelx) 30 | y = cv2.filter2D(binary, cv2.CV_16S, kernely) 31 | absX = cv2.convertScaleAbs(x) 32 | absY = cv2.convertScaleAbs(y) 33 | Prewitt = cv2.addWeighted(absX,0.5,absY,0.5,0) 34 | 35 | #Sobel算子 36 | x = cv2.Sobel(binary, cv2.CV_16S, 1, 0) 37 | y = cv2.Sobel(binary, cv2.CV_16S, 0, 1) 38 | absX = cv2.convertScaleAbs(x) 39 | absY = cv2.convertScaleAbs(y) 40 | Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 41 | 42 | #拉普拉斯算法 43 | dst = cv2.Laplacian(binary, cv2.CV_16S, ksize = 3) 44 | Laplacian = cv2.convertScaleAbs(dst) 45 | 46 | # Scharr算子 47 | x = cv2.Scharr(binary, cv2.CV_32F, 1, 0) #X方向 48 | y = cv2.Scharr(binary, cv2.CV_32F, 0, 1) #Y方向 49 | absX = cv2.convertScaleAbs(x) 50 | absY = cv2.convertScaleAbs(y) 51 | Scharr = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) 52 | 53 | #Canny算子 54 | gaussianBlur = cv2.GaussianBlur(binary, (3,3), 0) #高斯滤波 55 | Canny = cv2.Canny(gaussianBlur , 50, 150) 56 | 57 | #LOG算子 58 | gaussianBlur = cv2.GaussianBlur(binary, (3,3), 0) #高斯滤波 59 | dst = cv2.Laplacian(gaussianBlur, cv2.CV_16S, ksize = 3) 60 | LOG = cv2.convertScaleAbs(dst) 61 | 62 | #效果图 63 | titles = ['Source Image', 'Binary Image', 'Roberts Image', 64 | 'Prewitt Image','Sobel Image', 'Laplacian Image', 65 | 'Scharr Image', 'Canny Image', 'LOG Image'] 66 | images = [rgb_img, binary, Roberts, Prewitt, 67 | Sobel, Laplacian, Scharr, Canny, LOG] 68 | for i in np.arange(9): 69 | plt.subplot(3,3,i+1),plt.imshow(images[i],'gray') 70 | plt.title(titles[i]) 71 | plt.xticks([]),plt.yticks([]) 72 | plt.show() 73 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-03-lk.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('scenery.png') 9 | rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 10 | 11 | #灰度化处理图像 12 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 | 14 | #阈值化处理 15 | ret, binary = cv2.threshold(grayImage, 0, 255, 16 | cv2.THRESH_BINARY+cv2.THRESH_OTSU) 17 | 18 | #边缘检测 19 | contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, 20 | cv2.CHAIN_APPROX_SIMPLE) 21 | 22 | #轮廓绘制 23 | cv2.drawContours(img, contours, -1, (0, 255, 0), 1) 24 | 25 | #显示图像5 26 | cv2.imshow('gray', binary) 27 | cv2.imshow('res', img) 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-04-peoloe-get.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取图像 8 | img = cv2.imread('nv.png') 9 | 10 | #灰度化处理图像 11 | grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #设置掩码、fgbModel、bgModel 14 | mask = np.zeros(img.shape[:2], np.uint8) 15 | bgdModel = np.zeros((1,65), np.float64) 16 | fgdModel = np.zeros((1,65), np.float64) 17 | 18 | #矩形坐标 19 | rect = (100, 100, 500, 800) 20 | 21 | #图像分割 22 | cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, 23 | cv2.GC_INIT_WITH_RECT) 24 | 25 | #设置新掩码:0和2做背景 26 | mask2 = np.where((mask==2)|(mask==0), 0, 1).astype('uint8') 27 | 28 | #显示原图 29 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 30 | plt.subplot(1,2,1) 31 | plt.imshow(img) 32 | plt.title('Original Image') 33 | plt.xticks([]), plt.yticks([]) 34 | 35 | #使用蒙板来获取前景区域 36 | img = img*mask2[:, :, np.newaxis] 37 | plt.subplot(1,2,2) 38 | plt.imshow(img) 39 | plt.title('Target Image') 40 | plt.colorbar() 41 | plt.xticks([]), plt.yticks([]) 42 | plt.show() 43 | 44 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-05-kmeans.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像灰度颜色 8 | img = cv2.imread('scenery.png', 0) 9 | print(img.shape) 10 | 11 | #获取图像高度、宽度和深度 12 | rows, cols = img.shape[:] 13 | 14 | #图像二维像素转换为一维 15 | data = img.reshape((rows * cols, 1)) 16 | data = np.float32(data) 17 | 18 | #定义中心 (type,max_iter,epsilon) 19 | criteria = (cv2.TERM_CRITERIA_EPS + 20 | cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 21 | 22 | #设置标签 23 | flags = cv2.KMEANS_RANDOM_CENTERS 24 | 25 | #K-Means聚类 聚集成4类 26 | compactness, labels, centers = cv2.kmeans(data, 4, None, criteria, 10, flags) 27 | 28 | #生成最终图像 29 | dst = labels.reshape((img.shape[0], img.shape[1])) 30 | 31 | #用来正常显示中文标签 32 | plt.rcParams['font.sans-serif']=['SimHei'] 33 | 34 | #显示图像 35 | titles = [u'原始图像', u'聚类图像'] 36 | images = [img, dst] 37 | for i in range(2): 38 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray'), 39 | plt.title(titles[i]) 40 | plt.xticks([]),plt.yticks([]) 41 | plt.show() 42 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-06-kmeans(best).py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('scenery.png') 9 | print(img.shape) 10 | 11 | #图像二维像素转换为一维 12 | data = img.reshape((-1,3)) 13 | data = np.float32(data) 14 | 15 | #定义中心 (type,max_iter,epsilon) 16 | criteria = (cv2.TERM_CRITERIA_EPS + 17 | cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 18 | 19 | #设置标签 20 | flags = cv2.KMEANS_RANDOM_CENTERS 21 | 22 | #K-Means聚类 聚集成2类 23 | compactness, labels2, centers2 = cv2.kmeans(data, 2, None, criteria, 10, flags) 24 | 25 | #K-Means聚类 聚集成4类 26 | compactness, labels4, centers4 = cv2.kmeans(data, 4, None, criteria, 10, flags) 27 | 28 | #K-Means聚类 聚集成8类 29 | compactness, labels8, centers8 = cv2.kmeans(data, 8, None, criteria, 10, flags) 30 | 31 | #K-Means聚类 聚集成16类 32 | compactness, labels16, centers16 = cv2.kmeans(data, 16, None, criteria, 10, flags) 33 | 34 | #K-Means聚类 聚集成64类 35 | compactness, labels64, centers64 = cv2.kmeans(data, 64, None, criteria, 10, flags) 36 | 37 | #图像转换回uint8二维类型 38 | centers2 = np.uint8(centers2) 39 | res = centers2[labels2.flatten()] 40 | dst2 = res.reshape((img.shape)) 41 | 42 | centers4 = np.uint8(centers4) 43 | res = centers4[labels4.flatten()] 44 | dst4 = res.reshape((img.shape)) 45 | 46 | centers8 = np.uint8(centers8) 47 | res = centers8[labels8.flatten()] 48 | dst8 = res.reshape((img.shape)) 49 | 50 | centers16 = np.uint8(centers16) 51 | res = centers16[labels16.flatten()] 52 | dst16 = res.reshape((img.shape)) 53 | 54 | centers64 = np.uint8(centers64) 55 | res = centers64[labels64.flatten()] 56 | dst64 = res.reshape((img.shape)) 57 | 58 | #图像转换为RGB显示 59 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 60 | dst2 = cv2.cvtColor(dst2, cv2.COLOR_BGR2RGB) 61 | dst4 = cv2.cvtColor(dst4, cv2.COLOR_BGR2RGB) 62 | dst8 = cv2.cvtColor(dst8, cv2.COLOR_BGR2RGB) 63 | dst16 = cv2.cvtColor(dst16, cv2.COLOR_BGR2RGB) 64 | dst64 = cv2.cvtColor(dst64, cv2.COLOR_BGR2RGB) 65 | 66 | #用来正常显示中文标签 67 | plt.rcParams['font.sans-serif']=['SimHei'] 68 | 69 | #显示图像 70 | titles = [u'原始图像', u'聚类图像 K=2', u'聚类图像 K=4', 71 | u'聚类图像 K=8', u'聚类图像 K=16', u'聚类图像 K=64'] 72 | images = [img, dst2, dst4, dst8, dst16, dst64] 73 | for i in range(6): 74 | plt.subplot(2,3,i+1), plt.imshow(images[i], 'gray'), 75 | plt.title(titles[i]) 76 | plt.xticks([]),plt.yticks([]) 77 | plt.show() 78 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-07-shift.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像灰度颜色 8 | img = cv2.imread('scenery.png') 9 | 10 | spatialRad = 100 #空间窗口大小 11 | colorRad = 100 #色彩窗口大小 12 | maxPyrLevel = 2 #金字塔层数 13 | 14 | #图像均值漂移分割 15 | dst = cv2.pyrMeanShiftFiltering( img, spatialRad, colorRad, maxPyrLevel) 16 | 17 | #显示图像 18 | cv2.imshow('src', img) 19 | cv2.imshow('dst', dst) 20 | cv2.waitKey() 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-08-shift(best).py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像灰度颜色 8 | img = cv2.imread('scenery.png') 9 | 10 | #获取图像行和列 11 | rows, cols = img.shape[:2] 12 | 13 | #mask必须行和列都加2且必须为uint8单通道阵列 14 | mask = np.zeros([rows+2, cols+2], np.uint8) 15 | 16 | spatialRad = 100 #空间窗口大小 17 | colorRad = 100 #色彩窗口大小 18 | maxPyrLevel = 2 #金字塔层数 19 | 20 | #图像均值漂移分割 21 | dst = cv2.pyrMeanShiftFiltering( img, spatialRad, colorRad, maxPyrLevel) 22 | 23 | #图像漫水填充处理 24 | cv2.floodFill(dst, mask, (30, 30), (0, 255, 255), 25 | (100, 100, 100), (50, 50, 50), 26 | cv2.FLOODFILL_FIXED_RANGE) 27 | 28 | #显示图像 29 | cv2.imshow('src', img) 30 | cv2.imshow('dst', dst) 31 | cv2.waitKey() 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-09-fsl.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import numpy as np 4 | import cv2 5 | from matplotlib import pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('test01.png') 9 | 10 | #图像灰度化处理 11 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 12 | 13 | #图像阈值化处理 14 | ret, thresh = cv2.threshold(gray, 0, 255, 15 | cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 16 | 17 | #显示图像 18 | cv2.imshow('src', img) 19 | cv2.imshow('res', thresh) 20 | cv2.waitKey() 21 | cv2.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-10-water.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import numpy as np 4 | import cv2 5 | from matplotlib import pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('test01.png') 9 | 10 | #图像灰度化处理 11 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 12 | 13 | #图像阈值化处理 14 | ret, thresh = cv2.threshold(gray, 0, 255, 15 | cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 16 | 17 | #图像开运算消除噪声 18 | kernel = np.ones((3,3),np.uint8) 19 | opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2) 20 | 21 | #图像膨胀操作确定背景区域 22 | sure_bg = cv2.dilate(opening,kernel,iterations=3) 23 | 24 | #距离运算确定前景区域 25 | dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5) 26 | ret, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) 27 | 28 | #寻找未知区域 29 | sure_fg = np.uint8(sure_fg) 30 | unknown = cv2.subtract(sure_bg, sure_fg) 31 | 32 | #用来正常显示中文标签 33 | plt.rcParams['font.sans-serif']=['SimHei'] 34 | 35 | #显示图像 36 | titles = [u'原始图像', u'阈值化', u'开运算', 37 | u'背景区域', u'前景区域', u'未知区域'] 38 | images = [img, thresh, opening, sure_bg, sure_fg, unknown] 39 | for i in range(6): 40 | plt.subplot(2,3,i+1), plt.imshow(images[i], 'gray') 41 | plt.title(titles[i]) 42 | plt.xticks([]),plt.yticks([]) 43 | plt.show() 44 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-11-water-final.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import numpy as np 4 | import cv2 5 | from matplotlib import pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread('test01.png') 9 | 10 | #图像灰度化处理 11 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 12 | 13 | #图像阈值化处理 14 | ret, thresh = cv2.threshold(gray, 0, 255, 15 | cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 16 | 17 | #图像开运算消除噪声 18 | kernel = np.ones((3,3),np.uint8) 19 | opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2) 20 | 21 | #图像膨胀操作确定背景区域 22 | sure_bg = cv2.dilate(opening,kernel,iterations=3) 23 | 24 | #距离运算确定前景区域 25 | dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5) 26 | ret, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) 27 | 28 | #寻找未知区域 29 | sure_fg = np.uint8(sure_fg) 30 | unknown = cv2.subtract(sure_bg, sure_fg) 31 | 32 | #标记变量 33 | ret, markers = cv2.connectedComponents(sure_fg) 34 | 35 | #所有标签加一,以确保背景不是0而是1 36 | markers = markers+1 37 | 38 | #用0标记未知区域 39 | markers[unknown==255]=0 40 | 41 | #分水岭算法实现图像分割 42 | markers = cv2.watershed(img, markers) 43 | img[markers == -1] = [255,0,0] 44 | 45 | #用来正常显示中文标签 46 | plt.rcParams['font.sans-serif']=['SimHei'] 47 | 48 | #显示图像 49 | titles = [u'标记区域', u'图像分割'] 50 | images = [markers, img] 51 | for i in range(2): 52 | plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 53 | plt.title(titles[i]) 54 | plt.xticks([]),plt.yticks([]) 55 | plt.show() 56 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-12-mstc.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | 6 | #读取原始图像 7 | img = cv2.imread('test.png') 8 | 9 | #获取图像行和列 10 | rows, cols = img.shape[:2] 11 | 12 | #目标图像 13 | dst = img.copy() 14 | 15 | #mask必须行和列都加2且必须为uint8单通道阵列 16 | #mask多出来的2可以保证扫描的边界上的像素都会被处理 17 | mask = np.zeros([rows+2, cols+2], np.uint8) 18 | 19 | #图像漫水填充处理 20 | #种子点位置(30,30) 设置颜色(0,255,255) 连通区范围设定loDiff upDiff 21 | #src(seed.x, seed.y) - loDiff <= src(x, y) <= src(seed.x, seed.y) +upDiff 22 | cv2.floodFill(dst, mask, (30, 30), (0, 255, 255), 23 | (100, 100, 100), (50, 50, 50), 24 | cv2.FLOODFILL_FIXED_RANGE) 25 | 26 | #显示图像 27 | cv2.imshow('src', img) 28 | cv2.imshow('dst', dst) 29 | cv2.waitKey() 30 | cv2.destroyAllWindows() 31 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-13-mstc(best).py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import random 5 | import sys 6 | import numpy as np 7 | 8 | #使用说明 点击鼠标选择种子点 9 | help_message = '''USAGE: floodfill.py [] 10 | Click on the image to set seed point 11 | Keys: 12 | f - toggle floating range 13 | c - toggle 4/8 connectivity 14 | ESC - exit 15 | ''' 16 | 17 | if __name__ == '__main__': 18 | 19 | #输出提示文本 20 | print(help_message) 21 | 22 | #读取原始图像 23 | img = cv2.imread('nv.png') 24 | 25 | #获取图像高和宽 26 | h, w = img.shape[:2] 27 | 28 | #设置掩码 长和宽都比输入图像多两个像素点 29 | mask = np.zeros((h+2, w+2), np.uint8) 30 | 31 | #设置种子节点和4邻接 32 | seed_pt = None 33 | fixed_range = True 34 | connectivity = 4 35 | 36 | #图像漫水填充分割更新函数 37 | def update(dummy=None): 38 | if seed_pt is None: 39 | cv2.imshow('floodfill', img) 40 | return 41 | 42 | #建立图像副本并漫水填充 43 | flooded = img.copy() 44 | mask[:] = 0 #掩码初始为全0 45 | lo = cv2.getTrackbarPos('lo', 'floodfill') #观察点像素邻域负差最大值 46 | hi = cv2.getTrackbarPos('hi', 'floodfill') #观察点像素邻域正差最大值 47 | print('lo=', lo, 'hi=', hi) 48 | 49 | #低位比特包含连通值 4 (缺省) 或 8 50 | flags = connectivity 51 | 52 | #考虑当前象素与种子象素之间的差(高比特也可以为0) 53 | if fixed_range: 54 | flags |= cv2.FLOODFILL_FIXED_RANGE 55 | 56 | #以白色进行漫水填充 57 | cv2.floodFill(flooded, mask, seed_pt, 58 | (random.randint(0,255), random.randint(0,255), 59 | random.randint(0,255)), (lo,)*3, (hi,)*3, flags) 60 | 61 | #选定基准点用红色圆点标出 62 | cv2.circle(flooded, seed_pt, 2, (0, 0, 255), -1) 63 | print("send_pt=", seed_pt) 64 | 65 | #显示图像 66 | cv2.imshow('floodfill', flooded) 67 | 68 | #鼠标响应函数 69 | def onmouse(event, x, y, flags, param): 70 | global seed_pt #基准点 71 | 72 | #鼠标左键响应选择漫水填充基准点 73 | if flags & cv2.EVENT_FLAG_LBUTTON: 74 | seed_pt = x, y 75 | update() 76 | 77 | #执行图像漫水填充分割更新操作 78 | update() 79 | 80 | #鼠标更新操作 81 | cv2.setMouseCallback('floodfill', onmouse) 82 | 83 | #设置进度条 84 | cv2.createTrackbar('lo', 'floodfill', 20, 255, update) 85 | cv2.createTrackbar('hi', 'floodfill', 20, 255, update) 86 | 87 | #按键响应操作 88 | while True: 89 | ch = 0xFF & cv2.waitKey() 90 | #退出 91 | if ch == 27: 92 | break 93 | #选定时flags的高位比特位0 94 | #此时邻域的选定为当前像素与相邻像素的差, 联通区域会很大 95 | if ch == ord('f'): 96 | fixed_range = not fixed_range 97 | print('using %s range' % ('floating', 'fixed')[fixed_range]) 98 | update() 99 | #选择4方向或则8方向种子扩散 100 | if ch == ord('c'): 101 | connectivity = 12-connectivity 102 | print('connectivity =', connectivity) 103 | update() 104 | cv2.destroyAllWindows() 105 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/chapter14-14-word(best).py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | # 2021-05-17 Eastmount CSDN 3 | import cv2 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | 7 | #读取原始图像 8 | img = cv2.imread("word.png" ) 9 | 10 | #中值滤波去除噪声 11 | median = cv2.medianBlur(img, 3) 12 | 13 | #转换成灰度图像 14 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 15 | 16 | #Sobel算子锐化处理 17 | sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize = 3) 18 | 19 | #图像二值化处理 20 | ret, binary = cv2.threshold(sobel, 0, 255, 21 | cv2.THRESH_OTSU+cv2.THRESH_BINARY) 22 | 23 | #膨胀和腐蚀处理 24 | #设置膨胀和腐蚀操作的核函数 25 | element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 9)) 26 | element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (24, 6)) 27 | 28 | #膨胀突出轮廓 29 | dilation = cv2.dilate(binary, element2, iterations = 1) 30 | 31 | #腐蚀去掉细节 32 | erosion = cv2.erode(dilation, element1, iterations = 1) 33 | 34 | #查找文字轮廓 35 | region = [] 36 | contours, hierarchy = cv2.findContours(erosion, 37 | cv2.RETR_TREE, 38 | cv2.CHAIN_APPROX_SIMPLE) 39 | 40 | #筛选面积 41 | for i in range(len(contours)): 42 | #遍历所有轮廓 43 | cnt = contours[i] 44 | 45 | #计算轮廓面积 46 | area = cv2.contourArea(cnt) 47 | 48 | #寻找最小矩形 49 | rect = cv2.minAreaRect(cnt) 50 | 51 | #轮廓的四个点坐标 52 | box = cv2.boxPoints(rect) 53 | box = np.int0(box) 54 | 55 | # 计算高和宽 56 | height = abs(box[0][1] - box[2][1]) 57 | width = abs(box[0][0] - box[2][0]) 58 | 59 | #过滤太细矩形 60 | if(height > width * 1.5): 61 | continue 62 | 63 | region.append(box) 64 | 65 | #定位的文字用绿线绘制轮廓 66 | for box in region: 67 | print(box) 68 | cv2.drawContours(img, [box], 0, (0, 255, 0), 2) 69 | 70 | #显示图像 71 | cv2.imshow('Median Blur', median) 72 | cv2.imshow('Gray Image', gray) 73 | cv2.imshow('Sobel Image', sobel) 74 | cv2.imshow('Binary Image', binary) 75 | cv2.imshow('Dilation Image', dilation) 76 | cv2.imshow('Erosion Image', erosion) 77 | cv2.imshow('Result Image', img) 78 | cv2.waitKey(0) 79 | cv2.destroyAllWindows() 80 | -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_1.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_11.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_12.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_13.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_15.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_16.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_17.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_2.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_3.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_4.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_6.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/figure_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/figure_9.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/hua.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/hua.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/scenery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/scenery.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/test.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/test01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/test01.png -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/test02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/test02.jpg -------------------------------------------------------------------------------- /chapter14-ImageSegmentation/word.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter14-ImageSegmentation/word.png -------------------------------------------------------------------------------- /chapter15-fft&hough/Lena.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/Lena.bmp -------------------------------------------------------------------------------- /chapter15-fft&hough/Lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/Lena.png -------------------------------------------------------------------------------- /chapter15-fft&hough/Na.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/Na.png -------------------------------------------------------------------------------- /chapter15-fft&hough/QQ截图20190423145246.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/QQ截图20190423145246.png -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_01.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 as cv 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | import matplotlib 6 | 7 | #读取图像 8 | img = cv.imread('test.png', 0) 9 | 10 | #快速傅里叶变换算法得到频率分布 11 | f = np.fft.fft2(img) 12 | 13 | #默认结果中心点位置是在左上角, 14 | #调用fftshift()函数转移到中间位置 15 | fshift = np.fft.fftshift(f) 16 | 17 | #fft结果是复数, 其绝对值结果是振幅 18 | fimg = np.log(np.abs(fshift)) 19 | 20 | #设置字体 21 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 22 | 23 | #展示结果 24 | plt.subplot(121), plt.imshow(img, 'gray'), plt.title('(a)原始图像') 25 | plt.axis('off') 26 | plt.subplot(122), plt.imshow(fimg, 'gray'), plt.title('(b)傅里叶变换处理') 27 | plt.axis('off') 28 | plt.show() 29 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_02.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 as cv 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | 6 | #读取图像 7 | img = cv.imread('Na.png', 0) 8 | 9 | #傅里叶变换 10 | f = np.fft.fft2(img) 11 | 12 | #转移像素做幅度普 13 | fshift = np.fft.fftshift(f) 14 | 15 | #取绝对值:将复数变化成实数取对数的目的为了将数据变化到0-255 16 | res = np.log(np.abs(fshift)) 17 | 18 | #展示结果 19 | plt.subplot(121), plt.imshow(img, 'gray'), plt.title('Original Image') 20 | plt.subplot(122), plt.imshow(res, 'gray'), plt.title('Fourier Image') 21 | plt.show() 22 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_03.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 as cv 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | 6 | #读取图像 7 | img = cv.imread('Lena.png', 0) 8 | 9 | #傅里叶变换 10 | f = np.fft.fft2(img) 11 | fshift = np.fft.fftshift(f) 12 | res = np.log(np.abs(fshift)) 13 | 14 | #傅里叶逆变换 15 | ishift = np.fft.ifftshift(fshift) 16 | iimg = np.fft.ifft2(ishift) 17 | iimg = np.abs(iimg) 18 | 19 | #展示结果 20 | plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original Image') 21 | plt.axis('off') 22 | plt.subplot(132), plt.imshow(res, 'gray'), plt.title('Fourier Image') 23 | plt.axis('off') 24 | plt.subplot(133), plt.imshow(iimg, 'gray'), plt.title('Inverse Fourier Image') 25 | plt.axis('off') 26 | plt.show() 27 | 28 | rows, cols = img.shape 29 | crow,ccol = int(rows/2), int(cols/2) 30 | fshift[crow-30:crow+30, ccol-30:ccol+30] = 0 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_04.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | import cv2 4 | from matplotlib import pyplot as plt 5 | import matplotlib 6 | 7 | #读取图像 8 | img = cv2.imread('Lena.png', 0) 9 | 10 | #傅里叶变换 11 | dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) 12 | 13 | #将频谱低频从左上角移动至中心位置 14 | dft_shift = np.fft.fftshift(dft) 15 | 16 | #频谱图像双通道复数转换为0-255区间 17 | result = 20*np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1])) 18 | 19 | #设置字体 20 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 21 | 22 | #显示图像 23 | plt.subplot(121), plt.imshow(img, cmap = 'gray') 24 | plt.title(u'(a)原始图像'), plt.xticks([]), plt.yticks([]) 25 | plt.subplot(122), plt.imshow(result, cmap = 'gray') 26 | plt.title(u'(b)傅里叶变换处理'), plt.xticks([]), plt.yticks([]) 27 | plt.show() 28 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_05.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | import cv2 4 | from matplotlib import pyplot as plt 5 | import matplotlib 6 | 7 | #读取图像 8 | img = cv2.imread('Lena.png', 0) 9 | 10 | #傅里叶变换 11 | dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) 12 | dftshift = np.fft.fftshift(dft) 13 | res1= 20*np.log(cv2.magnitude(dftshift[:,:,0], dftshift[:,:,1])) 14 | 15 | #傅里叶逆变换 16 | ishift = np.fft.ifftshift(dftshift) 17 | iimg = cv2.idft(ishift) 18 | res2 = cv2.magnitude(iimg[:,:,0], iimg[:,:,1]) 19 | 20 | #设置字体 21 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 22 | 23 | #显示图像 24 | plt.subplot(131), plt.imshow(img, 'gray'), plt.title(u'(a)原始图像') 25 | plt.axis('off') 26 | plt.subplot(132), plt.imshow(res1, 'gray'), plt.title(u'(b)傅里叶变换处理') 27 | plt.axis('off') 28 | plt.subplot(133), plt.imshow(res2, 'gray'), plt.title(u'(b)傅里叶变换逆处理') 29 | plt.axis('off') 30 | plt.show() 31 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_06.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 as cv 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | import matplotlib 6 | 7 | #读取图像 8 | img = cv.imread('Na.png', 0) 9 | 10 | #傅里叶变换 11 | f = np.fft.fft2(img) 12 | fshift = np.fft.fftshift(f) 13 | 14 | #设置高通滤波器 15 | rows, cols = img.shape 16 | crow,ccol = int(rows/2), int(cols/2) 17 | fshift[crow-30:crow+30, ccol-30:ccol+30] = 0 18 | 19 | #傅里叶逆变换 20 | ishift = np.fft.ifftshift(fshift) 21 | iimg = np.fft.ifft2(ishift) 22 | iimg = np.abs(iimg) 23 | 24 | #设置字体 25 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 26 | 27 | #显示原始图像和高通滤波处理图像 28 | plt.subplot(121), plt.imshow(img, 'gray'), plt.title(u'(a)原始图像') 29 | plt.axis('off') 30 | plt.subplot(122), plt.imshow(iimg, 'gray'), plt.title(u'(b)结果图像') 31 | plt.axis('off') 32 | plt.show() 33 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_07.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | 6 | #读取图像 7 | img = cv2.imread('Na.png', 0) 8 | 9 | #傅里叶变换 10 | dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) 11 | fshift = np.fft.fftshift(dft) 12 | 13 | #设置低通滤波器 14 | rows, cols = img.shape 15 | crow,ccol = int(rows/2), int(cols/2) #中心位置 16 | mask = np.zeros((rows, cols, 2), np.uint8) 17 | mask[crow-30:crow+30, ccol-30:ccol+30] = 1 18 | 19 | #掩膜图像和频谱图像乘积 20 | f = fshift * mask 21 | print(f.shape, fshift.shape, mask.shape) 22 | 23 | 24 | #傅里叶逆变换 25 | ishift = np.fft.ifftshift(f) 26 | iimg = cv2.idft(ishift) 27 | res = cv2.magnitude(iimg[:,:,0], iimg[:,:,1]) 28 | 29 | #显示原始图像和低通滤波处理图像 30 | plt.subplot(121), plt.imshow(img, 'gray'), plt.title('Original Image') 31 | plt.axis('off') 32 | plt.subplot(122), plt.imshow(res, 'gray'), plt.title('Result Image') 33 | plt.axis('off') 34 | plt.show() 35 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_08.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | import matplotlib 6 | 7 | #读取图像 8 | img = cv2.imread('lines.png') 9 | 10 | #灰度变换 11 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #转换为二值图像 14 | edges = cv2.Canny(gray, 50, 150) 15 | 16 | #显示原始图像 17 | plt.subplot(121), plt.imshow(edges, 'gray'), plt.title(u'(a)原始图像') 18 | plt.axis('off') 19 | 20 | #霍夫变换检测直线 21 | lines = cv2.HoughLines(edges, 1, np.pi / 180, 160) 22 | 23 | #转换为二维 24 | line = lines[:, 0, :] 25 | 26 | #将检测的线在极坐标中绘制 27 | for rho,theta in line[:]: 28 | a = np.cos(theta) 29 | b = np.sin(theta) 30 | x0 = a * rho 31 | y0 = b * rho 32 | print(x0, y0) 33 | x1 = int(x0 + 1000 * (-b)) 34 | y1 = int(y0 + 1000 * (a)) 35 | x2 = int(x0 - 1000 * (-b)) 36 | y2 = int(y0 - 1000 * (a)) 37 | print(x1, y1, x2, y2) 38 | #绘制直线 39 | cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 1) 40 | 41 | #设置字体 42 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 43 | 44 | #显示处理图像 45 | plt.subplot(122), plt.imshow(img, 'gray'), plt.title(u'(b)结果图像') 46 | plt.axis('off') 47 | plt.show() 48 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_09.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | import matplotlib 6 | 7 | #读取图像 8 | img = cv2.imread('judge.png') 9 | 10 | #灰度转换 11 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | #转换为二值图像 14 | edges = cv2.Canny(gray, 50, 200) 15 | 16 | #显示原始图像 17 | plt.subplot(121), plt.imshow(edges, 'gray'), plt.title(u'(a)原始图像') 18 | plt.axis('off') 19 | 20 | #霍夫变换检测直线 21 | minLineLength = 60 22 | maxLineGap = 10 23 | lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, minLineLength, maxLineGap) 24 | 25 | #绘制直线 26 | lines1 = lines[:, 0, :] 27 | for x1,y1,x2,y2 in lines1[:]: 28 | cv2.line(img, (x1,y1), (x2,y2), (255,0,0), 2) 29 | 30 | res = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 31 | 32 | #设置字体 33 | matplotlib.rcParams['font.sans-serif']=['SimHei'] 34 | 35 | #显示处理图像 36 | plt.subplot(122), plt.imshow(res), plt.title(u'(b)结果图像') 37 | plt.axis('off') 38 | plt.show() 39 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_10.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | 6 | #读取图像 7 | img = cv2.imread('test01.png') 8 | 9 | #灰度转换 10 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 11 | 12 | #显示原始图像 13 | plt.subplot(121), plt.imshow(gray, 'gray'), plt.title('Input Image') 14 | plt.axis('off') 15 | 16 | #霍夫变换检测圆 17 | #circles1 = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, 18 | # param1=100, param2=30, minRadius=200, maxRadius=300) 19 | 20 | circles1 = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param2=30) 21 | 22 | print(circles1) 23 | 24 | #提取为二维 25 | circles = circles1[0, :, :] 26 | 27 | #四舍五入取整 28 | circles = np.uint16(np.around(circles)) 29 | 30 | #绘制圆 31 | for i in circles[:]: 32 | cv2.circle(img, (i[0],i[1]), i[2], (255,0,0), 5) #画圆 33 | cv2.circle(img, (i[0],i[1]), 2, (255,0,255), 10) #画圆心 34 | 35 | #显示处理图像 36 | plt.subplot(122), plt.imshow(img), plt.title('Result Image') 37 | plt.axis('off') 38 | plt.show() 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter15-fft&hough/chapter15_11.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | 6 | #读取图像 7 | img = cv2.imread('eyes.png') 8 | 9 | #灰度转换 10 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 11 | 12 | #显示原始图像 13 | plt.subplot(121), plt.imshow(gray, 'gray'), plt.title('Input Image') 14 | plt.axis('off') 15 | 16 | #霍夫变换检测圆 17 | circles1 = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, 18 | param1=100, param2=30, 19 | minRadius=160, maxRadius=200) 20 | print(circles1) 21 | 22 | #提取为二维 23 | circles = circles1[0, :, :] 24 | 25 | #四舍五入取整 26 | circles = np.uint16(np.around(circles)) 27 | 28 | #绘制圆 29 | for i in circles[:]: 30 | cv2.circle(img, (i[0],i[1]), i[2], (255,0,0), 5) #画圆 31 | cv2.circle(img, (i[0],i[1]), 2, (255,0,255), 8) #画圆心 32 | 33 | #显示处理图像 34 | plt.subplot(122), plt.imshow(img), plt.title('Result Image') 35 | plt.axis('off') 36 | plt.show() 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /chapter15-fft&hough/eyes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/eyes.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_1.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_10.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_11.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_12.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_13.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_2.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_3.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_4.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_5.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_6.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_7.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_8.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_9.png -------------------------------------------------------------------------------- /chapter15-fft&hough/figure_99.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/figure_99.png -------------------------------------------------------------------------------- /chapter15-fft&hough/judge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/judge.png -------------------------------------------------------------------------------- /chapter15-fft&hough/lines.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/lines.png -------------------------------------------------------------------------------- /chapter15-fft&hough/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/test.png -------------------------------------------------------------------------------- /chapter15-fft&hough/test01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/test01.png -------------------------------------------------------------------------------- /chapter15-fft&hough/test02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eastmountyxz/Book3-Python-ImageProcessing/2247ff422b13a39ac45cb002fb7ff4f06f08efbe/chapter15-fft&hough/test02.png --------------------------------------------------------------------------------