工作概述

相似度探究

1

1
2
3
4
5
graph LR
A(取2*3的像素点) -->B(视为两个相邻格子)
B --> C(求取相似度S)
C -->|存在阈值D > S| D[属性相同]
C -->|存在阈值D < S| E[属性不同]

代码示例

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
# utf-8
# python3.8

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


def get_unit_similarity(a_4, b_4, k):
a_4 = sorted(a_4)
b_4 = sorted(b_4)
result = 0
for i in range(4):
result = result + (abs(int(a_4[i]) - int(b_4[i]))) ** k
result = result ** (1/k)
return int(result)


def get_channels_similarity(channel, k):
row, col = channel.shape
result = []
for i in range(row - 2):
for j in range(col - 2):
array = [channel[i][j], channel[i][j + 1], channel[i + 1][j + 1], channel[i + 1][j]]
r_array = [channel[i][j + 1], channel[i + 1][j + 1], channel[i][j + 2], channel[i + 1][j + 2]]
d_array = [channel[i + 1][j + 1], channel[i + 1][j], channel[i + 2][j + 1], channel[i + 2][j]]
r_similarity = get_unit_similarity(array, r_array, k)
d_similarity = get_unit_similarity(array, d_array, k)
result.append(r_similarity)
result.append(d_similarity)
return result


def get_img_similarity(img, k):
p = cv.imread(img, 1)
b, g, r = cv.split(p)
b_similarity = get_channels_similarity(b, k)
g_similarity = get_channels_similarity(g, k)
r_similarity = get_channels_similarity(r, k)
cnt = np.zeros(400)
for i in range(len(b_similarity)):
cnt[b_similarity[i]] += 1
for i in range(len(g_similarity)):
cnt[g_similarity[i]] += 1
for i in range(len(r_similarity)):
cnt[r_similarity[i]] += 1

plt.plot(cnt, color='red')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title('相似度分布图 k = ' + str(k), fontsize=24, color='black')
plt.show()


if __name__ == '__main__':
img_addr = 'img.png'
get_img_similarity(img_addr, 1)

结果

image-20220328225018380

image-20220328225138813

省略100以后的数据,得到的放大图

image-20220328225738338

image-20220328225907373

噪声图

image-20220328230311035