├── README.md
├── edge detector
├── .idea
│ ├── edge detector.iml
│ ├── misc.xml
│ ├── modules.xml
│ ├── vcs.xml
│ └── workspace.xml
├── __pycache__
│ └── detector.cpython-37.pyc
├── demo.py
└── detector.py
├── histogram equalization
├── .idea
│ ├── 4.iml
│ ├── misc.xml
│ ├── modules.xml
│ ├── vcs.xml
│ └── workspace.xml
├── __pycache__
│ └── enhancement.cpython-37.pyc
├── demo.py
└── enhancement.py
├── image interpolation
├── .idea
│ ├── image interpolation.iml
│ ├── misc.xml
│ ├── modules.xml
│ ├── vcs.xml
│ └── workspace.xml
├── __pycache__
│ └── interpolation.cpython-37.pyc
├── demo.py
└── interpolation.py
├── image segmentation based on k-means
├── .idea
│ ├── image segmentation based on k-means.iml
│ ├── misc.xml
│ ├── modules.xml
│ ├── other.xml
│ ├── vcs.xml
│ └── workspace.xml
├── __pycache__
│ └── segamentation.cpython-37.pyc
├── demo.py
└── segamentation.py
├── salt pepper noise and median denoising
├── .idea
│ ├── code.iml
│ ├── misc.xml
│ ├── modules.xml
│ ├── vcs.xml
│ └── workspace.xml
├── __pycache__
│ ├── denoising.cpython-37.pyc
│ └── noise.cpython-37.pyc
├── demo.py
├── denoising.py
└── noise.py
└── wiener filtering and gaussian white noise
├── .idea
├── misc.xml
├── modules.xml
├── vcs.xml
├── wiener filtering and gaussian white noise.iml
└── workspace.xml
├── __pycache__
├── filtering.cpython-37.pyc
└── noise.cpython-37.pyc
├── demo.py
├── filtering.py
└── noise.py
/README.md:
--------------------------------------------------------------------------------
1 | # Digital-Image-Processing
2 | My A specialty course called Digital Image processing , including code and notes
3 |
4 | # Notes
5 | - [click here](https://www.jianshu.com/nb/30347125)
6 |
--------------------------------------------------------------------------------
/edge detector/.idea/edge detector.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/edge detector/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/edge detector/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/edge detector/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/edge detector/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 | 1559218018976
154 |
155 |
156 | 1559218018976
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
--------------------------------------------------------------------------------
/edge detector/__pycache__/detector.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/edge detector/__pycache__/detector.cpython-37.pyc
--------------------------------------------------------------------------------
/edge detector/demo.py:
--------------------------------------------------------------------------------
1 | import detector
2 | import skimage as sk
3 | from matplotlib import pyplot as plt
4 | import numpy as np
5 |
6 | if __name__ == '__main__':
7 |
8 | image = sk.data.coins()
9 |
10 | threshold = 70
11 | image_point = detector.point(image, threshold)
12 |
13 | plt.subplot(1, 2, 1)
14 | plt.title("Source Image")
15 | plt.imshow(image, cmap="gray")
16 |
17 | plt.subplot(1, 2, 2)
18 | plt.title("Image with point detector: threshold=" + str(threshold))
19 | plt.imshow(image_point, cmap="gray")
20 |
21 | plt.show()
22 |
--------------------------------------------------------------------------------
/edge detector/detector.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 |
3 | def point(intput_signal, threshold):
4 | '''
5 | 点检测(使用于灰度图)
6 | :param intput_signal: 输入图像
7 | :param threshold: 拉普拉斯算子阈值
8 | :return: 点图像
9 | '''
10 | intput_signal_cp = np.copy(intput_signal) # 输入图像的副本
11 |
12 | m, n = intput_signal_cp.shape # 输入图像的尺寸(行、列)
13 |
14 | output_signal = np.zeros((m, n)) # 检测点的输出
15 |
16 | # 空间滤波模板
17 | filtering_template = np.array([
18 | [0, 1, 0],
19 | [1, -4, 1],
20 | [0, 1, 0]
21 | ])
22 |
23 | point_matrix = np.zeros((3, 3)) # 矩阵灰度值,用于计算拉普拉斯算子
24 |
25 | for i in range(m):
26 | for j in range(n):
27 | # 该像素点灰度值
28 | point_matrix[1, 1] = intput_signal_cp[i, j]
29 |
30 | # 该像素点周围的点的灰度值,如若已至边界则视为0
31 | if i - 1 >= 0:
32 | point_matrix[0, 1] = intput_signal_cp[i-1, j]
33 | if i + 1 < m:
34 | point_matrix[2, 1] = intput_signal_cp[i+1, j]
35 | if j - 1 >= 0:
36 | point_matrix[1, 0] = intput_signal_cp[i, j-1]
37 | if j + 1 < n:
38 | point_matrix[1, 2] = intput_signal_cp[i, j+1]
39 |
40 | temp = abs(np.sum(point_matrix * filtering_template)) # 拉普拉斯算子值计算
41 |
42 | if temp >= threshold:
43 | output_signal[i, j] = 255 # 拉普拉斯算子大于阈值,则为亮点
44 | else:
45 | output_signal[i, j] = 0 #否则为暗点
46 |
47 | point_matrix = np.zeros((3, 3)) # 重置,为下一个点做准备
48 |
49 | return output_signal
--------------------------------------------------------------------------------
/histogram equalization/.idea/4.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/histogram equalization/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/histogram equalization/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/histogram equalization/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/histogram equalization/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 | 1558620397956
136 |
137 |
138 | 1558620397956
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
--------------------------------------------------------------------------------
/histogram equalization/__pycache__/enhancement.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/histogram equalization/__pycache__/enhancement.cpython-37.pyc
--------------------------------------------------------------------------------
/histogram equalization/demo.py:
--------------------------------------------------------------------------------
1 | import skimage as sk
2 | from matplotlib import pyplot as plt
3 | import numpy as np
4 | import enhancement
5 |
6 | if __name__ == '__main__':
7 |
8 | imgae = sk.data.camera()
9 | imgae_hist = enhancement.hist_equalization(imgae)
10 |
11 | # 作灰度图对比
12 | plt.figure(1)
13 | plt.subplot(1, 2, 1)
14 | plt.title("Source Image")
15 | plt.imshow(imgae, cmap="gray")
16 |
17 | plt.subplot(1, 2, 2)
18 | plt.title("Image processed by Histogram equalization")
19 | plt.imshow(imgae_hist, cmap="gray")
20 |
21 | # 作直方图对比
22 | plt.figure(2)
23 | plt.subplot(1, 2, 1)
24 | plt.title("Histogram of the source image")
25 | plt.hist(np.array(imgae).flatten(), bins=256)
26 |
27 | plt.subplot(1, 2, 2)
28 | plt.title("Histogram of the image processed by Histogram equalization")
29 | plt.hist(np.array(imgae_hist).flatten(), bins=256)
30 | plt.show()
31 |
--------------------------------------------------------------------------------
/histogram equalization/enhancement.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 |
3 |
4 | def hist_equalization(intput_signal):
5 | '''
6 | 直方图均衡(适用于灰度图)
7 | :param intput_signal: 输入图像
8 | :return: 直方图均衡化后的输出图像
9 | '''
10 |
11 | output_signal = np.copy(intput_signal) # 输出图像,初始化为输入
12 |
13 | intput_signal_cp = np.copy(intput_signal) # 输入图像的副本
14 |
15 | m, n = intput_signal_cp.shape # 输入图像的尺寸(行、列)
16 |
17 | pixel_total_num = m * n # 输入图像的像素点总数
18 |
19 | p_r = [] # 输入图像的概率密度分布
20 | p_s = [] # 输出图像的概率密度分布
21 |
22 | # 求输入图像的概率密度分布函数
23 | for i in range(256):
24 | p_r.append(np.sum(intput_signal_cp == i) / pixel_total_num)
25 |
26 | # 求输出图像的概率密度分布函数
27 | single_pixel_class_probobility_t = 0 # 临时存储某一灰度级的概率
28 | for i in range(256):
29 | single_pixel_class_probobility_t = single_pixel_class_probobility_t + p_r[i]
30 | p_s.append(single_pixel_class_probobility_t)
31 |
32 | # 求解变换后的输出图像
33 | for i in range(256):
34 | output_signal[np.where(intput_signal_cp == i)] = 255 * p_s[i]
35 |
36 | return output_signal
--------------------------------------------------------------------------------
/image interpolation/.idea/image interpolation.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/image interpolation/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/image interpolation/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/image interpolation/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/image interpolation/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 | 1558934760632
145 |
146 |
147 | 1558934760632
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
--------------------------------------------------------------------------------
/image interpolation/__pycache__/interpolation.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/image interpolation/__pycache__/interpolation.cpython-37.pyc
--------------------------------------------------------------------------------
/image interpolation/demo.py:
--------------------------------------------------------------------------------
1 | import skimage as sk
2 | import interpolation
3 | from matplotlib import pyplot as plt
4 | import numpy as np
5 |
6 | if __name__ == '__main__':
7 |
8 | image = sk.data.coins()
9 |
10 | zoom_multiples = 0.5
11 | image_processed_with_nearest_neighbor = interpolation.nearest_neighbor(image, zoom_multiples)
12 |
13 | image_processed_with_linear = interpolation.double_linear(image, zoom_multiples)
14 |
15 | plt.imsave("image.png", image)
16 | plt.imsave("image_processed_with_nearest_neighbor.png", image_processed_with_nearest_neighbor)
17 | plt.imsave("image_processed_with_linear.png", image_processed_with_linear)
18 |
19 |
--------------------------------------------------------------------------------
/image interpolation/interpolation.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import math
3 |
4 | def nearest_neighbor(input_signal, zoom_multiples):
5 | '''
6 | 最近邻插值(适用于灰度图)
7 | :param input_signal: 输入图像
8 | :param zoom_multiples: 缩放倍数
9 | :return: 缩放后的图像
10 | '''
11 | input_signal_cp = np.copy(input_signal) # 输入图像的副本
12 |
13 | input_row, input_col = input_signal_cp.shape # 输入图像的尺寸(行、列)
14 |
15 | # 输出图像的尺寸
16 | output_row = int(input_row * zoom_multiples)
17 | output_col = int(input_col * zoom_multiples)
18 |
19 | output_signal = np.zeros((output_row, output_col)) # 输出图片
20 |
21 | for i in range(output_row):
22 | for j in range(output_col):
23 | # 输出图片中坐标 (i,j)对应至输入图片中的(m,n)
24 | m = round(i / output_row * input_row)
25 | n = round(j / output_col * input_col)
26 | # 防止四舍五入后越界
27 | if m >= input_row:
28 | m = input_row - 1
29 | if n >= input_col:
30 | n = input_col - 1
31 | # 插值
32 | output_signal[i, j] = input_signal_cp[m, n]
33 |
34 | return output_signal
35 |
36 |
37 | def double_linear(input_signal, zoom_multiples):
38 | '''
39 | 双线性插值
40 | :param input_signal: 输入图像
41 | :param zoom_multiples: 放大倍数
42 | :return: 双线性插值后的图像
43 | '''
44 | input_signal_cp = np.copy(input_signal) # 输入图像的副本
45 |
46 | input_row, input_col = input_signal_cp.shape # 输入图像的尺寸(行、列)
47 |
48 | # 输出图像的尺寸
49 | output_row = int(input_row * zoom_multiples)
50 | output_col = int(input_col * zoom_multiples)
51 |
52 | output_signal = np.zeros((output_row, output_col)) # 输出图片
53 |
54 | for i in range(output_row):
55 | for j in range(output_col):
56 | # 输出图片中坐标 (i,j)对应至输入图片中的最近的四个点点(x1,y1)(x2, y2),(x3, y3),(x4,y4)的均值
57 | temp_x = i / output_row * input_row
58 | temp_y = j / output_col * input_col
59 |
60 | x1 = int(temp_x)
61 | y1 = int(temp_y)
62 |
63 | x2 = x1
64 | y2 = y1 + 1
65 |
66 | x3 = x1 + 1
67 | y3 = y1
68 |
69 | x4 = x1 + 1
70 | y4 = y1 + 1
71 |
72 | u = temp_x - x1
73 | v = temp_y - y1
74 |
75 | # 防止越界
76 | if x4 >= input_row:
77 | x4 = input_row - 1
78 | x2 = x4
79 | x1 = x4 - 1
80 | x3 = x4 - 1
81 | if y4 >= input_col:
82 | y4 = input_col - 1
83 | y3 = y4
84 | y1 = y4 - 1
85 | y2 = y4 - 1
86 |
87 | # 插值
88 | output_signal[i, j] = (1-u)*(1-v)*int(input_signal_cp[x1, y1]) + (1-u)*v*int(input_signal_cp[x2, y2]) + u*(1-v)*int(input_signal_cp[x3, y3]) + u*v*int(input_signal_cp[x4, y4])
89 | return output_signal
--------------------------------------------------------------------------------
/image segmentation based on k-means/.idea/image segmentation based on k-means.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/image segmentation based on k-means/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/image segmentation based on k-means/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/image segmentation based on k-means/.idea/other.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/image segmentation based on k-means/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/image segmentation based on k-means/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 | 1560082423403
162 |
163 |
164 | 1560082423403
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
--------------------------------------------------------------------------------
/image segmentation based on k-means/__pycache__/segamentation.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/image segmentation based on k-means/__pycache__/segamentation.cpython-37.pyc
--------------------------------------------------------------------------------
/image segmentation based on k-means/demo.py:
--------------------------------------------------------------------------------
1 | from skimage import data
2 | from skimage import io
3 | from matplotlib import pyplot as plt
4 | import segamentation
5 |
6 | if __name__ == '__main__':
7 |
8 |
9 | image = data.coins()
10 | k = 3
11 | threshold = 1
12 | labels = segamentation.k_means(image, k, threshold)
13 |
14 | plt.subplot(1, 2, 1)
15 | plt.title("Soucre Image")
16 | plt.imshow(image,cmap="gray")
17 | plt.subplot(1, 2, 2)
18 | plt.title("Segamenting Image with k-means\n" + "k=" + str(k) + " threshold=" + str(threshold))
19 | plt.imshow(labels/3)
20 | plt.show()
--------------------------------------------------------------------------------
/image segmentation based on k-means/segamentation.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import random
3 |
4 | def loss_function(present_center, pre_center):
5 | '''
6 | 损失函数,计算上一次与当前聚类中的差异(像素差的平方和)
7 | :param present_center: 当前聚类中心
8 | :param pre_center: 上一次聚类中心
9 | :return: 损失值
10 | '''
11 | present_center = np.array(present_center)
12 | pre_center = np.array(pre_center)
13 | return np.sum((present_center - pre_center)**2)
14 |
15 |
16 | def classifer(intput_signal, center):
17 | '''
18 | 分类器(通过当前的聚类中心,给输入图像分类)
19 | :param intput_signal: 输入图像
20 | :param center: 聚类中心
21 | :return: 标签矩阵
22 | '''
23 | input_row, input_col= intput_signal.shape # 输入图像的尺寸
24 |
25 | pixls_labels = np.zeros((input_row, input_col)) # 储存所有像素标签
26 |
27 | pixl_distance_t = [] # 单个元素与所有聚类中心的距离,临时用
28 |
29 | for i in range(input_row):
30 | for j in range(input_col):
31 | # 计算每个像素与所有聚类中心的差平方
32 | for k in range(len(center)):
33 | distance_t = np.sum(abs((intput_signal[i, j]).astype(int) - center[k].astype(int))**2)
34 | pixl_distance_t.append(distance_t)
35 | # 差异最小则为该类
36 | pixls_labels[i, j] = int(pixl_distance_t.index(min(pixl_distance_t)))
37 | # 清空该list,为下一个像素点做准备
38 | pixl_distance_t = []
39 | return pixls_labels
40 |
41 |
42 | def k_means(input_signal, center_num, threshold):
43 | '''
44 | 基于k-means算法的图像分割(适用于灰度图)
45 | :param input_signal: 输入图像
46 | :param center_num: 聚类中心数目
47 | :param threshold: 迭代阈值
48 | :return:
49 | '''
50 | input_signal_cp = np.copy(input_signal) # 输入信号的副本
51 | input_row, input_col = input_signal_cp.shape # 输入图像的尺寸
52 | pixls_labels = np.zeros((input_row, input_col)) # 储存所有像素标签
53 |
54 | # 随机初始聚类中心行标与列标
55 | initial_center_row_num = [i for i in range(input_row)]
56 | random.shuffle(initial_center_row_num)
57 | initial_center_row_num = initial_center_row_num[:center_num]
58 |
59 | initial_center_col_num = [i for i in range(input_col)]
60 | random.shuffle(initial_center_col_num)
61 | initial_center_col_num = initial_center_col_num[:center_num]
62 |
63 | # 当前的聚类中心
64 | present_center = []
65 | for i in range(center_num):
66 | present_center.append(input_signal_cp[initial_center_row_num[i], initial_center_row_num[i]])
67 | pixls_labels = classifer(input_signal_cp, present_center)
68 |
69 | num = 0 # 用于记录迭代次数
70 | while True:
71 | pre_centet = present_center.copy() # 储存前一次的聚类中心
72 | # 计算当前聚类中心
73 | for n in range(center_num):
74 | temp = np.where(pixls_labels == n)
75 | present_center[n] = sum(input_signal_cp[temp].astype(int)) / len(input_signal_cp[temp])
76 | # 根据当前聚类中心分类
77 | pixls_labels = classifer(input_signal_cp, present_center)
78 | # 计算上一次聚类中心与当前聚类中心的差异
79 | loss = loss_function(present_center, pre_centet)
80 | num = num + 1
81 | print("Step:"+ str(num) + " Loss:" + str(loss))
82 | # 当损失小于迭代阈值时,结束迭代
83 | if loss <= threshold:
84 | break
85 | return pixls_labels
86 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/.idea/code.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 | 1558447228813
117 |
118 |
119 | 1558447228813
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/__pycache__/denoising.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/salt pepper noise and median denoising/__pycache__/denoising.cpython-37.pyc
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/__pycache__/noise.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/salt pepper noise and median denoising/__pycache__/noise.cpython-37.pyc
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/demo.py:
--------------------------------------------------------------------------------
1 | import skimage as sk
2 | import matplotlib.pyplot as plt
3 | import noise
4 | import denoising
5 |
6 | if __name__ == '__main__':
7 |
8 | noisy_probability = 0.1 # 椒盐噪声概率
9 |
10 | # 加椒盐噪声
11 | camera_processed_with_salt_pepper, actual_noisy_data_num, theory_noisy_data_num = noise.salt_pepper(sk.data.camera(), noisy_probability)
12 |
13 | # 中值滤波处理
14 | camera_median = denoising.median_filtering(camera_processed_with_salt_pepper)
15 |
16 | plt.figure()
17 | plt.subplot(1, 3, 1)
18 | plt.title("Source Image")
19 | plt.imshow(sk.data.camera(), cmap="gray")
20 | plt.subplot(1, 3, 2)
21 | plt.title("Image processed by Salt Pepper Noise: " + str(noisy_probability))
22 | plt.imshow(camera_processed_with_salt_pepper, cmap="gray")
23 | plt.subplot(1, 3, 3)
24 | plt.title("Image Processed by Median Filtering")
25 | plt.imshow(camera_median, cmap="gray")
26 | plt.show()
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/denoising.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import random
3 |
4 | def median_filtering(input_signal):
5 | '''
6 | 中值滤波(适用于灰度图)
7 | 如:
8 | - + -
9 | + * +
10 | - + -
11 | * 为噪点,滤波方法为:取4个+的中位数(若某+不在输入信号范围内,则随机添加0或255)
12 | :param input_signal: 输入信号矩阵(2D)
13 | :return: 滤波后的信号
14 | '''
15 | salt_pepper = [0, 255]
16 |
17 | m, n = input_signal.shape # 获取输入图片的尺寸(行和列)
18 |
19 | input_signal_cp = input_signal.copy() # 输入信号的副本
20 |
21 | nosiy_data_around = [] # 存放噪点上下左右的数据点
22 | # 遍历滤波
23 | for i in range(m):
24 | for j in range(n):
25 | # 当灰度值为0或255时,则认为该数据点为椒盐噪点
26 | if input_signal_cp[i, j] == 255 or input_signal_cp[i, j] == 0:
27 | # 每次无效数据点(即不再范围内)为4,每有一个在范围内,即-1
28 | invalid_data_per = 4
29 | if i + 1 < n:
30 | nosiy_data_around.append(input_signal_cp[i + 1, j])
31 | invalid_data_per = invalid_data_per - 1
32 | if i - 1 >= 0:
33 | nosiy_data_around.append(input_signal_cp[i - 1, j])
34 | invalid_data_per = invalid_data_per - 1
35 | if j + 1 < m:
36 | nosiy_data_around.append(input_signal_cp[i, j + 1])
37 | invalid_data_per = invalid_data_per - 1
38 | if j - 1 >= 0:
39 | nosiy_data_around.append(input_signal_cp[i, j - 1])
40 | invalid_data_per = invalid_data_per - 1
41 | else:
42 | if invalid_data_per > 0:
43 | # 根据无效数据点的个数,随机添加0或255
44 | for k in range(invalid_data_per):
45 | nosiy_data_around.append(salt_pepper[random.randint(0, 1)])
46 | # 取中位数
47 | input_signal_cp[i, j] = np.median(nosiy_data_around)
48 |
49 | # 该噪点的周围数据数组清空,为下一个噪点周围数据存在做准备
50 | nosiy_data_around = []
51 |
52 | return input_signal_cp
53 |
54 |
--------------------------------------------------------------------------------
/salt pepper noise and median denoising/noise.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import random
3 |
4 | def salt_pepper(intput_signal, probability):
5 | '''
6 | 椒盐噪声算法(适用于灰度图)
7 | :param intput_signal: 输入信号矩阵(2D)
8 | :param probability: 噪声概率(如:0.1为10%)
9 | :return: 加噪后的图像、实际噪点数、理论噪点数
10 | '''
11 | nisy = [0, 255] # 噪声(salt, papper)
12 |
13 | m, n= intput_signal.shape # 获取输入图片尺寸(行和列)
14 |
15 | intput_signal_cp = intput_signal.copy() # 输入信号的副本
16 | intput_signal_cp = intput_signal_cp.reshape(-1) # reshape为一个行向量
17 |
18 | # 该噪声概率下,理论上noisy_data_probability_num个数据点中1一个噪点
19 | noisy_data_probability_num = int(100 / (probability * 100))
20 |
21 | # 噪点数组,当数组中的值为1时,则认为对应的数据点为噪点
22 | noisy_data = []
23 | for i in range(m*n):
24 | noisy_data.append(random.randint(1, noisy_data_probability_num))
25 |
26 | # 实际噪点数与理论噪点数
27 | actual_noisy_data_num = 0
28 | theory_noisy_data_num = int(m * n * probability)
29 |
30 | # 添加噪点
31 | for i in range(m*n):
32 | if noisy_data[i] == 1:
33 | actual_noisy_data_num = actual_noisy_data_num + 1
34 | intput_signal_cp[i] = nisy[random.randint(0, 1)]
35 |
36 | # 重塑至m*n的矩阵
37 | intput_signal_cp = intput_signal_cp.reshape(m, n)
38 |
39 | return intput_signal_cp, actual_noisy_data_num, theory_noisy_data_num
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/.idea/wiener filtering and gaussian white noise.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 | 1558783978638
115 |
116 |
117 | 1558783978638
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/__pycache__/filtering.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/wiener filtering and gaussian white noise/__pycache__/filtering.cpython-37.pyc
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/__pycache__/noise.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iwuqing/Digital-Image-Processing/16a837df326a2fb3db0e166976d38a73fbe418c5/wiener filtering and gaussian white noise/__pycache__/noise.cpython-37.pyc
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/demo.py:
--------------------------------------------------------------------------------
1 | from matplotlib import pyplot as plt
2 | import skimage as sk
3 | import noise
4 | import numpy as np
5 | import filtering
6 | import math
7 | import scipy.signal
8 |
9 |
10 | if __name__ == '__main__':
11 |
12 | image = sk.data.moon()
13 |
14 | # 抖动噪声
15 | shake_noise = np.eye(20) / 5
16 |
17 | # 噪声与源图像卷积
18 | image_add_shake = scipy.signal.convolve2d(image, shake_noise, mode = 'same')
19 |
20 | # 增加加性高斯白噪声
21 | image_add_shake_and_gwn = noise.gaussian_white_noise(image_add_shake, 0, 10)
22 |
23 | # 求解退化函数h
24 | image_fft = np.fft.fft2(image)
25 | image_add_noise_fft = np.fft.fft2(image_add_shake)
26 | h = np.fft.ifft2(image_add_noise_fft / image_fft)
27 |
28 | # 逆滤波
29 | image_processed_inverse_filtering = filtering.inverse_filtering(image_add_shake_and_gwn, h)
30 |
31 | # 维纳滤波
32 | K = 0.03
33 | image_processed_wiener_filtering = filtering.wiener_filtering(image_add_shake_and_gwn, h, K)
34 |
35 | plt.figure()
36 | plt.subplot(2, 2, 1)
37 | plt.title("Source Image")
38 | plt.imshow(image, cmap="gray")
39 |
40 | plt.subplot(2, 2, 2)
41 | plt.title("Image through H and Gaussian white noise(mu=0, sigma=10)")
42 | plt.imshow(image_add_shake_and_gwn, cmap="gray")
43 |
44 | plt.subplot(2, 2, 3)
45 | plt.title("Image processed with Inverse filtering")
46 | plt.imshow(image_processed_inverse_filtering, cmap="gray")
47 |
48 | plt.subplot(2, 2, 4)
49 | plt.title("Image processed with Wiener filtering: K=" + str(K))
50 | plt.imshow(image_processed_wiener_filtering, cmap="gray")
51 | plt.show()
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/filtering.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 |
3 |
4 | def inverse_filtering(input_singal, h):
5 | '''
6 | 逆滤波
7 | :param input_singal: 输入信号
8 | :param h: 退化函数(时域)
9 | :return: 滤波后的信号(幅值)
10 | '''
11 | output_signal = [] #输出信号
12 | output_signal_fft = [] #输出信号的傅里叶变换
13 |
14 | h_fft = np.fft.fft2(h) # 退化函数的傅里叶变换
15 |
16 | input_singal_fft = np.fft.fft2(input_singal) # 输入信号的傅里叶变换
17 |
18 | output_signal_fft = input_singal_fft / h_fft
19 |
20 | output_signal = np.abs(np.fft.ifft2(output_signal_fft))
21 |
22 | return output_signal
23 |
24 |
25 | def wiener_filtering(input_signal, h, K):
26 | '''
27 | 维纳滤波
28 | :param input_signal: 输入信号
29 | :param h: 退化函数(时域)
30 | :param K: 参数K
31 | :return: 维纳滤波后的信号(幅值)
32 | '''
33 | output_signal = [] # 输出信号
34 |
35 | output_signal_fft = [] # 输出信号的傅里叶变换
36 |
37 | input_signal_cp = np.copy(input_signal) # 输入信号的副本
38 |
39 | input_signal_cp_fft = np.fft.fft2(input_signal_cp) # 输入信号的傅里叶变换
40 |
41 | h_fft = np.fft.fft2(h) # 退化函数的傅里叶变换
42 |
43 | h_abs_square = np.abs(h_fft)**2 # 退化函数模值的平方
44 |
45 | # 维纳滤波
46 | output_signal_fft = np.conj(h_fft) / (h_abs_square + K)
47 |
48 | output_signal = np.abs(np.fft.ifft2(output_signal_fft * input_signal_cp_fft)) # 输出信号傅里叶反变换
49 |
50 | return output_signal
--------------------------------------------------------------------------------
/wiener filtering and gaussian white noise/noise.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import random
3 |
4 |
5 | def gaussian_white_noise(intput_signal, mu, sigma):
6 | '''
7 | 加性高斯白噪声(适用于灰度图)
8 | :param intput_signal: 输入图像
9 | :param mu: 均值
10 | :param sigma: 标准差
11 | :return:
12 | '''
13 | intput_signal_cp = np.copy(intput_signal) # 输入图像的副本
14 |
15 | m, n = intput_signal_cp.shape # 输入图像尺寸(行、列)
16 |
17 | # 添加高斯白噪声
18 | for i in range(m):
19 | for j in range(n):
20 | intput_signal_cp[i, j] = intput_signal_cp[i, j] + random.gauss(mu, sigma)
21 |
22 |
23 | return intput_signal_cp
24 |
--------------------------------------------------------------------------------