Image histogram.
h = histogram(f)
This function computes the number of occurrence of each pixel value. The function histogram_eq is implemented to show an implementation based on the equation of the histogram.
In [12]:
import numpy as np
def histogram(f):
return np.bincount(f.ravel())
In [13]:
def histogram_eq(f):
from numpy import amax, zeros, arange, sum
n = amax(f) + 1
h = zeros((n,),int)
for i in arange(n):
h[i] = sum(i == f)
return h
In [14]:
def histogram_eq1(f):
import numpy as np
n = f.size
m = f.max() + 1
haux = np.zeros((m,n),int)
fi = f.ravel()
i = np.arange(n)
haux[fi,i] = 1
h = np.add.reduce(haux,axis=1)
return h
In [1]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python histogram.ipynb
import numpy as np
import sys,os
import matplotlib.image as mpimg
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
In [2]:
if testing:
f = np.array([[0,1,2,3,4],
[4,3,2,1,1]], 'uint8')
h = ia.histogram(f)
print(h.dtype)
print(h)
In [3]:
if testing:
h1 = ia.histogram_eq(f)
print(h1.dtype)
print(h1)
In [4]:
if testing:
h1 = ia.histogram_eq1(f)
print(h1.dtype)
print(h1)
In [5]:
if testing:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
f = mpimg.imread('../data/woodlog.tif')
plt.imshow(f,cmap='gray')
In [6]:
if testing:
h = ia.histogram(f)
plt.plot(h)
In [7]:
if testing:
import numpy as np
from numpy.random import rand, seed
seed([10])
f = (255 * rand(1000,1000)).astype('uint8')
%timeit h = ia.histogram(f)
%timeit h1 = ia.histogram_eq(f)
%timeit h2 = ia.histogram_eq1(f)
In [9]:
if testing:
print(ia.histogram(np.array([3,7,0,0,3,0,10,7,0,7])) == \
np.array([4, 0, 0, 2, 0, 0, 0, 3, 0, 0, 1]))
In [ ]: