Function iahistogram

Synopse

Image histogram.

  • h = iahistogram(f)
    • f: Input image. Pixel data type must be integer.
    • h: Output, integer vector.

Description

This function computes the number of occurrence of each pixel value. The function iahistogram_eq is implemented to show an implementation based on the equation of the histogram.

Function Code

1. import numpy as np
2. 
3. def iahistogram(f):
4. 
5.     return np.bincount(f.ravel())

Function Code for brute force implementation

1. def iahistogram_eq(f):
2. 
3.     from numpy import amax, zeros, arange, sum
4. 
5.     n = amax(f) + 1
6.     h = zeros((n,),int)
7.     for i in arange(n):
8.       h[i] = sum(i == f)
9.     return h

Function code for bidimensional matrix implementation

01. def iahistogram_eq1(f):
02. 
03.     import numpy as np
04.     n = f.size
05.     m = f.max() + 1
06.     haux = np.zeros((m,n),int)
07.     fi = f.ravel()
08.     i = np.arange(n)
09.     haux[fi,i] = 1
10.     h = np.add.reduce(haux,axis=1)
11.     return h

Examples

Numerical examples

1. import ia636 as ia
2. 
3. f = uint8(
4.    [[0,1,2,3,4],
5.     [4,3,2,1,1]])
6. 
7. h = ia.iahistogram(f)
8. print h.dtype
9. print h
int64
[1 3 2 2 2]
1. h1 = ia.iahistogram_eq(f)
2. print h1.dtype
3. print h1
int64
[1 3 2 2 2]
1. h1 = ia.iahistogram_eq1(f)
2. print h1.dtype
3. print h1
int64
[1 3 2 2 2]

Example 1

1. f = adreadgray('woodlog.pgm')
2. adshow(f)
3. h = ia.iahistogram(f)
4. adshow(ia.iaplot([h]))

Speed performance

01. import numpy as np
02. from numpy.random import rand, seed
03. import time
04. 
05. seed([10])
06. f = (256 * rand(1000,1000)).astype(uint8)
07. t1 = time.time();    h = ia.iahistogram(f); tf1 = time.time()
08. t2 = time.time();    h = ia.iahistogram_eq(f); tf2 = time.time()
09. t3 = time.time();    h = ia.iahistogram_eq1(f); tf3 = time.time()
10. print 'iahistogram = ', tf1-t1
11. print 'iahistogram_eq = ', tf2-t2
12. print 'iahistogram_eq1 = ', tf3-t3
iahistogram =  0.018904209137
iahistogram_eq =  1.22028613091
iahistogram_eq1 =  2.37262201309

Equation