探究降噪合理性

区分度

合理的降噪结果应该是本身区分度大的地方更大,让本身区分度小的地方更小

因此我们拿一张原图,跟降噪后的的图片进行试验(这里就不给出图片样例了)

但是可以看一下结果:

img

代码如下:

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
# python3.8
# utf-8
import cv2 as cv
import matplotlib.pyplot as plt


# 考虑位置
def division(_8area):
result = [0] * 8
p = [_8area[0][0], _8area[0][1], _8area[0][2], _8area[1][2],
_8area[2][2], _8area[2][1], _8area[2][0], _8area[1][0]]
d = [int(p[0]) - int(p[1]), int(p[1]) - int(p[2]), int(p[2]) - int(p[3]), int(p[3]) - int(p[4]),
int(p[4]) - int(p[5]), int(p[5]) - int(p[6]), int(p[6]) - int(p[7]), int(p[7]) - int(p[0])]
max_index = 0
min_index = 0
for i in range(1, 8):
if d[i] > d[max_index]:
max_index = i
if d[i] < d[min_index]:
min_index = i
if max_index == min_index:
pass
elif max_index > min_index:
for i in range(0, min_index + 1):
result[i] = 1
for i in range(min_index + 1, max_index + 1):
result[i] = 2
if max_index < 7:
for i in range(max_index + 1, 8):
result[i] = 1
elif max_index < min_index:
for i in range(0, max_index + 1):
result[i] = 2
for i in range(max_index + 1, min_index + 1):
result[i] = 1
if min_index < 7:
for i in range(min_index + 1, 8):
result[i] = 2
# 求区分度
max1 = 0
for i in range(8):
if result[i] == 1 and p[i] > max1:
max1 = p[i]
min2 = 0
for i in range(8):
if result[i] == 2 and p[i] < min2:
min2 = p[i]
return max1 - min2


# 以2X2为单位求区分度
def get_difference(img):
row, col = img.shape
result = [0] * 256
for i in range(256):
result[i] = 0
for i in range(1, row - 1):
for j in range(1, col - 1):
# _8_area = type(img_channel)
_8_area = [[0] * 3 for i in range(3)]
_8_area[0][0] = img[i - 1][j - 1]
_8_area[0][1] = img[i - 1][j]
_8_area[0][2] = img[i - 1][j + 1]
_8_area[1][2] = img[i][j + 1]
_8_area[2][2] = img[i + 1][j + 1]
_8_area[2][1] = img[i + 1][j]
_8_area[2][0] = img[i + 1][j - 1]
_8_area[1][0] = img[i][j - 1]
result[division(_8_area)] += 1
return result


def main():
# 图像地址
origin_address = "origin.png"
denoise_address = "denoise.png"

# 以灰度方式读入图像
origin_img = cv.imread(origin_address, 0)
denoise_img = cv.imread(denoise_address, 0)

# 求区分度
origin_difference = get_difference(origin_img)
denoise_difference = get_difference(denoise_img)

# 制图
plt.plot(origin_difference, color='red')
plt.plot(denoise_difference, color='blue')
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用来正常显示负号
plt.rcParams['axes.unicode_minus'] = False
# 标题
plt.title('区分度折线图(红色原图/蓝色处理图)', fontsize=24, color='black')
plt.show()


if __name__ == '__main__':
main()

差异度

差异度为3X3小区块内,最大值减去最小值,将得到的值即为差异度,将各差异度累加(大小范围在0~255),制成折线图

效果图:

img

代码如下:

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
python# python3.8
# utf-8
import cv2 as cv
import matplotlib.pyplot as plt


def division(_8area):
result = [0] * 8
p = [_8area[0][0], _8area[0][1], _8area[0][2], _8area[1][2],
_8area[2][2], _8area[2][1], _8area[2][0], _8area[1][0]]

# 求差异度
max1 = 0
for i in range(8):
if p[i] > max1:
max1 = p[i]
min2 = 0
for i in range(8):
if p[i] < min2:
min2 = p[i]
return max1 - min2


# 求差异度
def get_d(img):
row, col = img.shape
result = [0] * 256
for i in range(256):
result[i] = 0
for i in range(1, row - 1):
for j in range(1, col - 1):
# _8_area = type(img_channel)
_8_area = [[0] * 3 for i in range(3)]
_8_area[0][0] = img[i - 1][j - 1]
_8_area[0][1] = img[i - 1][j]
_8_area[0][2] = img[i - 1][j + 1]
_8_area[1][2] = img[i][j + 1]
_8_area[2][2] = img[i + 1][j + 1]
_8_area[2][1] = img[i + 1][j]
_8_area[2][0] = img[i + 1][j - 1]
_8_area[1][0] = img[i][j - 1]
result[division(_8_area)] += 1
return result


def main():
# 图像地址
origin_address = "origin.png"
denoise_address = "denoise.png"

# 以灰度方式读入图像
origin_img = cv.imread(origin_address, 0)
denoise_img = cv.imread(denoise_address, 0)

# 求差异度
d_origin = get_d(origin_img)
d_denoise = get_d(denoise_img)

# 制图
x = [0] * 256
for i in range(256):
x[i] = i
plt.plot(x, d_origin, color='red')
plt.plot(x, d_denoise, color='blue')
# plt.axis([1, 60, -1, 50])
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用来正常显示负号
plt.rcParams['axes.unicode_minus'] = False
# 标题
plt.title('差异度折线图(红色原图/蓝色处理图)', fontsize=24, color='black')
plt.show()


if __name__ == '__main__':
main()