This example calculates the histogram from an Image and performs Histogram
Equalization on the same image.def HistEqualize(image,report=0):
im =GetImageMatr(image)
rows,cols=im.dim()
if report: print image,rows,cols
hist=range(0,256)
for i in range(0,256): hist[i]=0
for i in range(0,rows*cols): hist[ im[i] ]+=1
nump = rows*cols
alpha = 255.0/nump
def printhist(hist):
max=0
for it in hist:
if it>max:max=it
for i in range(0,len(hist)):
print '%03d %03d' % (i,hist[i]),'*' * int((hist[i]/(max*1.0)) * 100)
if report:
printhist(hist)
print '='*30
cumfreq =range(0,256)
cumfreq[0]=hist[0]
cum =hist[0]
cumfreq[0]=cum
for i in range(1,256):
cum+=hist[i]
cumfreq[i]=cum
for i in range(0,rows*cols):
v= int(cumfreq[im[i]]*alpha)
if v>255:v=255
im[i] = v
if report:
for i in range(0,256): hist[i]=0
for i in range(0,rows*cols): hist[ im[i] ]+=1
printhist(hist)
SetImageMatr(image,im)
|