# Image Contrast Enhancement Cumulative Histogram Equalization Matlab code

A histogram is a graph which shows frequency of anything. The bars that represent frequency of occurring of data in the whole data set.

The histogram of an image normally refers to a histogram of the pixel intensity values. histogram is a graph showing the number of pixels in an image at each different intensity value found in that image. The histogram plots the number of pixels in the image at vertical axis and with a particular brightness value or gray level intensities at horizontal axis.

**Histogram Equalization**

Histogram equalization is used to enhance contrast. It is a process for increasing the contrast in an image by spreading the histogram. The idea of histogram equalization is the distributed pixels in uniformly over the whole intensity range, so the Original Image is transformed to the output image which has a flat histogram. The increase of dynamic range produces an increase in contrast.

For cumulative histogram equalization, the algorithmic steps are as follows.

- Create the histogram for the image.
- Calculate the cumulative distribution function histogram.
- Calculate the new values through the general histogram equalization formula.
- Assign new values for each gray value in the image.

#### Algorithm of matlab code given below

- Load image
- Get image size
- Get image histogram
- Get cumulative distribution function
- Calculate new values via general histogram equalization formula
- Build new image by replacing original gray values with the new gray values
- Show output image & its histogram as result.
Download code histogram-equalization.zip (479 downloads)

close all; clear all; clc; X=rgb2gray(imread('lena.bmp')); numofpixels=size(X,1)*size(X,2); figure(1) subplot(2,2,1); imshow(X); title('Original Image'); subplot(2,2,2); imhist(X); Y=uint8(zeros(size(X,1),size(X,2))); cnts=zeros(256,1); probf=zeros(256,1); prbc=zeros(256,1); cum=zeros(256,1); fin_lc=zeros(256,1); %each pixel count for i=1:size(X,1) for j=1:size(X,2) value=X(i,j); cnts(value+1)=cnts(value+1)+1; probf(value+1)=cnts(value+1)/numofpixels; end end sum=0; n=255; %cumulative distribution probability for i=1:size(probf) sum=sum+cnts(i); cum(i)=sum; prbc(i)=cum(i)/numofpixels; fin_lc(i)=round(prbc(i)*n); end %final output image reconstruction for i=1:size(X,1) for j=1:size(X,2) Y(i,j)=fin_lc(X(i,j)+1); end end subplot(2,2,3); imshow(Y); title('Enhanced Image'); subplot(2,2,4); imhist(Y); % histogram of enhanced image title('Histogram equalization');

Following figure shows the input , output images with its respective histograms.

histeq is matlab built in function which return the grayscale transformation `T`

that maps gray levels in the image `I`

to gray levels in `K`

.

I = rgb2gray(imread('lena.bmp')); //Read Image & Convert to Gray Scale. [K T] = histeq(I); figure(1); subplot(2,2,1); imshow(I) subplot(2,2,2); imhist(I) subplot(2,2,3); imshow(J) subplot(2,2,4); imhist(J)

Output Image

Download code histogram-equalization.zip (479 downloads)