-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshannonsEntropy.py
More file actions
executable file
·53 lines (41 loc) · 1.18 KB
/
shannonsEntropy.py
File metadata and controls
executable file
·53 lines (41 loc) · 1.18 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 4 00:33:27 2015
@author: p
"""
import numpy as np
import cv2
import os
import sys
import math
def getfrequencyofbin(mat,hist):
frequency = 0.0;
for i in range(len(hist)):
Hc = hist[i]
frequency += Hc;
return frequency;
def calcentropy(img,B,G,R):
entropy = 0.0
img = cv2.imread(img)
histB = cv2.calcHist([B],[0],None,[256],[0,256])
histG = cv2.calcHist([G],[0],None,[256],[0,256])
histR = cv2.calcHist([R],[0],None,[256],[0,256])
normB = [float(i)/sum(histB) for i in histB]
normG = [float(i)/sum(histG) for i in histG]
normR = [float(i)/sum(histR) for i in histR]
frequencyR = getfrequencyofbin(R,normR)
for i in range(len(normR)):
Hc = normR[i]
if(Hc!=0.):
entropy += -(Hc/frequencyR) * math.log10((Hc/frequencyR));
frequencyG = getfrequencyofbin(G,normG)
for i in range(len(normG)):
Hc = normG[i]
if(Hc!=0.):
entropy += -(Hc/frequencyG) * math.log10((Hc/frequencyG));
frequencyB = getfrequencyofbin(B,normB)
for i in range(len(normB)):
Hc = normB[i]
if(Hc!=0.):
entropy += -(Hc/frequencyB) * math.log10((Hc/frequencyB));
return entropy