-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiibvsi_final.py
349 lines (274 loc) · 9.82 KB
/
iibvsi_final.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
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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import cv2
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from numpy.fft import fft2, ifft2, fftshift, ifftshift
# 1. Feature Extraction
def compute_gradient_magnitude(img, sigma):
"""
수식 1, 2: 원본 이미지와 암호화된 이미지의 gradient magnitude 계산
"""
hx, hy = gaussian_partial_derivative_filter(sigma)
gx = signal.convolve2d(img, hx, mode="same", boundary="symm")
gy = signal.convolve2d(img, hy, mode="same", boundary="symm")
G = np.sqrt(gx**2 + gy**2)
return G
def gaussian_partial_derivative_filter(sigma, size=None):
"""
수식 3: 가우시안 편미분 필터 생성
"""
if size is None:
size = int(2 * np.ceil(3 * sigma) + 1)
x, y = np.meshgrid(
np.arange(-size // 2 + 1, size // 2 + 1),
np.arange(-size // 2 + 1, size // 2 + 1),
)
constant = -1 / (2 * np.pi * sigma**4)
exp_part = np.exp(-(x**2 + y**2) / (2 * sigma**2))
hx = constant * x * exp_part
hy = constant * y * exp_part
return hx, hy
def compute_spatial_contrast_map(img, sigma, M):
"""
수식 4, 5 : GM 평균 계산
- param img : 입력 이미지
- param simga : 가우시안 필터 스케일 파라미터
- param M : GM 맵의 최대 차수 - 논문에서 1~8까지 사용했고, M=3이 best였다함.(기본값)
=> retrun C : spatial contrast map
"""
G_maps = []
current_map = img
for _ in range(M):
current_map = compute_gradient_magnitude(current_map, sigma)
G_maps.append(current_map)
# GM map 평균
C = np.mean(G_maps, axis=0)
return C
def log_gabor_filter(shape, sigma_s, mu_o, sigma_o):
"""
수식 6: Log-Gabor 필터 생성
"""
rows, cols = shape
x, y = np.meshgrid(np.linspace(-0.5, 0.5, cols), np.linspace(-0.5, 0.5, rows))
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y, x)
r_s = 0.5 # 중심 주파수
r[r == 0] = np.finfo(float).eps # 로그 계산에서 0을 피하기 위해 작은 값을 추가
log_gabor_radial = np.exp(
-((np.log(r / r_s)) ** 2) / (2 * (np.log(sigma_s / r_s)) ** 2)
)
log_gabor_angular = np.exp(-((theta - mu_o) ** 2) / (2 * sigma_o**2))
log_gabor = log_gabor_radial * log_gabor_angular
return log_gabor
def apply_log_gabor(img, sigma_s, mu_o, sigma_o):
"""
수식 7: Log-Gabor 필터 적용 및 amplitude_map 계산
"""
log_gabor = log_gabor_filter(img.shape, sigma_s, mu_o, sigma_o)
img_fft = fftshift(fft2(img))
filtered_img = img_fft * log_gabor
filtered_img_ifft = ifft2(ifftshift(filtered_img))
amplitude_map = np.abs(filtered_img_ifft)
return amplitude_map
def apply_log_gabor_filter_only(img, sigma_s, mu_o, sigma_o):
"""
Log-Gabor 필터만 적용한 결과를 반환합니다.
"""
log_gabor = log_gabor_filter(img.shape, sigma_s, mu_o, sigma_o)
img_fft = fftshift(fft2(img))
filtered_img = img_fft * log_gabor
filtered_img_ifft = ifft2(ifftshift(filtered_img))
return np.real(filtered_img_ifft)
def compute_texture_map(img, S, O):
"""
수식 8, 9: 텍스처 맵 계산
"""
texture_map = np.zeros_like(img, dtype=float)
for s in range(S):
for o in range(O):
sigma_s = 0.35 + 0.1 * s # 예시 값, 필요에 따라 조정
mu_o = o * np.pi / O
sigma_o = 0.5 # 예시 값, 필요에 따라 조정
amplitude_map = apply_log_gabor(img, sigma_s, mu_o, sigma_o)
texture_map += amplitude_map
texture_map /= S * O
return texture_map
# 3. Similarity Measurement
def compute_contrast_similarity_map(C_P, C_E, R1=1e-12):
"""
수식 10: Spatial Contrast Similarity Map 계산
- param C_P: 원본 이미지 GM 평균 맵
- param C_E: 암호화된 이미지의 GM 평균 맵
- param R1: 분모 0 방지를 위한 상수
- return: Spatial Contrast Similarity Map
"""
S_C = (2 * C_P * C_E + R1) / (C_P**2 + C_E**2 + R1)
return S_C
def compute_texture_similarity_map(T_P, T_E, R2=1e-12):
"""
수식 11: Texture Similarity Map 계산
- param T_P: 원본 이미지 Texture map
- param T_E: 암호화된 이미지 Texture map
- param R2: 분모 0 방지를 위한 상수
- return: Texture Similarity Map
"""
S_T = (2 * T_P * T_E + R2) / (T_P**2 + T_E**2 + R2)
return S_T
def compute_visual_security_map(S_C, S_T):
"""
수식 12: Visual Security Map 계산
- param S_C: Spatial Contrast Similarity Map
- param S_T: Texture Similarity Map
- return: Visual Security Map
"""
alpha = np.mean(S_C) # sc 평균값
beta = np.mean(S_T) # st 평균값
S_VS = (S_C**alpha) * (S_T**beta)
return S_VS
# 3. Image Importance-Based Pooling
def compute_weighting_map(C_P, C_E):
"""
수식 13: Weighting Map 계산
- param C_P: 원본 이미지 GM 평균 맵
- param C_E: 암호화된 이미지의 GM 평균 맵
- return: Weighting Map
"""
W = np.maximum(np.abs(C_P), np.abs(C_E))
return W
def compute_visual_security_score(W, S_VS):
"""
수식 14: Visual Security Score 계산
- param W: Weighting Map
- param S_VS: Visual Security Map
- return: Visual Security Score
"""
VS = np.sum(W * S_VS) / np.sum(W)
return VS
def downsample_image(img, t): # T를 위한
"""
이미지 다운샘플링
- param img: 입력 이미지
- param t: 다운샘플링 횟수
- return: 다운샘플링된 이미지
"""
return img[:: 2**t, :: 2**t]
def compute_iibvsi(plain_img, encrypted_img, T=2, sigma=1.0, M=3, S=4, O=8):
"""
수식 15: IIBVSI 계산
- param plain_img: 원본 이미지
- param encrypted_img: 암호화된 이미지
- param T: 다중 해상도 레벨 수
- param sigma: 가우시안 필터 스케일 파라미터
- param M: GM 맵의 최대 차수
- param S: 스케일 수
- param O: 방향 수
- return: IIBVSI Score
"""
VS_scores = []
for t in range(T + 1):
# 다운샘플링
plain_img_ds = downsample_image(plain_img, t)
encrypted_img_ds = downsample_image(encrypted_img, t)
# GM 맵, Spatial Contrast Map 계산
G_P = compute_gradient_magnitude(plain_img_ds, sigma)
C_P = compute_spatial_contrast_map(plain_img_ds, sigma, M)
G_E = compute_gradient_magnitude(encrypted_img_ds, sigma)
C_E = compute_spatial_contrast_map(encrypted_img_ds, sigma, M)
# texture
T_P = compute_texture_map(plain_img_ds, S, O)
T_E = compute_texture_map(encrypted_img_ds, S, O)
# the similarity maps
S_C = compute_contrast_similarity_map(C_P, C_E)
S_T = compute_texture_similarity_map(T_P, T_E)
S_VS = compute_visual_security_map(S_C, S_T)
# the weighting map and visual security score
W = compute_weighting_map(C_P, C_E)
VS = compute_visual_security_score(W, S_VS)
VS_scores.append(VS)
# IBVSI score
IIBVSI_score = np.mean(VS_scores)
return IIBVSI_score
"""
시각화
"""
# 이미지 로드
plain_image_path = "/Users/jiminking/Documents/김지민/projects/myproject/gradu/data/PEID/refimg/tower.bmp"
encrypted_image_path = "/Users/jiminking/Documents/김지민/projects/myproject/gradu/data/PEID/encimg/tower_09_5.bmp"
# plain_image_path = "/Users/jiminking/Documents/김지민/projects/myproject/gradu/data/Celebrity Faces Dataset/Angelina Jolie/002_8f8da10e.jpg"
# encrypted_image_path = "/Users/jiminking/Documents/김지민/projects/myproject/gradu/data/black_data/Angelina Jolie/002_8f8da10e.jpg.png"
img_plain = cv2.imread(plain_image_path, cv2.IMREAD_GRAYSCALE)
img_encrypted = cv2.imread(encrypted_image_path, cv2.IMREAD_GRAYSCALE)
if img_plain is None:
raise FileNotFoundError("Plain image not found")
if img_encrypted is None:
raise FileNotFoundError("Encrypted image not found")
# IIBVSI 계산
iibvsi_score = compute_iibvsi(img_plain, img_encrypted)
print("IIBVSI Score:", iibvsi_score)
# 파라미터 설정
sigma = 1.0
M = 3 # GM 맵의 최대 차수
S = 4 # 스케일 수
O = 8 # 방향 수
# Compute the gradient magnitudes and spatial contrast maps
G_P = compute_gradient_magnitude(img_plain, sigma) # GM
C_P = compute_spatial_contrast_map(img_plain, sigma, M) # GM 평균
G_E = compute_gradient_magnitude(img_encrypted, sigma) # GM
C_E = compute_spatial_contrast_map(img_encrypted, sigma, M) # GM 평균
# texture maps
texture_map_combined_plain = compute_texture_map(img_plain, S, O)
texture_map_combined_encrypted = compute_texture_map(img_encrypted, S, O)
# similarity maps
S_C = compute_contrast_similarity_map(C_P, C_E)
S_T = compute_texture_similarity_map(
texture_map_combined_plain, texture_map_combined_encrypted
)
S_VS = compute_visual_security_map(S_C, S_T)
# print("Visual Security Map S_VS")
# print(S_VS)
# 시각화
plt.figure(figsize=(20, 10))
plt.subplot(3, 3, 1)
plt.title("Plain Image")
plt.imshow(img_plain, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 2)
plt.title("Gradient Magnitude G_P")
plt.imshow(G_P, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.subplot(3, 3, 3)
plt.title("Spatial Contrast Map C_P")
plt.imshow(C_P, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.subplot(3, 3, 4)
plt.title("Encrypted Image")
plt.imshow(img_encrypted, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 5)
plt.title("Gradient Magnitude G_E")
plt.imshow(G_E, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.subplot(3, 3, 6)
plt.title("Spatial Contrast Map C_E")
plt.imshow(C_E, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.subplot(3, 3, 7)
plt.title("Encrypted Image")
plt.imshow(img_encrypted, cmap="gray")
plt.axis("off")
plt.subplot(3, 3, 8)
plt.title("Contrast Similarity Map S_C")
plt.imshow(S_C, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.subplot(3, 3, 9)
plt.title("Texture Similarity Map S_T")
plt.imshow(S_T, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.tight_layout()
plt.show()