In the following code we calculate the normalized histogram with the histogram equlization of an image.
Before showing the whole code let's look at the content of the code.Normalized Histogram
p(rk) = nk / n where n is the total numbers of pixel and nk is the number of pixels having gray level rk. In our gray level images number of gray levels is taken as 255 and probability of this gray levels frequencies is evaluated.
% for loops to take all indicies of input A
for i=1:rows
for j=1:cols
%value holds the matrix value at i,j indicies
value=A(i,j);
%occur holds the occurrrnce of each value in the input
%since it is a matrix and the index of the matrix is equal to the
%value, it calculates the occurances through the for loops
%1 is added to index because 0 can't be an index
occur(value+1)=occur(value+1)+1;
%calculates the normalized histogram by dividing the occurrence of
%each value by the pixel number
%1 is added to index because 0 can't be an index
histogram(value+1)=occur(value+1)/pixelnumbers;
end
end
In matlab the histogram matrix is evaluated with the above two for loops which travels all the indexes of the input matrix A. Then it calculates the occurrences of each gray levels by simply doing occur(value+1)=occur(value+1)+1 where occur(10) = 100 means that the gray level value 10 has occurred 100 times. So, I get the occurrences of the pixels. Finally, histogram matrix is evaluated by dividing occurrences to number of pixels which gives the normalized histogram. You can see the normlized histogram of the below image.
Histogram Equalization
In histogram equalization we need to find the cumulative distribution function of pixel occurrence. In the solution 9, I already found the occurrences. Now, I use occurrences and the histogram function to obtain equalized histogram function. Then, I multiply the new function with the number of bins that is 255. So, I get the equalized histogram.
% for loops to take all indicies of the histogram for given input
for i=1:size(histogram)
sum = sum+occur(i);
%repeatedly we add the number of occurrences to ongoing ones
cum(i)=sum;
%histogram equalization is done here
equalized_histogram(i)=round((cum(i)/pixelnumbers)*255);
end
After that I need to get new image that is histogram equalized. So, I replace the values in the input array from the values from equalized histogram and save it to the output. For example, say that I have the value x in the input array A (1,1) and the cdf of A (1,1) is y. Then, I make A(1,1)’s value y. The following for loops do that.
The whole code of the program is below. You can use the code using bar command to draw histograms as the following. Comments are included for better understanding.
| Normalized histogram of above image |
In histogram equalization we need to find the cumulative distribution function of pixel occurrence. In the solution 9, I already found the occurrences. Now, I use occurrences and the histogram function to obtain equalized histogram function. Then, I multiply the new function with the number of bins that is 255. So, I get the equalized histogram.
% for loops to take all indicies of the histogram for given input
for i=1:size(histogram)
sum = sum+occur(i);
%repeatedly we add the number of occurrences to ongoing ones
cum(i)=sum;
%histogram equalization is done here
equalized_histogram(i)=round((cum(i)/pixelnumbers)*255);
end
After that I need to get new image that is histogram equalized. So, I replace the values in the input array from the values from equalized histogram and save it to the output. For example, say that I have the value x in the input array A (1,1) and the cdf of A (1,1) is y. Then, I make A(1,1)’s value y. The following for loops do that.
% for loops to take all indicies of input A
for i=1:rows
for j=1:cols
% replace the values of the input with the values from histogram
% equalization and save it to the B
%1 is added to index because 0 can't be an index
B(i,j)=equalized_histogram(A(i,j)+1);
end
end
The whole code of the program is below. You can use the code using bar command to draw histograms as the following. Comments are included for better understanding.
X = imread('Image name');
[b,e,h] = MyHistogram(X);
bar(e);
%Normalized Histogram && Histogram Equalization
%Takes the image as matrix
%Outputs the Equalized Image,Equalized Histogram and Normalized Histogram
%as matrices
function [B,equalized_histogram,histogram] = MyHistogram(A)
%input is converted to double
A = double(A);
%row & columns of A
[rows,cols] = size(A);
%pixel number is found
pixelnumbers = rows*cols;
%Initialize the histogram equalized output image with full of unit8 zeros
%otherwise we need to use imread(matrix,[]) instead of imread([])
B=uint8(zeros(rows,cols));
%occur matrix is used to hold occurrences of each value
occur=zeros(256,1);
%histogram matrix is used for normalized histogram of the input image
histogram =zeros(256,1);
%equalized_histogram matrix is used for equalized histogram of the input
equalized_histogram=zeros(256,1);
%cum is used to hold cumalitives for equalized histogram
cum=zeros(256,1);
%sum is set to 0
sum=0;
% for loops to take all indicies of input A
for i=1:rows
for j=1:cols
%value holds the matrix value at i,j indicies
value=A(i,j);
%occur holds the occurance of each value in the input
%since it is a matrix and the index of the matrix is equal to the
%value, it calculates the occurances through the for loops
%1 is added to index because 0 can't be an index
occur(value+1)=occur(value+1)+1;
%calculates the normalized histogram by dividing the occurance of
%each value by the pixel number
%1 is added to index because 0 can't be an index
histogram(value+1)=occur(value+1)/pixelnumbers;
end
end
%bar(histogram);
% for loops to take all indicies of the histogram for given input
for i=1:size(histogram)
sum = sum+occur(i);
%repeatedly we add the number of occurrences to ongoing ones
cum(i)=sum;
%histogram equalization is done here
equalized_histogram(i)=round((cum(i)/pixelnumbers)*255);
end
% for loops to take all indicies of input A
for i=1:rows
for j=1:cols
% replace the values of the input with the values from histogram
% equalization and save it to the B
%1 is added to index because 0 can't be an index
B(i,j)=equalized_histogram(A(i,j)+1);
end
end
%bar(equalized_histogram);
end
No comments:
Post a Comment