histogram (specification or macthing) work but there is a wrong thing

4 views (last 30 days)
hi
i have this code that do histogram (specification or matching) it work well for some image and for many time there is white block appear
why this happen
here as my result
here is my code
please any help
clc; clear all;
colormap('gray');
%%%requiredhist
img=imread('requiredhist.png');
figure(1);
imshow(img);
img=double(img); %input image
hist1=zeros(1,256);
[m n]=size(img);
for i=1:m
for j=1:n
hist1(img(i,j)+1)=hist1(img(i,j)+1)+1;
end
end
PDF=hist1/(m*n);
CDF(1)=PDF(1);
for k=2:256
CDF(k)=CDF(k-1)+PDF(k);
end
L=256;
x1=imread('light52.png');
figure(2)
imshow(x1);
x0=double(x1); %input image
[m,n]=size(x0);
len=m*n; %number of pixels
x=reshape(x0,len,1); %convert to [len:1]
xpdf=hist(x,[0:L-1]); % pdf, 1 x L
xpdf=xpdf/len;%Normalize it to get nk/n (eq 3.3-7)....(lenis equal to sum(xpdf), number %of pixels...)
sk=xpdf*triu(ones(L));%CDF (eq 3.3-8), (eq 3.3-13)
zk=CDF;
mapping=zeros(256);
z0=zeros(m,n);
for q=1:L
for p=mapping(q)+1:L
if (zk(p)>=sk(q))
mapping(q) = p;
list=find(x0 == q-1); a=size(list);%find value
%in input image
z0(list)=p; %map sk value for each k valued
%pixel
break;
end
end
end
z0=uint8(z0);
figure(3)
imshow(z0);

Accepted Answer

Image Analyst
Image Analyst on 12 Jun 2013
I don't really follow the code. I suspect that something is going wrong when you try to do stuff with the start of your arrays - down near gray level 0. If you use the debugger, you will be able to solve this on your own very quickly: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/.
By the way, there is an imhistmatch() function in the Image Processing Toolbox which does approximate histogram matching. But like yours, it's not exact - they even say so (but it might be better than yours). If you want an exact match, you have to use random numbers, believe it or not. See my implementation here: http://www.mathworks.com/matlabcentral/fileexchange/28972-custom-shaped-histogram If you want histogram matching done in color, the best algorithm is by Grundland and Dodgson: http://www.eyemaginary.com/Portfolio/ColorHistogramWarp.html

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!