-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacks.py
1068 lines (888 loc) · 39.2 KB
/
attacks.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cv2
import numpy as np
import matplotlib.pyplot as plt
import random
import imageio.v2 as imageio
from PIL import Image, ImageFont, ImageDraw
from skimage import exposure
from scipy.ndimage import rotate ,median_filter,uniform_filter,gaussian_filter,convolve
from sklearn.cluster import KMeans
from skimage.util import random_noise
from config_variables import *
from functions import *
import pickle
def TIFF_save(watermarked_img):
imageio.imwrite(os.path.join(current_directory, 'Images/Saved.tiff'), watermarked_img, format="tiff")
TIFF_img = imageio.imread(os.path.join(current_directory, 'Images/Saved.tiff'))
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img,TIFF_img)
print("TIFF image PSNR : ",psnr_value, "\t \t TIFF image MSE : ",mse_value ,"\t \t TIFF image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(TIFF_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(TIFF_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(TIFF_img, cmap='gray')
ax1.set_title('TIFF image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Extracted watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def PNG_save(watermarked_img):
pil_image = Image.fromarray(watermarked_img)
pil_image.save(os.path.join(current_directory, 'Images/Saved.png'))
png_img = imageio.imread(os.path.join(current_directory, 'Images/Saved.png'))
mse_value, psnr_value, ssim_value = Image_metrics(png_img, watermarked_img)
print("PNG image PSNR : ",psnr_value, "\t \t PNG image MSE : ",mse_value ,"\t \t PNG image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(png_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(png_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(png_img, cmap='gray')
ax1.set_title('PNG image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Extracted watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def JPEG_save(watermarked_img,qual):
pil_image = Image.fromarray(watermarked_img)
pil_image.save(os.path.join(current_directory, 'Images/Saved.jpg'),"JPEG",quality=qual)
jpeg_img = imageio.imread(os.path.join(current_directory, 'Images/Saved.jpg'))
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, jpeg_img)
print("JPEG image PSNR : ",psnr_value, "\t \t JPEG image MSE : ",mse_value ,"\t \t JPEG image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(jpeg_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(jpeg_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(jpeg_img, cmap='gray')
ax1.set_title('JPEG image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Extracted watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Rotation_attack(watermarked_img,rot):
rotated_img = rotate(watermarked_img, rot,reshape=False)
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img,rotated_img)
print("Rotated image PSNR : ",psnr_value, "\t \t Rotated image MSE : ",mse_value, "\t \t Rotated image MSE : ",ssim_value )
if self_embed==True:
org_water=self_embedding(rotated_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(rotated_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(rotated_img, cmap='gray')
ax1.set_title('Rotated image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Flip_direction_attack(watermarked_img,ax):
flipped_img = np.flip(watermarked_img, axis=ax)
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, flipped_img)
print("flipped image PSNR : ",psnr_value, "\t \t flipped image MSE : ",mse_value, "\t \t flipped image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(flipped_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(flipped_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
flipped_im=flipped_img
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow( flipped_im, cmap='gray')
ax1.set_title('flipped image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Scaling_attack(watermarked_img,size):
scaled_img=cv2.resize(watermarked_img,(size,size))
if self_embed==True:
org_water=self_embedding(scaled_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(scaled_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(scaled_img, cmap='gray')
ax1.set_title('Scaled image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Translation_attack(watermarked_img,dx,dy):
M = np.float64([[1, 0, dx], [0, 1, dy]])
translated_img = cv2.warpAffine(watermarked_img, M, (watermarked_img.shape[1], watermarked_img.shape[0]))
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, translated_img)
print("Translated image PSNR : ",psnr_value, "\t \t Translated image MSE : ",mse_value ,"\t \t Translated image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(translated_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(translated_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(translated_img, cmap='gray')
ax1.set_title('Translated image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Zooming_attack(watermarked_img,XD, YD, sizex, sizey):
zoomed_img=cv2.resize(watermarked_img,(sizey,sizex))
for i in range(sizex):
for j in range(sizey):
zoomed_img[i][j]=watermarked_img[XD+i][YD+j]
zoomed_img=cv2.resize(zoomed_img,(img_size,img_size))
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, zoomed_img)
print("Zoomed image PSNR : ",psnr_value, "\t \t Zoomed image MSE : ",mse_value ,"\t \t Zoomed image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(zoomed_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( zoomed_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(zoomed_img, cmap='gray')
ax1.set_title('Zoomed image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Scanning_attack(watermarked_img,type,size=256,rot=30):
if type =='scalling':
S_img=cv2.resize(watermarked_img,(size,size))
S_img=cv2.resize(S_img,(img_size,img_size))
elif type=='rotation':
S_img = rotate(watermarked_img, rot,reshape=False)
S_img = rotate(S_img, rot*-1,reshape=False)
else:
raise ValueError("Choose either scalling or rotation")
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img,S_img)
print("Scanning image PSNR : ",psnr_value, "\t \t Scanning image MSE : ",mse_value ,"\t \t Scanning image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(S_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( S_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(S_img, cmap='gray')
ax1.set_title('Scanning image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Gaussian_noise(watermarked_img,mean,stddev):
if len((np.asarray(watermarked_img)).shape)==3:
long=3
else:
long=1
image=[]
for channel in range(long):
if long==3:
img=watermarked_img[:, :, channel]
else:
img=watermarked_img
noise = np.random.normal(mean, stddev, img.shape)
noisy_img = img + noise
image.append(noisy_img)
if long==3:
noisy_img = np.stack([image[0], image[1], image[2]], axis=2)
else:
noisy_img=image[0]
noisy_img = noisy_img.astype("uint8")
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, noisy_img)
print("Noisy image PSNR : ",psnr_value, "\t \t Noisy image MSE : ",mse_value ,"\t \t Noisy image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(noisy_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(noisy_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(noisy_img, cmap='gray')
ax1.set_title('Noisy image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Salt_paper(watermarked_img,salt_vs_pepper,amount):
salt_paper_image=np.copy(watermarked_img)
# Generate salt-and-pepper noise
salt = np.ceil(amount * salt_paper_image.size * salt_vs_pepper)
pepper = np.ceil(amount * salt_paper_image.size * (1.0 - salt_vs_pepper))
coords_salt = [np.random.randint(0, i - 1, int(salt)) for i in salt_paper_image.shape]
coords_pepper = [np.random.randint(0, i - 1, int(pepper)) for i in salt_paper_image.shape]
salt_paper_image[coords_salt[0], coords_salt[1]] = 255
salt_paper_image[coords_pepper[0], coords_pepper[1]] = 0
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, salt_paper_image)
print("Salt&paper image PSNR : ",psnr_value, "\t \t Salt&paper image MSE : ",mse_value ,"\t \t Salt&paper image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(salt_paper_image)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(salt_paper_image,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(salt_paper_image, cmap='gray')
ax1.set_title('Salt&paper image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Speckle_noise(watermarked_img):
if len((np.asarray(watermarked_img)).shape)==3:
long=3
else:
long=1
image=[]
for channel in range(long):
if long==3:
img=watermarked_img[:, :, channel]
else:
img=watermarked_img
noisy_img = img / np.max(img)
noisy_img = random_noise(noisy_img, mode='speckle', seed=None, clip=True)
noisy_img = noisy_img * np.max(img)
noisy_img = noisy_img.astype("uint8")
image.append(noisy_img)
if long==3:
noisy_img = np.stack([image[0], image[1], image[2]], axis=2)
else:
noisy_img=image[0]
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, noisy_img)
print("Speckle noise image PSNR : ",psnr_value, "\t \t Speckle noise image MSE : ",mse_value ,"\t \t Speckle noise image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(noisy_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(noisy_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(noisy_img, cmap='gray')
ax1.set_title('Speckle noise')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Median_filtring(watermarked_img,size):
filtered_image = median_filter(watermarked_img, size=size)
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, filtered_image)
print("Median filter image PSNR : ",psnr_value, "\t \t Median filter image MSE : ",mse_value ,"\t \t Median filter image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(filtered_image)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(filtered_image,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(filtered_image, cmap='gray')
ax1.set_title('Median filter image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Average_filtring(watermarked_img,size):
if len((np.asarray(watermarked_img)).shape)==3:
long=3
else:
long=1
image=[]
for channel in range(long):
if long==3:
img=watermarked_img[:, :, channel]
else:
img=watermarked_img
img = uniform_filter(img, size=size)
image.append(img)
if long==3:
filtered_image = np.stack([image[0], image[1], image[2]], axis=2)
else:
filtered_image=image[0]
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, filtered_image)
print("Average filter image PSNR : ",psnr_value, "\t \t Average filter image MSE : ",mse_value ,"\t \t Average filter image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(filtered_image)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(filtered_image,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(filtered_image, cmap='gray')
ax1.set_title('Average filter image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Gaussian_filtring(watermarked_img):
filtered_image = gaussian_filter(watermarked_img, sigma=1.0, order=0)
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, filtered_image)
print("Gaussian filter image PSNR : ",psnr_value, "\t \t Gaussian filter image MSE : ",mse_value ,"\t \t Gaussian filter image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(filtered_image)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(filtered_image,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2,ax3) = plt.subplots(1, 3)
ax1.imshow(filtered_image, cmap='gray')
ax1.set_title('Gaussian filter image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Histogram_equalization(watermarked_img):
if len((np.asarray(watermarked_img)).shape)==3:
long=3
else:
long=1
image=[]
for channel in range(long):
if long==3:
img=watermarked_img[:, :, channel]
else:
img=watermarked_img
max=np.max(img)
filtered_image = exposure.equalize_hist(img)
filtered_image = filtered_image *max
filtered_image = filtered_image.astype("uint8")
image.append(filtered_image)
if long==3:
filtered_image = np.stack([image[0], image[1], image[2]], axis=2)
else:
filtered_image=image[0]
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, filtered_image)
print("His-EQ image PSNR : ",psnr_value, "\t \t His-EQ image MSE : ",mse_value ,"\t \t His-EQ image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(filtered_image)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark(filtered_image,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax0,ax1, ax2,ax3) = plt.subplots(1, 4)
ax0.imshow(filtered_image, cmap='gray')
ax0.set_title('His-EQ image')
ax1.imshow(org_watermark, cmap='gray')
ax1.set_title('Rec watermark')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Low_pass(watermarked_img,kernel,co):
blurred = cv2.GaussianBlur(watermarked_img, kernel, co)
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, blurred)
print("Low pass image PSNR : ",psnr_value, "\t \t Low pass image MSE : ",mse_value ,"\t \t Low pass image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(blurred)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( blurred,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(blurred, cmap='gray')
ax1.set_title('Low pass image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Motion_blur(watermarked_img,size,angle):
img=np.copy(watermarked_img)
# Create the motion blur kernel
kernel = np.zeros((size, size))
kernel[int((size-1)/2), :] = np.ones(size)
kernel = rotate(kernel, angle, mode='constant')
# Normalize the kernel
kernel = kernel / np.sum(kernel)
# Apply the motion blur filter to the image
if len((np.asarray(watermarked_img)).shape)==3:
long=3
else:
long=1
image=[]
for channel in range(long):
if long==3:
img=watermarked_img[:, :, channel]
else:
img=watermarked_img
blurred_img = convolve(img, kernel)
image.append(blurred_img)
if long==3:
blurred_img = np.stack([image[0], image[1], image[2]], axis=2)
else:
blurred_img=image[0]
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img, blurred_img)
print("Motion blur image PSNR : ",psnr_value, "\t \t Motion blur image MSE : ",mse_value ,"\t \t Motion blur image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(blurred_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( blurred_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(blurred_img, cmap='gray')
ax1.set_title('Motion blur image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Cropping_attack(watermarked_img,XD, YD, sizex, sizey):
cropped_img=np.copy(watermarked_img)
if len((np.asarray(cropped_img)).shape)==3:
long=3
else:
long=1
image=[]
for channel in range(long):
if long==3:
img=cropped_img[:, :, channel]
else:
img=cropped_img
for i in range(sizex):
for j in range(sizey):
img[XD+i][YD+j]=0
image.append(img)
if long==3:
cropped_img = np.stack([image[0], image[1], image[2]], axis=2)
else:
cropped_img=image[0]
mse_value, psnr_value, ssim_value = Image_metrics(original_img, cropped_img)
print("Cropped image PSNR : ",psnr_value, "\t \t Cropped image MSE : ",mse_value ,"\t \t Cropped image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(cropped_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( cropped_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
tamper=Tamper_localization(tamper)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(cropped_img, cmap='gray')
ax1.set_title('Cropped image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Copy_move_attack(watermarked_img,XD, YD, XD2, YD2, sizex, sizey):
image=np.copy(watermarked_img)
if len((np.asarray(image)).shape)==3:
long=3
else:
long=1
imag=[]
for channel in range(long):
if long==3:
img=image[:, :, channel]
else:
img=image
for i in range(sizex):
for j in range(sizey):
img[XD+i][YD+j]=img[XD2+i][YD2+j]
imag.append(img)
if long==3:
Copy_move_img = np.stack([imag[0], imag[1], imag[2]], axis=2)
else:
Copy_move_img=imag[0]
mse_value, psnr_value, ssim_value = Image_metrics(original_img, Copy_move_img)
print("Copy move image PSNR : ",psnr_value, "\t \t Copy move image MSE : ",mse_value ,"\t \t Copy move image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(Copy_move_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( Copy_move_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
tamper=Tamper_localization(tamper)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(Copy_move_img, cmap='gray')
ax1.set_title('Copy move image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Copy_paste_attack(watermarked_img,XD, YD,sizex, sizey):
image=np.copy(watermarked_img)
img = imageio.imread(os.path.join(current_directory, 'Images/Peppers.tiff'))
img=cv2.resize(img,(img_size,img_size))
if len((np.asarray(image)).shape)==3:
long=3
else:
long=1
if len((np.asarray(img)).shape)==3:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imag=[]
for channel in range(long):
if long==3:
img1=image[:, :, channel]
img2=img[:, :, channel]
else:
img1=image
img2=img
for i in range(sizex):
for j in range(sizey):
img1[XD+i][YD+j]=img2[XD+i][YD+j]
imag.append(img1)
if long==3:
Copy_paste_img = np.stack([imag[0], imag[1], imag[2]], axis=2)
else:
Copy_paste_img=imag[0]
mse_value, psnr_value, ssim_value = Image_metrics(original_img, Copy_paste_img)
print("Copy paste image PSNR : ",psnr_value, "\t \t Copy paste image MSE : ",mse_value ,"\t \t Copy paste image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(Copy_paste_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( Copy_paste_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
tamper=Tamper_localization(tamper)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(Copy_paste_img, cmap='gray')
ax1.set_title('Copy_paste image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Text_addition(watermarked_img,text,x,y,size):
image=np.copy(watermarked_img)
if len((np.asarray(image)).shape)==3:
long=3
else:
long=1
imag=[]
for channel in range(long):
if long==3:
img=image[:, :, channel]
else:
img=image
texted_image = Image.fromarray(img)
draw = ImageDraw.Draw(texted_image)
font = ImageFont.truetype("arial.ttf", size)
# Draw the text on the image
draw.text((x, y), text, font=font)
texted_image=np.array(texted_image)
imag.append(texted_image)
if long==3:
texted_image = np.stack([imag[0], imag[1], imag[2]], axis=2)
else:
texted_image=imag[0]
mse_value, psnr_value, ssim_value = Image_metrics(original_img, texted_image)
print("Text addition image PSNR : ",psnr_value, "\t \t Text addition image MSE : ",mse_value ,"\t \t Text addition image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(texted_image)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( texted_image,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(texted_image, cmap='gray')
ax1.set_title('Cropped image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Content_addition(watermarked_img,des_x, des_y ,sizex,sizey):
image=np.copy(watermarked_img)
img = imageio.imread(os.path.join(current_directory, 'Images/Addition.png'))
zoomed_img=cv2.resize(img,(sizey,sizex))
if len((np.asarray(image)).shape)==3:
long=3
else:
long=1
if len((np.asarray(zoomed_img)).shape)==3:
zoomed_img=cv2.cvtColor(zoomed_img, cv2.COLOR_BGR2GRAY)
imag=[]
for channel in range(long):
if long==3:
img1=image[:, :, channel]
img2=zoomed_img[:, :, channel]
else:
img1=image
img2=zoomed_img
for i in range(sizex):
for j in range(sizey):
img1[des_x+i][des_y+j]=img2[i][j]
imag.append(img1)
if long==3:
content_addition_img = np.stack([imag[0], imag[1], imag[2]], axis=2)
else:
content_addition_img=imag[0]
mse_value, psnr_value, ssim_value = Image_metrics(original_img, content_addition_img) #print("Extracted Watermark PSNR : ",psnr_value)
print("Content addition image PSNR : ",psnr_value, "\t \t Content addition image MSE : ",mse_value ,"\t \t Content addition image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(content_addition_img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img=extraction_DWT_watermark( content_addition_img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
tamper=Tamper_localization(tamper)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(content_addition_img, cmap='gray')
ax1.set_title('Added image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Constant_feature_attack(type,watermarked_img,XD, YD, xsize, ysize,MSB):
image=np.copy(watermarked_img)
if len((np.asarray(image)).shape)==3:
long=3
else:
long=1
imag=[]
if type=="all":
for channel in range(long):
if long==3:
img=image[:, :, channel]
else:
img=image
for j in range(len(img[0])):
for k in range(len(img[0])):
bits=""
for t in range(MSB):
pixel =dec_to_bin(img[j][k])
bits=bits+str(random.randint(0,1))
pixel=bits+pixel[MSB:len(pixel)]
img[j][k]=bin_to_dec(pixel)
imag.append(img)
elif type=="zone":
for channel in range (long):
if long==3:
img=image[:, :, channel]
else:
img=image
for j in range(xsize):
for k in range(ysize):
pixel =dec_to_bin(img[XD+j][YD+k])
bits=""
for t in range(MSB):
bits=bits+str(random.randint(0,1))
pixel=bits+pixel[MSB:len(pixel)]
img[XD+j][YD+k]=bin_to_dec(pixel)
imag.append(img)
else:
raise ValueError("Selecet either 'all' or 'zone'")
if long==3:
img = np.stack([imag[0], imag[1], imag[2]], axis=2)
else:
img=imag[0]
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img,img)
print("Content feature image PSNR : ",psnr_value, "\t \t Content feature image MSE : ",mse_value ,"\t \t Content feature image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(img, cmap='gray')
ax1.set_title('Content feature image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def Content_only_attack(type,watermarked_img,XD, YD,xsize, ysize,MSB):
image=np.copy(watermarked_img)
pic = imageio.imread(os.path.join(current_directory, 'Images/Peppers.tiff'))
pic=cv2.resize(pic,(img_size,img_size))
if len((np.asarray(image)).shape)==3:
long=3
else:
long=1
if len((np.asarray(pic)).shape)==3:
pic=cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
if type=="all":
max_x=len(image[0])
max_y=len(image[0])
XD=0
YD=0
elif type=="zone":
max_x=xsize
max_y=ysize
else :
raise ValueError("Error , select either 'all' or 'zone'")
imag=[]
for channel in range(long):
if long==3:
img1=image[:, :, channel]
img2=pic[:, :, channel]
else:
img1=image
img2=pic
for j in range(max_x):
for k in range(max_y):
pixel =dec_to_bin(img1[XD+j][YD+k])
pixel2 =dec_to_bin(img2[XD+j][YD+k])
pixel=pixel2[0:MSB]+pixel[MSB:len(pixel)]
img1[XD+j][YD+k]=bin_to_dec(pixel)
imag.append(img1)
if long==3:
img = np.stack([imag[0], imag[1], imag[2]], axis=2)
else:
img=imag[0]
mse_value, psnr_value, ssim_value = Image_metrics(watermarked_img,img)
print("Content only image PSNR : ",psnr_value, "\t \t Content only image MSE : ",mse_value ,"\t \t Content only image SSIM : ",ssim_value)
if self_embed==True:
org_water=self_embedding(img)
else:
org_water=org_watermark
with open(os.path.join(os.path.dirname(__file__), 'Images/Max_frequencies.pkl'), 'rb') as file:
max_frequencies = pickle.load(file)
ext_watermark,restored_img =extraction_DWT_watermark( img,max_frequencies)
tamper=Tamper_detection(org_water,ext_watermark)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(img, cmap='gray')
ax1.set_title('Content only image')
ax2.imshow(ext_watermark, cmap='gray')
ax2.set_title('Attacked watermark')
ax3.imshow(tamper, cmap='binary',vmin=0, vmax=1)
ax3.set_title('Tampering')
plt.show()
def VQ_attack(watermarked_img):
# Convert image to a 2D array of pixels
if len((np.asarray(watermarked_img)).shape)==3:
long=3
VQ_img = np.reshape(watermarked_img, (img_size*img_size, 3))
else:
long=1
VQ_img = np.reshape(watermarked_img, (img_size*img_size,1))
# Apply k-means clustering to the pixel data
kmeans = KMeans(n_clusters=16, random_state=0,n_init=10).fit(VQ_img)
codebook = kmeans.cluster_centers_
# Replace each pixel with its nearest codevector in the codebook
quantized = codebook[kmeans.labels_]
quantized = np.reshape(quantized, (VQ_img.shape[0], VQ_img.shape[1]))
if long==3:
quantized=quantized.reshape(img_size,img_size,3)
quantized = quantized.astype("uint8")