-
Notifications
You must be signed in to change notification settings - Fork 7
/
metrics.py
76 lines (66 loc) · 1.97 KB
/
metrics.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
import scipy.stats as stats
import numpy as np
import math
from sklearn.metrics import mean_squared_error
#Pearson index to "view" the correlation between two watermarks
def correlationIndex(wOriginal, wExtracted):
p = stats.pearsonr(wOriginal, wExtracted)
return p
#Binary Detection of corretc watermark based on threshold
def binaryDetection(index, threshold):
return (True if abs(index[0]) > threshold else False)
#PSNR for watermark images
def PSNR(wOriginal, wExtracted):
#print(np.array_equal(wOriginal, wExtracted))
mse = 0
for i in range(wOriginal.shape[0]):
valueOriginal = wOriginal[i]
if wOriginal[i] == True:
valueOriginal = 255
elif wOriginal[i] == False:
valueOriginal = 0
valueExtracted = wExtracted[i]
if wExtracted[i] == True:
valueExtracted = 255
elif wExtracted[i] == False:
valueExtracted = 0
mse += (valueOriginal - valueExtracted)**2
mse /= wOriginal.shape[0]
'''
try:
mse = np.mean((wOriginal - wExtracted)**2)
except TypeError:
mse = np.mean((wOriginal ^ wExtracted)**2)
print(mse)
'''
#mse = mean_squared_error(wOriginal, wExtracted)
if mse == 0: mse+=1
psnr = 10 * math.log10((255.0**2)/mse)
return psnr
#SNR for audio files
def SNR(data):
mean = np.mean(data)
std = np.std(data)
return mean/std
'''
TESTING
'''
'''
image1 = Image.new("L",(3,4))
image1.putpixel(xy=(1,2),value=255)
image1.putpixel(xy=(2,2),value=100)
image1.putpixel(xy=(1,0),value=150)
image1.putpixel(xy=(2,3),value=55)
image1.putpixel(xy=(0,0),value=70)
array1 = np.ravel(np.asarray(image1))
image2 = Image.new("L",(3,4))
image2.putpixel(xy=(1,2),value=25)
image2.putpixel(xy=(2,2),value=10)
image2.putpixel(xy=(1,0),value=149)
image2.putpixel(xy=(2,3),value=50)
image2.putpixel(xy=(0,0),value=40)
array2 = np.ravel(np.asarray(image2))
p = correlationIndex(array1, array2)
print(p)
print(binaryDetection(p, 0.7))
'''