Skip to content

Instantly share code, notes, and snippets.

@abdul-rehman-2050
Last active July 15, 2021 03:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdul-rehman-2050/a61adb930da7984b6888deb78737fa92 to your computer and use it in GitHub Desktop.
Save abdul-rehman-2050/a61adb930da7984b6888deb78737fa92 to your computer and use it in GitHub Desktop.
histogram equalization via matlab from scratch
function new_im = im2imhisteq(I)
% takes an Input Image and
%http://www.osmanoglu.org/supplement/unpublishedWork/DigitalImageProcessing_HistogramEqualizationMatching.pdf
%Manhas, Pratima & Thakral, Shaveta. (2018)...
% Comparative analysis of different wavelet ...
% filters and Image enhancement using Histogram Equalization.
L=256;
[m,n]=size(I);
len=m*n; %number of pixels
dum_hist=imhist(I); % pdf, 1 x L
probsAll=dum_hist/len;
cdfsAll=probsAll'*triu(ones(L));
new_im=zeros(m,n);
for k=0:L-1
if (probsAll(k+1) > 0)
list=find(I == k);
new_im(list)=(cdfsAll(k+1)*L); %map sk value for each k valued
end
end
new_im = uint8(new_im);
end
function new_hist = myhisteq(dum_hist)
L = length(dum_hist);
new_hist = zeros(1,L);
total_pixels = sum(dum_hist);
probsAll = dum_hist/total_pixels;
% calculate CDF
cdfsAll = zeros(1,L);
cdfsAll(1)=probsAll(1);
for i=2:L
cdfsAll(i) = cdfsAll(i-1)+probsAll(i);
end
% our final result
sk = (cdfsAll * (L-1));
rfval = uint8(sk);
%map to new histogram
for i=1:L
new_hist(rfval(i)+1) = new_hist(rfval(i)+1)+dum_hist(i);
end
function myhist = myhistogram(I)
L=256;
myhist=zeros(1,L);
[m,n]=size(I);
len = m*n;
I1D = reshape(I,len,1);
for i = 1:len
myhist(I1D(i)+1) = myhist(I1D(i)+1)+1;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment