-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_histeq.m
41 lines (32 loc) · 1.1 KB
/
my_histeq.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function [equalized_img, equalized_hist, cdf] = my_histeq(img)
%my_histeq: Performs the histogram equalizatiaon of the image
%INPUT:
% img: image with (either graylevel or rgb)
%OUTPUT:
% equalized_img: Transformed image with histogram equalization
% equalized_hist: Histogram of the transformed image
% cdf: commulative density function of original image
[r, c] = size(img); % getting rows and columns of given image
number_of_pixels = r*c; % total pixels in the image M*N
gray_levels = 256;
my_hist = my_imhist(img); % calculate histogram of the image
prob = my_hist/number_of_pixels; % calculate PMF of the histograms (or normalized histogram)
% cdf of the original image
cdf = zeros(gray_levels, 1);
cdf(1) = prob(1);
for i=2:256
cdf(i) = cdf(i-1)+prob(i);
end
% discretizing the cdf with 256 gray levels
cdf = cdf * (gray_levels-1);
cdf = uint8(cdf);
% mapping the calculated cdf to the new image
equalized_img = img;
for i=1:r
for j=1:c
equalized_img(i, j) = cdf(img(i, j)+1);
end
end
% calculating the historam of the transformed image
equalized_hist = my_imhist(equalized_img);
end