Transforms an image with an arbitrary histogram to one with a flat histogram
Discrete Implementation
function histogram_eq(inimgname)
img=imread(imgname);
figure; imshow(img);
M,N]=size(img);
[
H=imhist(img);
H=H/(M*N);
figure; bar(H);
%Computing the mapping function
for (k=1:256)
C(k)=uint8(sum(H(1:k))*255);
end;
% C = uint8(cumsum(H)*255);
figure;plot(C);
%perform mapping
for (i=1:M)
for (j=1:N)
f=double(img(i,j))+1;
histeqimg(i,j)=C(f);
end;
end;
%note the above loop can be replaced by: %histeqimg=C(double(img)+1);
%this will be much faster!
figure;
imshow(histeqimg);
import cv2
import numpy as np
from matplotlib import pyplot as plt
# read the image using openCV
= cv2.imread('kid.jpg',0)
img # Calculate the histogram and corresponding bins hist,bins = np.histogram(img.flatten(),256,[0,256])
# Calculate the cdf and normalize the values to 0-255 cdf = hist.cumsum()
= cdf * 255/ cdf[-1]
cdf_normalized # Replace the vales with normalized cdf values img_histeq = cdf_normalized[img]
#display results
= plt.figure()
fig = plt.subplot(2,2,1) ax1.get_xaxis().set_visible(False) ax1.get_yaxis().set_visible(False) plt.imshow(img,cmap=plt.cm.gray)
ax1 = plt.subplot(2,2,2) plt.hist(img.ravel(),256,[0,256])
ax2 = plt.subplot(2,2,3) ax3.get_xaxis().set_visible(False) ax3.get_yaxis().set_visible(False) plt.imshow(img_histeq,cmap=plt.cm.gray) ax4 = plt.subplot(2,2,4) plt.hist(img_histeq.ravel(),256,[0,256]) plt.show() ax3
Using non-overlapping blocks to compute the histograms and the mapping function for each block center.
The pixel’s mapping function is determined by interpolating the 4 mapping functions of the four block centers
Using blinear weights determined based on its distance to the block centers
References: