Please complete the provided Matlab code to implement Histogram equalization. Pl
ID: 2248723 • Letter: P
Question
Please complete the provided Matlab code to implement Histogram equalization.
Please use the follow program to test your function.
J is output of the histogram equalization function provided by Matlab. Please compare your output and Matlab’s output.
function OutIm = HistogramEq(InIm)
HistogramEq Code:
% Input:
% InIm - input image
%
% Output:
% OutIm - output image
%
%%%%%%%%
%%%%%%%%
% get the size of input image;
[row, col] = size(InIm);
% initalize the histogram array;
H = zeros(1,256);
% initalize the output image;
OutIm = uint8(zeros(row, col));
% Step 1: computer histogram (note the index starts from 1 but gray level starts from 0);
for i=1:row
for j=1:col
% Your code here!
end
end
% Step 2: compute PDF;
PDF= % Your code here!
% Step 3: compute CDF (Hint: use 'cumsum' function, type 'help cumsum' to see help)
CDF= % Your code here!
% Step 4: compute transformation T (T=CDF*(L-1) and L=256);
T= % Your code here!
% Step 5: generate output image using transformation T;
for i=1:row
for j=1:col
OutIm(i,j)= % Your coder here!
end
end
Explanation / Answer
GIm=imread('tire.tif');
numofpixels=size(GIm,1)*size(GIm,2);
figure,imshow(GIm);
title('Original Image');
HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
%freq counts the occurrence of each pixel value.
%The probability of each occurrence is calculated by probf.
for i=1:size(GIm,1)
for j=1:size(GIm,2)
value=GIm(i,j);
freq(value+1)=freq(value+1)+1;
probf(value+1)=freq(value+1)/numofpixels;
end
end
sum=0;
no_bins=255;
%The cumulative distribution probability is calculated.
for i=1:size(probf)
sum=sum+freq(i);
cum(i)=sum;
probc(i)=cum(i)/numofpixels;
output(i)=round(probc(i)*no_bins);
end
for i=1:size(GIm,1)
for j=1:size(GIm,2)
HIm(i,j)=output(GIm(i,j)+1);
end
end
figure,imshow(HIm);
title('Histogram equalization');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.