-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed_panebbianco.py
151 lines (117 loc) · 5.43 KB
/
embed_panebbianco.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
#!/usr/bin/env python3
import cv2
import pywt
from scipy.fft import dct, idct
import numpy as np
import functions as f
# LL LH HL HH
alphaPaper = [13.7, 8, 8, 8] # 30 20 20 20
extractWeight = [0.45, 0.2, 0.2, 0.15]
blockSizeDwtDctSvd = 8
def dctCoeffMatrix(fromDwt):
if(fromDwt.shape[0] != fromDwt.shape[1]):
raise ValueError('fromDwt input is not a square matrix')
numBlockSide = int(fromDwt.shape[0]/blockSizeDwtDctSvd)
dctMatrix = np.empty((numBlockSide, numBlockSide))
dctMatrixSortWatermarked = np.empty((numBlockSide, numBlockSide))
for i in range(numBlockSide):
for j in range(numBlockSide):
minX = int(i * blockSizeDwtDctSvd)
maxX = int((i+1) * blockSizeDwtDctSvd)
minY = int(j * blockSizeDwtDctSvd)
maxY = int((j+1) * blockSizeDwtDctSvd)
dctMatrix = dct(dct(fromDwt[minX:maxX, minY:maxY],axis=0, norm='ortho'),axis=1, norm='ortho')
dctMatrixSortWatermarked[i,j] = dctMatrix[0, 0]
return dctMatrixSortWatermarked
# @return inverse of dctCoeffMatrixes, return HL/LL/.. of watermarked image
def idctCoeffMatrix(originalFromDwt, watermarkedMatrix):
if(originalFromDwt.shape[0] != originalFromDwt.shape[1]):
raise ValueError('fromDwt input is not a square matrix')
numBlockSide = int(originalFromDwt.shape[0]/blockSizeDwtDctSvd)
toInvDwt = np.empty((originalFromDwt.shape[0], originalFromDwt.shape[1]))
for i in range(numBlockSide):
for j in range(numBlockSide):
minX = int(i * blockSizeDwtDctSvd)
maxX = int((i+1) * blockSizeDwtDctSvd)
minY = int(j * blockSizeDwtDctSvd)
maxY = int((j+1) * blockSizeDwtDctSvd)
dctMatrix = dct(dct(originalFromDwt[minX:maxX, minY:maxY],axis=0, norm='ortho'),axis=1, norm='ortho')
dctMatrix[0, 0] = watermarkedMatrix[i,j]
toInvDwt[minX:maxX, minY:maxY] = idct(idct(dctMatrix,axis=0, norm='ortho'),axis=1, norm='ortho')
return toInvDwt
def svdInsert(dctMatrix, watermark, alphaPaperLocal):
# apply SVD to the result matrix. The only matrix that we need is "s" (according to dctMatrix = USV^(T))
u,s,v = np.linalg.svd(dctMatrix)
s_mod = np.diag(s) + watermark * alphaPaperLocal
dcDwtWat = np.matmul(u,np.matmul(s_mod,v))
return dcDwtWat
# @param originalImg Original image with shape of (512,512)
# @param watermark Watermark with shape of (32,32)
# @return watermarkImage Waterarked Image
def embedded(originalImg, watermark):
watermark = watermark.reshape(32,32)
if (originalImg.shape != (512,512)) or (watermark.shape != (32,32)) :
raise ValueError("Size of images aren't standard.")
# Compute dwt of original image
coeff = pywt.dwt2(originalImg, 'haar') # Old: originalImg
LL, (LH, HL, HH) = coeff
## LL
# Find 32 matrixes of higher components of LH component
dctMatrix = dctCoeffMatrix(LL)
# insert on every coefficents matrix a line of watermark
Mp = svdInsert(dctMatrix, watermark, alphaPaper[0])
# Find the modified LL component
LL_star = idctCoeffMatrix(LL, Mp)
## LH
# Find 32 matrixes of higher components of LH component
dctMatrix = dctCoeffMatrix(LH)
# insert on every coefficents matrix a line of watermark
Mp = svdInsert(dctMatrix, watermark, alphaPaper[1])
# Find the modified LH component
LH = idctCoeffMatrix(LH, Mp)
##
## HL
# Find 32 matrixes of higher components of LH component
dctMatrix = dctCoeffMatrix(HL)
# insert on every coefficents matrix a line of watermark
Mp = svdInsert(dctMatrix, watermark, alphaPaper[2])
# Find the modified HL component
HL = idctCoeffMatrix(HL, Mp)
##
## HH
# Find 32 matrixes of higher components of LH component
dctMatrix = dctCoeffMatrix(HH)
# insert on every coefficents matrix a line of watermark
Mp = svdInsert(dctMatrix, watermark, alphaPaper[3])
# Find the modified HH component
HH = idctCoeffMatrix(HH, Mp)
##
# compute inverse of idw2 with watermarked components
watermarkedImage=pywt.idwt2((LL_star,(LH,HL,HH)),'haar')
return watermarkedImage
# @param originalImg Original image with shape of (512,512)
# @param watermark Watermark with shape of (32,32)
# @param ssNum Number of SS watermark to insert into image
# @param alphaSS Alpha of SS
# @return watermarkImage Waterarked Image
def embeddedFinalMethod(originalImg, watermark):
watermarkedImgA = embedded(originalImg, watermark)
cv2.imwrite('../Images/temp.bmp', watermarkedImgA)
watermarkedImg = cv2.imread('../Images/temp.bmp', 0)
return watermarkedImg
def main():
try:
imagesToWatermark = f.imageOnFolder('../originalImages/')
watermark=np.load('../Utilities/panebbianco.npy')
for imagePath in imagesToWatermark:
imageName = imagePath.split('.')[0]
imagePath = "../originalImages/" + imagePath
originalImg = cv2.imread(imagePath, 0)
watermarkedImg = embedded(originalImg, watermark)
cv2.imwrite(f'../watermarkedImages/{imageName}_panebbianco.bmp', watermarkedImg)
cv2.imwrite(f'../imagesToAttack/panebbianco_{imageName}.bmp', watermarkedImg)
print(f'WPSNR {imageName}: {f.wpsnr(originalImg, watermarkedImg)} dB')
except ValueError as e:
print("\x1b[6;31mError: " + str(e) + "\x1b[0m")
if __name__ == "__main__":
main()