-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualize_and_Histeq.m
More file actions
85 lines (72 loc) · 2.29 KB
/
Equalize_and_Histeq.m
File metadata and controls
85 lines (72 loc) · 2.29 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function im2 = equalize(im)
%Input: im (uint8 grayscale image)
% or:im (uint8 colored image)
%Output: im2 (uint8 output image after histogram equalization)
%Get the size of the input image
[rows, cols] = size(im);
%Compute the histogram of the input image (256 possible intensity values for uint8)
histogram = zeros(1, 256);
for i = 1:rows
for j = 1:cols
intensity = im(i, j); %Get the pixel intensity value
histogram(intensity + 1) = histogram(intensity + 1) + 1;
end
end
%Compute the cumulative distribution function (CDF)
cdf = zeros(1, 256);
cdf(1) = histogram(1); %First value of CDF is the first histogram value
for k = 2:256
cdf(k) = cdf(k - 1) + histogram(k);
end
%Normalize the CDF (Scale it to the range [0, 255])
cdf_min = min(cdf(cdf > 0)); %The first non-zero CDF value
cdf_max = cdf(end); %The last value in the CDF (total number of pixels)
%Create a mapping of each intensity value to the new equalized intensity
equalized_map = zeros(1, 256);
for k = 1:256
equalized_map(k) = round((cdf(k) - cdf_min) / (cdf_max - cdf_min) * 255);
end
%Apply the mapping to get the new image
im2 = zeros(rows, cols, 'uint8');
for i = 1:rows
for j = 1:cols
im2(i, j) = equalized_map(im(i, j) + 1);
end
end
end
%Plot the histeqed images
%im1_histeq = histeq(im1);
%im3_histeq = histeq(im2);
%Load the original image, show the original image, perform equalize
%function, & show the equalized & histeq image
im_raw = imread('image_colored.tif');
% Can also be replaced with grayscale image
% convert RGBA image (4 channels) to RGB (3 channels)
if ndims(im_raw) == 3 && size(im_raw, 3) == 4
im = im_raw(:, :, 1:3);
else
im = im_raw;
end
subplot(1, 3, 1);
imshow(im);
title('Original');
ylabel('image');
im2 = equalize(im);
subplot(1, 3, 2);
imshow(im2);
title('Equalized');
im1_histeq = histeq(im);
subplot(1, 3, 3);
imshow(im1_histeq);
title('histeq');
% plot the histograms in another figure
figure;
subplot(1, 3, 1);
imhist(im);
title('Original Histogram');
subplot(1, 3, 2);
imhist(im2);
title('equalized Histogram');
subplot(1, 3, 3);
imhist(im1_histeq);
title('histeq Histogram');