Skip to content

Instantly share code, notes, and snippets.

@Kulbear
Created September 11, 2017 21:32
Show Gist options
  • Save Kulbear/7c1dbc9f0f58cdf88e43bb61ed98c1bd to your computer and use it in GitHub Desktop.
Save Kulbear/7c1dbc9f0f58cdf88e43bb61ed98c1bd to your computer and use it in GitHub Desktop.
% Author: Ji Yang
%% Description
% A simple implementation of histogram equalization.
%
%% Note
% Histogram equalization is a technique for adjusting image intensities
% to enhance contrast.
%
%% Implementation
clear; close all; clc
I = imread('../images/tire.tif');
L = 256;
[r, c] = size(I);
% Define some matrices we need later
frequency = zeros(L, 1);
prob_dist = zeros(L, 1);
cum_prob = zeros(L, 1);
out_map = zeros(L, 1);
% Get the probability distribution
for i = 1:r
for j = 1:c
value = I(i, j);
frequency(value + 1) = frequency(value + 1) + 1;
prob_dist(value + 1) = frequency(value + 1) / (r * c);
end
end
% Get the cumulative probability distribution and the output mapping
cum_term = 0;
L = 256;
for i = 1:L
cum_term = cum_term + prob_dist(i);
cum_prob(i) = cum_term;
out_map(i) = round((L - 1) * cum_prob(i));
end
% Generate the output image
I_output = out_map(I + 1);
subplot(3,2,1), imshow(I, []), title('The Original Image');
subplot(3,2,2), imhist(I), title('Hist: The Original Image');
subplot(3,2,3), imshow(I_output, []), title('My histqe');
subplot(3,2,4), imhist(uint8(I_output)), title('Hist: My histeq');
subplot(3,2,5), imshow(histeq(I), []), title('The built-in histeq');
subplot(3,2,6), imhist(histeq(I)), title('Hist: Built-in histeq');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment