BBHE Code not working on Color Images
Show older comments
a=imread('C:\Users\Ritu\Documents\dez\bot.png');
b = a(:,:,1);
imshow(b); figure, imhist(b); sz=size(b); o_mean = round(mean(b(:)));
h_l = zeros(256,1);
h_u = zeros(256,1);
for i = 1:sz(1)
for j = 1:sz(2)
g_val = b(i,j);
if(g_val<=o_mean)
h_l(g_val+1) = h_l(g_val+1) + 1;
else
h_u(g_val+1) = h_u(g_val+1) + 1;
end
end
end
disp(h_l)
disp(h_u)
%pdf
nh_l = h_l/sum(h_l);
nh_u = h_u/sum(h_u);
%cdf
hist_l_cdf = double(zeros(256,1));
hist_u_cdf = double(zeros(256,1));
hist_l_cdf(1) = nh_l(1);
hist_u_cdf(1) = nh_u(1);
for k = 2:o_mean
hist_l_cdf(k) = hist_l_cdf(k-1) + nh_l(k);
end
for k=2:256
hist_u_cdf(k) = hist_u_cdf(k-1) + nh_u(k);
end
equalized_img = zeros(sz);
range_l = [0 o_mean];
range_u = [(o_mean+1) 255];
for i =1:sz(1)
for j =1:sz(2)
g_val = b(i,j);
if(g_val<=o_mean)
equalized_img(i,j) = range_l(1) + round(((range_l(2)-range_l(1))*hist_l_cdf(g_val+1)));
else
equalized_img(i,j) = range_u(1) + round(((range_u(2)-range_u(1))*hist_u_cdf(g_val+1)));
end
end
end
o=mat2gray(equalized_img);
figure, imshow(o);
figure, imhist(o);
3 Comments
CSE
on 26 Mar 2014
CSE
on 27 Mar 2014
pankaj swaroop
on 28 Mar 2017
Edited: pankaj swaroop
on 28 Mar 2017
To run it on rgb. Follow this
1. Save the above code with name BBHE and first line:
function BBHE(a)
2. Then run these line to get output image which would improve the contrast of rgb image
out1=BBHE(a(:,:,1));
out2=BBHE(a(:,:,2));
out3=BBHE(a(:,:,3));
figure
imshow(cat(3,out1,out2,out3));
Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!