-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpecvUtils.py
143 lines (122 loc) · 5.13 KB
/
OpecvUtils.py
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
import numpy as np
import cv2
import datetime
import os
from skimage.metrics import structural_similarity
BASE_PATH = 'static/png'
def remove_green_from_img(img_path: str):
img = cv2.imread(img_path)
hsv_mat = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
min_mat = np.array([50, 100, 100])
max_mat = np.array([70, 255, 255])
mask = cv2.inRange(hsv_mat, min_mat, max_mat)
mask_not = cv2.bitwise_not(mask)
# green_mat = cv2.bitwise_and(img, img, mask=mask)
green_not_mat = cv2.bitwise_and(img, img, mask=mask_not)
b,g,r = cv2.split(green_not_mat)
bgra = cv2.merge([b, g, r, mask_not])
output_filename = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ".png"
cv2.imwrite(os.path.join(BASE_PATH, "", output_filename), bgra)
# cv2.imshow('bgra', bgra)
# cv2.waitKey()
# cv2.destroyAllWindows()
return output_filename
def unwrap_n_replace(img_path: str):
return unwrap_color_replace(img_path=img_path, bgr=[55, 67, 188])
def unwrap_color_replace(img_path: str, bgr):
img = cv2.imread(img_path)
gray_mat = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY)
_, mask = cv2.threshold(gray_mat, 50.0, 255.0, cv2.THRESH_BINARY)
img[mask > 0] = [0, 0, 0]
img[mask == 0] = bgr
# cv2.imshow("output_", img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
width, height, channel = img.shape
for i in range(width):
for j in range(height):
b, g, r, a = img[i][j]
if r == 0 and g == 0 and b == 0:
img[i][j] = [0, 0, 0, 0]
# cv2.imshow("output", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
output_filename = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ".png"
cv2.imwrite(os.path.join(BASE_PATH, "", output_filename), img)
# img = cv2.imread(os.path.join(BASE_PATH, "", output_filename), cv2.COLOR_BGRA2RGBA)
# cv2.imshow("output", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# cv2.imwrite(os.path.join(BASE_PATH, "", output_filename), img)
return output_filename
def compare_img(img1path: str, img2path: str):
if os.path.exists(img1path) is False or os.path.exists(img2path) is False:
return 0
img1path = os.path.join(BASE_PATH, "", unwrap_n_replace(img_path=img1path))
img1 = cv2.imread(img1path)
img2 = cv2.imread(img2path)
# cv2.imshow("img1_gray_mat", img1)
# cv2.imshow("img2_gray_mat", img2)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
size = (int(500), int(500))
img1 = cv2.resize(img1, size, cv2.INTER_AREA)
img2 = cv2.resize(img2, size, cv2.INTER_AREA)
img1_gray_mat = cv2.cvtColor(img1, cv2.COLOR_BGRA2GRAY, cv2.CV_8UC3)
img2_gray_mat = cv2.cvtColor(img2, cv2.COLOR_BGRA2GRAY, cv2.CV_8UC3)
# cv2.imshow("img1_gray_mat", img1_gray_mat)
# cv2.imshow("img2_gray_mat", img2_gray_mat)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# img1_hist = cv2.calcHist(img1_gray_mat, [0], None, [255], [0, 255])
# img2_hist = cv2.calcHist(img2_gray_mat, [0], None, [255], [0, 255])
# ssim = cv2.compareHist(img1_hist, img2_hist, cv2.HISTCMP_CORREL)
# diff = cv2.absdiff(img1_gray_mat, img2_gray_mat)
# ssim = np.mean(diff * 2)
# img_compare = cv2.compare(img1_gray_mat, img2_gray_mat, cv2.CMP_EQ)
# cv2.imshow("img_compare", img_compare)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# img1_hist = cv2.calcHist(img1_gray_mat, [0], None, [256], [0, 255])
# img2_hist = cv2.calcHist(img2_gray_mat, [0], None, [256], [0, 255])
# ssim = 0
# for i in range(len(img1_hist)):
# if img1_hist[i] != img2_hist[i]:
# ssim = ssim + (1 - (abs(img1_hist[i] - img2_hist[i]) / max(img1_hist, img2_hist)))
# else:
# ssim = ssim + 1
# ssim = ssim / len(img1_hist)
# sub_image1 = cv2.split(img1_gray_mat)
# sub_image2 = cv2.split(img2_gray_mat)
# ssim = 0
# for i1, i2 in zip(sub_image1, sub_image2):
# ssim += cal2(i1, i2)
# ssim = ssim / 3
# ssim = np.sum((img1_gray_mat.astype("float") - img2_gray_mat.astype("float")) ** 2)
# ssim /= float(img1_gray_mat.shape[0] * img1_gray_mat.shape[1])
# ssim, _ = structural_similarity(img1_gray_mat, img2_gray_mat, full=True)
# 初始化ORB检测器
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1_gray_mat, None)
kp2, des2 = orb.detectAndCompute(img2_gray_mat, None)
# 提取并计算特征点
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
# knn筛选结果
matches = bf.knnMatch(des1, trainDescriptors=des2, k=2)
# 查看最大匹配点数目
good = [m for (m, n) in matches if m.distance < 0.55 * n.distance]
print(len(good))
print(len(matches))
ssim = len(good) / len(matches)
print("两张图片相似度为:%s" % ssim)
return ssim
# def cal2(im1, im2):
# img1_hist = cv2.calcHist(im1, [0], None, [256], [0, 255])
# img2_hist = cv2.calcHist(im2, [0], None, [256], [0, 255])
# ssim = 0
# for i in range(len(img1_hist)):
# if img1_hist[i] != img2_hist[i]:
# ssim = ssim + (1 - (abs(img1_hist[i] - img2_hist[i]) / max(img1_hist, img2_hist)))
# else:
# ssim = ssim + 1
# ssim = ssim / len(img1_hist)
# return ssim