-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.py
450 lines (391 loc) · 19.9 KB
/
apps.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import tkinter.messagebox # 弹窗库
from tkinter.ttk import *
from algorithm import *
import tkinter.font as tkFont
import numpy as np
from tkinter import *
from tkinter import ttk, messagebox
from functools import partial
np.set_printoptions(suppress=True)
plt.rcParams['font.sans-serif'] = ['SimHei']
rootdir = "./tmp/"
class WatermarkSystem2:
def __init__(self):
self.root = Tk()
self.root.title("信号与系统")
window_width = 1200
window_height = 800
set_center(self.root, window_width, window_height)
self.root.attributes('-toolwindow', False, '-alpha', 0.9, '-fullscreen', False, '-topmost', False)
global fontStyle, fontStyle1, fontStyle2
fontStyle = tkFont.Font(family="Lucida Grande", size=20)
fontStyle1 = tkFont.Font(family="Lucida Grande", size=15)
fontStyle2 = tkFont.Font(family="Lucida Grande", size=10)
self.fontStyle = fontStyle
self.fontStyle2 = fontStyle2
self.img_origin_path = "./tmp/image.png"
self.mask_path = rootdir + "defaultwm.png"
self.mask_path2 = rootdir + "defaultwm2.png"
self.img_origin = PhotoImage(file=self.img_origin_path)
self.img_show1 = PhotoImage(file=self.img_origin_path)
self.img_show2 = PhotoImage(file=self.img_origin_path)
self.mask_show1 = PhotoImage(file=self.mask_path)
self.create_widgets()
def update_img(self, new_img, img):
img_path = rootdir + new_img
img.configure(file=img_path)
def update_function_sub(self, selected_value):
values = list()
if selected_value == "时域分析":
values = ["FFT时域重建", "DCT时域重建", "三维FFT时域重建", "三维DCT时域重建"]
elif selected_value == "频域分析":
values = ["FFT频域分析", "DCT频域分析", "三维FFT频域分析", "三维DCT频域分析"]
elif selected_value == "水印添加":
values = ["DWT-DCT-SVD水印", "FFT频域水印", "DCT频域水印", "LSB时域水印", "三维FFT频域水印", "三维DCT频域水印", "文本水印"]
elif selected_value == "篡改检测":
values = ["DWT-DCT-SVD水印提取", "篡改存在", "频域水印提取", "ELA分析", "图像比对", "文本水印提取"]
elif selected_value == "图像篡改":
values = ["篡改1-高斯模糊", "篡改2-图像嫁接", "篡改3-图像压缩", "篡改4-裁剪攻击", "篡改5-锐化/钝化攻击"]
self.function_combobox_sub['values'] = values
def call_method(self, selected_method):
prefix_img_path_in = rootdir + str(self.combobox1.get())
prefix_img_path_in2 = rootdir + str(self.combobox2.get())
new_option = str(self.combobox1.get()) # 输出命名
img_path_in = prefix_img_path_in + '.png'
img_path_in2 = prefix_img_path_in2 + '.png'
img_out = None
print(img_path_in)
if selected_method == "FFT频域分析":
img_in = path2cv2(img_path_in)
img_out, _, _, _ = FFT_trans(img_in)
new_option = new_option + '_FFT'
elif selected_method == "DCT频域分析":
img_in = path2cv2(img_path_in)
img_out, _, _, _ = DCT_trans(img_in)
new_option = new_option + '_DCT'
elif selected_method == "三维DCT频域分析":
img_in = path2cv3(img_path_in)
wm_img = path2cv3(self.mask_path)
img_out, _, _ = DCT3_insert(img_in, wm_img)
new_option = new_option + '_DCT3'
elif selected_method == "三维FFT频域分析":
img_in = path2cv3(img_path_in)
wm_img = path2cv3(self.mask_path)
img_out, _, _ = FFT3_insert(img_in, wm_img)
new_option = new_option + '_FFT3'
elif selected_method == "三维FFT频域水印":
img_in = path2cv3(img_path_in)
wm_img = path2cv3(self.mask_path)
_, img_out, _ = FFT3_insert(img_in, wm_img)
new_option = new_option + '_FFT3_marked'
elif selected_method == "三维DCT频域水印":
img_in = path2cv3(img_path_in)
wm_img = path2cv3(self.mask_path)
_, img_out, _ = DCT3_insert(img_in, wm_img)
new_option = new_option + '_DCT3_marked'
elif selected_method == "三维FFT时域重建":
img_in = path2cv3(img_path_in)
wm_img = path2cv3(self.mask_path)
_, _, img_out = FFT3_insert(img_in, wm_img)
new_option = new_option + '_IFFT3'
elif selected_method == "三维DCT时域重建":
img_in = path2cv3(img_path_in)
wm_img = path2cv3(self.mask_path)
_, _, img_out = DCT3_insert(img_in, wm_img)
new_option = new_option + '_IDCT3'
elif selected_method == "FFT时域重建":
parts = prefix_img_path_in.rsplit("_", 1)[0]
parts = parts.split("_", 1)
img_path_in0 = parts[0] + '.png'
img_in0 = path2cv2(img_path_in0)
img_in = path2cv2(img_path_in)
img_fft2_log_wm, fft2_flag, fft2_max, fft2_min = FFT_trans(img_in0)
img_out = IFFT_trans(img_in, fft2_flag, fft2_max, fft2_min)
new_option = new_option + '_IFFT'
elif selected_method == "DCT时域重建":
parts = prefix_img_path_in.rsplit("_", 1)[0]
parts = parts.split("_", 1)
img_path_in0 = parts[0] + '.png'
img_in0 = path2cv2(img_path_in0)
img_in = path2cv2(img_path_in)
img_dct_log255, dct_flag, dct_max, dct_min = DCT_trans(img_in0)
img_out = IDCT_trans(img_in, dct_flag, dct_max, dct_min)
new_option = new_option + '_IDCT'
elif selected_method == "FFT频域水印":
img_in = path2cv2(img_path_in)
wm_img = path2cv2(self.mask_path)
img_in, _, _, _ = FFT_trans(img_in)
img_out = FFT_insert(img_in, wm_img)
new_option = new_option + '_FFT_marked'
elif selected_method == "DCT频域水印":
img_in = path2cv2(img_path_in)
wm_img = path2cv2(self.mask_path)
img_in, _, _, _ = DCT_trans(img_in)
img_out = DCT_insert(img_in, wm_img)
new_option = new_option + '_DCT_marked'
elif selected_method == "图片比对":
img_in = path2cv2(img_path_in)
img_in2 = path2cv2(img_path_in2)
img_out = Change_Detect(img_in2, img_in)
new_option = new_option + '_diff'
elif selected_method == "篡改存在":
img_in = path2cv2(img_path_in)
wm = cv2.imread(self.mask_path)
if "FFT" in img_path_in:
img_out = FFT2_Detect(img_in, wm)
else:
img_out = DCT_Detect2(img_in, wm)
messagebox.showinfo('篡改存在检测', str(img_out))
return
elif selected_method == "LSB时域水印":
host_image = cv2.imread(img_path_in, 1)
# host_image = bImg # 选择host
watermark_image = cv2.imread(self.mask_path, 0) # 选择水印
img_out = trace_insert(host_image, watermark_image)
new_option = new_option + '_Trace_marked'
elif selected_method == "篡改定位":
watermarked_image = cv2.imread(img_path_in, 1)
img_out = trace_extract(watermarked_image)
new_option = new_option + '_Trace_extrated'
elif selected_method == "ELA分析":
img_out = convert_to_ela_image(img_path_in, quality=90)
new_option = new_option + '_ELA'
img_path_out = rootdir + new_option + '.png'
img_out.save(img_path_out, 'PNG', quality=90)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
return
elif selected_method == "文本水印提取":
txt_out = DCT_txt_extract(img_path_in)
self.entry.insert(0, txt_out)
return
elif selected_method == "文本水印":
self.add_wm_txt()
return
elif selected_method == "DWT-DCT-SVD水印":
img_out = blind_insert(img_path_in, self.mask_path2)
new_option = new_option + '_DDS_marked'
elif selected_method == "DWT-DCT-SVD水印提取":
wm = path2cv2(self.mask_path2)
img_out = blind_extract(img_path_in, wm.shape)
new_option = new_option + '_DDS_extracted'
elif selected_method == "频域水印提取":
img_in = cv2.imread(img_path_in)
wm = cv2.imread(self.mask_path)
if "FFT" in img_path_in:
img_out = Get_WM(img_in, wm)
else:
img_out = Get_DCT_WM(img_in, wm)
new_option = new_option + '_Mark_extracted'
elif selected_method == "篡改1-高斯模糊":
self.add_tamper()
return
elif selected_method == "篡改2-图像嫁接":
self.add_wm_img()
return
elif selected_method == "篡改3-图像压缩":
self.add_tamper_compress()
return
elif selected_method == "篡改4-裁剪攻击":
self.add_tamper_cut()
return
elif selected_method == "篡改5-锐化/钝化攻击":
self.add_tamper_blur_sharpen()
return
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def on_select1(self, event):
selected_value = self.combobox1.get()
self.update_img(str(selected_value) + '.png', self.img_show1)
print(f"Selected1: {selected_value}")
def on_select2(self, event):
selected_value = self.combobox2.get()
self.update_img(str(selected_value) + '.png', self.img_show2)
print(f"Selected2: {selected_value}")
def on_select_function(self, event):
selected_value = self.function_combobox.get()
self.update_function_sub(selected_value)
print(f"Selected_function: {selected_value}")
def on_select_function_sub(self, event):
selected_value = self.function_combobox_sub.get()
self.call_method(selected_value)
print(f"Selected_function_sub: {selected_value}")
def add_combobox_option(self, combobox, new_option):
values = list(combobox['values'])
if new_option in values:
values.remove(new_option)
combobox['values'] = values + [new_option]
print('Add combobox option')
def delete_combobox_option(self, combobox, selected_option):
values = list(combobox['values'])
values.remove(selected_option) # 从选项列表中移除选中的选项
combobox['values'] = values
def add_wm_img(self):
# 图像篡改1
prefix_img_path_in = rootdir + str(self.combobox1.get())
new_option = str(self.combobox1.get()) + '_out'
img_path_in = prefix_img_path_in + '.png'
img_in = cv2.imread(img_path_in, 1)
img_out = img_tamper2(img_in)
new_option = new_option + '_Tampered2'
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def add_wm_txt(self):
txt = self.entry.get()
txt = str(txt)
prefix_img_path_in = rootdir + str(self.combobox1.get())
img_path_in = prefix_img_path_in + '.png'
new_option = str(self.combobox1.get())
img_out = DCT_txt_insert(img_path_in, txt)
print(txt)
new_option = new_option + '_txt_marked'
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def add_tamper(self):
prefix_img_path_in = rootdir + str(self.combobox1.get())
new_option = str(self.combobox1.get())
img_path_in = prefix_img_path_in + '.png'
img_in = cv2.imread(img_path_in, 1)
img_out = img_tamper(img_in)
new_option = new_option + '_Tampered1'
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def add_tamper_compress(self):
prefix_img_path_in = rootdir + str(self.combobox1.get())
new_option = str(self.combobox1.get())
img_path_in = prefix_img_path_in + '.png'
img_in = path2cv3(img_path_in)
img_out = Tamper_Compress(img_in, 0.5)
new_option = new_option + '_Tampered3'
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def add_tamper_cut(self):
prefix_img_path_in = rootdir + str(self.combobox1.get())
new_option = str(self.combobox1.get())
img_path_in = prefix_img_path_in + '.png'
img_in = path2cv3(img_path_in)
img_out = Tamper_Cut2(img_in)
new_option = new_option + '_Tampered4'
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def add_tamper_blur_sharpen(self):
prefix_img_path_in = rootdir + str(self.combobox1.get())
new_option = str(self.combobox1.get())
img_path_in = prefix_img_path_in + '.png'
img_in = path2cv3(img_path_in)
img_out = Tamper_sharpen_and_blur(img_in)
new_option = new_option + '_Tampered5'
img_path_out = rootdir + new_option + '.png'
print(img_path_out)
cv2.imwrite(img_path_out, img_out)
self.add_combobox_option(self.combobox2, new_option)
self.add_combobox_option(self.combobox1, new_option)
self.update_img(str(new_option) + '.png', self.img_show2)
def resize(self, event):
# 在窗口大小调整时调用此函数
window_width = self.root.winfo_width()
window_height = self.root.winfo_height()
# 计算新的组件位置
label_x = window_width // 2
label_y = window_height // 2
self.img_container1.place(x=window_width // 30, y=30 + 20)
self.img_container2.place(x=label_x + window_width // 30, y=30 + 20)
self.combobox1.place(x=window_width // 30, y=480)
self.combobox2.place(x=label_x + window_width // 30, y=480)
self.function_combobox.place(x=window_width // 30, y=530)
self.function_combobox_sub.place(x=window_width // 30, y=630)
self.img_container3.place(x=label_x + window_width // 30 + 50, y=window_height // 30 + 450)
self.change_wm_button.place(height=60, width=100, x=window_width // 30, y=window_height * 7 // 8)
self.judge_button.place(height=60, width=100, x=window_width // 30 + 150, y=window_height * 7 // 8)
# self.wm_img_button.place(height=60, width=100, x=window_width // 2 + 30, y=600)
# self.wm_txt_button.place(height=60, width=100, x=window_width // 2 + 30, y=500)
self.entry.place(x=window_width // 2 + 30, y=550)
# self.tamper_button.place(height=60, width=100, x=window_width // 2 + 30, y=700)
def create_widgets(self):
width = self.root.winfo_width()
height = self.root.winfo_height()
Label(self.root, text="基于数字水印的图片篡改检测系统", font=self.fontStyle).pack()
self.root.bind("<Configure>", self.resize)
# 创建标签并显示图片
self.img_container1 = Label(self.root, image=self.img_show1, width=400, height=400)
self.img_container2 = Label(self.root, image=self.img_show2, width=400, height=400)
self.img_container1.place(x=30, y=50)
self.img_container2.place(x=width // 2 + 30, y=50)
self.img_container3 = Label(self.root, image=self.mask_show1, width=400, height=400)
self.img_container3.place(x=width // 2 + 30, y=400)
init_images = ["image", "mark", "liftingbody", "peppers", "saturn"]
self.combobox1 = ttk.Combobox(self.root, values=init_images)
self.combobox1.configure(width=50, font=('Arial', 12, 'bold'))
self.combobox1.current(0)
self.combobox1.bind("<<ComboboxSelected>>", self.on_select1)
self.combobox1.place(x=30, y=470)
self.combobox2 = ttk.Combobox(self.root, values=init_images)
self.combobox2.configure(width=50, font=('Arial', 12, 'bold'))
self.combobox2.current(0)
self.combobox2.bind("<<ComboboxSelected>>", self.on_select2)
self.combobox2.place(x=width // 2 + 30, y=470)
# self.wm_txt_button = Button(self.root, text='文本水印', command=self.add_wm_txt)
# self.wm_txt_button.place(height=60, width=100, x=width // 2 + 30, y=500)
# self.wm_img_button = Button(self.root, text='图像篡改1', command=self.add_wm_img)
# self.wm_img_button.place(height=60, width=100, x=width // 2 + 30, y=600)
self.change_wm_button = Button(self.root, text='水印切换', command=self.change_wm)
self.change_wm_button.place(height=60, width=100, x=width // 2 + 30, y=700)
self.judge_button = Button(self.root, text='质量评估', command=self.Judge)
self.judge_button.place(height=60, width=100, x=width // 2 + 180, y=700)
self.entry = Entry(self.root)
self.entry.place(x=width // 2 + 30, y=550)
self.function_combobox = ttk.Combobox(self.root, values=["时域分析", "频域分析", "水印添加", "图像篡改", "篡改检测"])
self.function_combobox.configure(width=30, font=('Arial', 12, 'bold'))
self.function_combobox.current(0)
self.function_combobox.bind("<<ComboboxSelected>>", self.on_select_function)
self.function_combobox.place(x=30, y=500)
self.function_combobox_sub = ttk.Combobox(self.root, values=[])
self.function_combobox_sub.configure(width=30, font=('Arial', 12, 'bold'))
self.function_combobox_sub.bind("<<ComboboxSelected>>", self.on_select_function_sub)
self.function_combobox_sub.place(x=30, y=600)
def change_wm(self, image1_path="./tmp/defaultwm.png", image2_path="./tmp/defaultwm2.png"):
return
image1_name, image1_ext = os.path.splitext(image1_path)
image2_name, image2_ext = os.path.splitext(image2_path)
tmp = "./tmp/123.png"
os.rename(image1_path, tmp)
os.rename(image2_path, image1_name + image2_ext)
os.rename(image1_path, image2_name + image1_ext)
pass
def Judge(self):
prefix_img_path_in = rootdir + str(self.combobox1.get())
prefix_img_path_in2 = rootdir + str(self.combobox2.get())
img_path_in = prefix_img_path_in + '.png'
img_path_in2 = prefix_img_path_in2 + '.png'
img_in = path2cv2(img_path_in)
img_in2 = path2cv2(img_path_in2)
img_out = PSNR(img_in, img_in2)
img_out2 = SSIM(img_in, img_in2)
messagebox.showinfo('图像质量检测', 'PSNR: ' + str(img_out) + '\n' + 'SSIM: ' + str(img_out2))
def mainloop(self):
self.root.mainloop()