-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwatermark_embedding_extraction.py
375 lines (302 loc) · 11.3 KB
/
watermark_embedding_extraction.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
from utils import *
from PIL import Image
from audio_managing import frameToAudio, audioToFrame
from image_managing import binarization, grayscale, imgSize
import numpy as np
import math
import sys
ALPHA = 0.1
#Check if the image is in grayscale and covert it in this mode
def isImgGrayScale(image):
if image.mode != "L":
image = grayscale(image)
return image
#Check if the image is in binary and covert it in this mode
def isImgBinary(image):
if image.mode != "1":
image = binarization(image)
return image
#Embedding of width and heigth. Audio must be linear and not frames
def sizeEmbedding(audio, width, height):
embedded = audio.copy()
#Embedding width and heigth
embedded[0][-1] = width
embedded[1][-1] = height
return embedded
def sizeExtraction(audio):
#Extraction of width and heigth
return int(audio[0][-1]), int(audio[1][-1])
#Check if audio is divided in frames:
# if true, it joins audio and then the inverse will be called
# if false, it does nothing
def isJoinedAudio(audio):
if type(audio[0]) in (np.int16, np.int64, np.float64, int, float):
numOfFrames = -1 #Audio is not divided in frame
joinAudio = audio.copy()
else:
numOfFrames = audio.shape[0]
joinAudio = frameToAudio(audio)
return joinAudio, numOfFrames
def iisJoinedAudio(audio, numOfFrames):
if type(audio[0]) in (np.int16, np.int64, np.float64, int, float):
return audioToFrame(audio, numOfFrames)
def LSB(audio, image):
image = isImgBinary(image)
joinAudio, numOfFrames = isJoinedAudio(audio)
width, heigth = imgSize(image)
audioLen = len(joinAudio)
if (width * heigth) + 32 >= audioLen:
sys.exit("LEAST SIGNIFICANT BIT: Cover dimension is not sufficient for this payload size!")
joinAudio = sizeEmbedding(joinAudio, width, heigth)
#Embedding watermark
for i in range(width):
for j in range(heigth):
value = image.getpixel(xy=(i,j))
value = 1 if value == 255 else 0
x = i*heigth + j
joinAudio[x + 32] = setLastBit(joinAudio[x + 32],value)
if numOfFrames is not -1:
return audioToFrame(joinAudio, numOfFrames)
else:
return joinAudio
def iLSB(audio):
#Verify if audio is divided in frames
joinAudio, numOfFrames = isJoinedAudio(audio)
width, heigth = (128,128)#sizeExtraction(joinAudio)
image = Image.new("1",(width,heigth))
#Extraction watermark
for i in range(width):
for j in range(heigth):
x = i*heigth + j
value = getLastBit(joinAudio[x+32])
image.putpixel(xy=(i,j),value=value)
return image
#Delta embedding mixed with LSB technique for embedding of width and heigth
def bruteBinary(coeffs, image):
image = isImgBinary(image)
joinCoeffs = coeffs.copy()
coeffsLen = len(coeffs)
frameLen = len(coeffs[0])
width, heigth = imgSize(image)
if (width * heigth) + 2 >= coeffsLen:
sys.exit("DELTA DCT: Cover dimension is not sufficient for this payload size!")
joinCoeffs = sizeEmbedding(joinCoeffs, width, heigth)
#Embedding watermark
for i in range(width):
for j in range(heigth):
value = image.getpixel(xy=(i,j))
x = i*heigth + j
joinCoeffs[x+2] = setBinary(joinCoeffs[x+2], value)
return joinCoeffs
def ibruteBinary(coeffs):
joinCoeffs = coeffs.copy()
width, heigth = (128,128)#sizeExtraction(joinCoeffs)
extracted = Image.new("1",(width,heigth))
coeffsLen = len(joinCoeffs)
#Extraction watermark
for i in range(width):
for j in range(heigth):
x = i*heigth + j
try:
value = getBinary(joinCoeffs[x+2])
except IndexError:
value = 0
extracted.putpixel(xy=(i,j),value=value)
return extracted
#Delta embedding mixed with LSB technique for embedding of width and heigth
def deltaDCT(coeffs, image):
image = isImgBinary(image)
width, heigth = imgSize(image)
joinCoeffs = coeffs.copy()
coeffsLen = len(coeffs)
#Embedding watermark
for i in range(width):
for j in range(heigth):
value = image.getpixel(xy=(i,j))
x = i*heigth + j
v1, v2 = subVectors(joinCoeffs[x])
norm1, u1 = normCalc(v1)
norm2, u2 = normCalc(v2)
norm = (norm1 + norm2) / 2
norm1, norm2 = setDelta(norm, 10, value)
v1 = inormCalc(norm1, u1)
v2 = inormCalc(norm2, u2)
joinCoeffs[x] = isubVectors(v1, v2)
return joinCoeffs
def ideltaDCT(coeffs):
joinCoeffs = coeffs.copy()
width, heigth = (128,128)#sizeExtraction(joinCoeffs)
extracted = Image.new("L",(width,heigth))
coeffsLen = len(coeffs)
#Extraction watermark
for i in range(width):
for j in range(heigth):
x = i*heigth + j
if x < coeffsLen:
v1, v2 = subVectors(joinCoeffs[x])
norm1, u1 = normCalc(v1)
norm2, u2 = normCalc(v2)
value = getDelta(norm1 , norm2)
else:
value = 0
extracted.putpixel(xy=(i,j),value=value)
return extracted
#Delta embedding mixed with LSB technique for embedding of width and heigth
def bruteGray(coeffs, image):
image = isImgGrayScale(image)
joinCoeffs = coeffs.copy()
coeffsLen = len(coeffs)
frameLen = len(coeffs[0])
width, heigth = imgSize(image)
if (width * heigth) + 2 >= coeffsLen:
sys.exit("DELTA DCT: Cover dimension is not sufficient for this payload size!")
joinCoeffs = sizeEmbedding(joinCoeffs, width, heigth)
#Embedding watermark
for i in range(width):
for j in range(heigth):
value = image.getpixel(xy=(i,j))
x = i*heigth + j
joinCoeffs[x+2] = setGray(joinCoeffs[x+2], value)
return joinCoeffs
def ibruteGray(coeffs):
joinCoeffs = coeffs.copy()
width, heigth = (128,128)#sizeExtraction(joinCoeffs)
extracted = Image.new("L",(width,heigth))
coeffsLen = len(coeffs)
#Extraction watermark
for i in range(width):
for j in range(heigth):
x = i*heigth + j
try:
value = getGray(joinCoeffs[x+2])
except IndexError:
value = 0
extracted.putpixel(xy=(i,j),value=value)
return extracted
def embed_bit(frame, step, bit):
data = frame.copy()
#we want frame[x] > frame[x+1]
if bit == 1:
if data[ste]
def embedding(coeffs, image):
image = isImgGrayScale(image)
joinCoeffs = coeffs.copy()
coeffsLen = len(coeffs)
frameLen = len(coeffs[0])
if frameLen < 16:
sys.exit("EMBEDDING: Frame length must be >= 16!")
#Embedding watermark
for i in range(width):
for j in range(heigth):
value = image.getpixel(xy=(i,j))
bin_value = decToBinary(value, bits=8)
x = i*heigth + j
for b in range(0,16, 2):
joinCoeffs[x][b] =
return joinCoeffs
def iembedding(coeffs):
joinCoeffs = coeffs.copy()
width, heigth = (128,128)#sizeExtraction(joinCoeffs)
extracted = Image.new("L",(width,heigth))
coeffsLen = len(coeffs)
#Extraction watermark
for i in range(width):
for j in range(heigth):
x = i*heigth + j
try:
value = getGray(joinCoeffs[x+2])
except IndexError:
value = 0
extracted.putpixel(xy=(i,j),value=value)
return extracted
#The watermark is embedded into k coefficents of greater magnitudo
def magnitudoDCT(coeffs, watermark, alpha):
watermark = isImgGrayScale(watermark)
#print(np.asarray(watermark))
watermark = createImgArrayToEmbed(watermark)
#print(watermark)
coeffs, joinFlag = isJoinedAudio(coeffs)
if(coeffs.shape[0] < len(watermark)):
sys.exit("MAGNITUDO DCT: Cover dimension is not sufficient for this payload size!")
#coeffs = coeffs[:len(watermark)] #to delete for main.py
wCoeffs = []
for i in range(len(watermark)):
wCoeffs.append((coeffs[i])*(1 + alpha*watermark[i]))
for i in range(len(watermark), len(coeffs)):
wCoeffs.append(coeffs[i])
if joinFlag != -1:
wCoeffs = np.asarray(wCoeffs)
wCoeffs = iisJoinedAudio(wCoeffs, joinFlag)
return wCoeffs
#The extraction of watermark from k coefficents of greater magnitudo
def imagnitudoDCT(coeffs, wCoeffs, alpha):
coeffs, joinCoeffsFlag = isJoinedAudio(coeffs)
wCoeffs, joinWCoeffsFlag = isJoinedAudio(wCoeffs)
#coeffs = coeffs[:len(wCoeffs)]
watermark = []
#print(wCoeffs)
for i in range(len(wCoeffs)):
#print("wCoeffs[i]: ", wCoeffs[i], " coeffs[i]: ", coeffs[i])
#if coeffs[i] == 0.0: coeffs[i] = 1.0
if math.isinf((wCoeffs[i] - coeffs[i])/(coeffs[i])):
print("wCoeffs[i]: ", wCoeffs[i], " coeffs[i]: ", coeffs[i], "i: ", i)
watermark.append(abs(math.floor(wCoeffs[i])))
continue
if math.isnan((wCoeffs[i] - coeffs[i])/(coeffs[i])):
print("wCoeffs[i]: ", wCoeffs[i], " coeffs[i]: ", coeffs[i], "i: ", i)
watermark.append(0)
continue
#print(math.floor(abs((wCoeffs[i] - coeffs[i])/(coeffs[i]*alpha))))
watermark.append(math.floor(abs((wCoeffs[i] - coeffs[i])/(coeffs[i]*alpha))))
#watermark.append(abs(math.ceil(wCoeffs[i] - coeffs[i])))
return convertToPIL(createImgMatrix(extractImage(watermark)))
#Extract image coefficients from global watermark array
def extractImage(watermark):
nPixel = (watermark[0]*watermark[1])+2
print(watermark[0], watermark[1])
print(nPixel)
return watermark[:nPixel]
#The image becomes matrix from array
def createImgMatrix(image):
width = image[0]
heigth = image[1]
matrixImg = np.reshape(image[2:], (width, heigth))
print(matrixImg)
return matrixImg
#Convert numpy type to Image type
def convertToPIL(image):
PImage = Image.fromarray((image).astype("uint8"), mode="L")
return PImage
#Routine procedure to embedd the shape of image into flatted array of it
def createImgArrayToEmbed(image):
width, heigth = imgSize(image)
flattedImage = [width, heigth]
tmp = np.ravel(image)
for i in range(len(tmp)):
flattedImage.append(tmp[i])
return flattedImage
'''
TESTING
'''
if __name__ == "__main__":
audio = [1,5,6,7,8,9,4,5,6,1,3,5,4,7,1,5,6,7,8,9,4,5,6,1,3,5,4,7,1,5,6,7,8,9,4,5,6,1,3,5,4,7,5,6,7]
image = Image.new("1",(3,4))
image.putpixel(xy=(1,2),value=1)
lsb = LSB(audio,image)
print(np.asarray(iLSB(lsb)))
image = Image.new("L",(3,4))
image.putpixel(xy=(1,2),value=255)
image.putpixel(xy=(2,2),value=100)
image.putpixel(xy=(1,0),value=150)
image.putpixel(xy=(2,3),value=55)
image.putpixel(xy=(0,0),value=70)
delta = deltaDCT(audio, image)
print(np.asarray(ideltaDCT(audio, delta)))
#flattedImage = createImgArrayToEmbed(image)
#print("flatted image: ", flattedImage)
#lenFlattedImage = len(flattedImage)
#coeffs = audio[:lenFlattedImage]
wCoeffs = magnitudoDCT(audio, image, ALPHA)
print("watermarked coeffs: ", wCoeffs)
watermark = imagnitudoDCT(audio, wCoeffs, ALPHA)
print("extracted watermark: ", watermark)