I am trying to write a function that computes CDF of an image, but the output I get is always zero. what am I doing wrong?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
function cdf = CDF(im,j)
rows = size(im,1);
cols = size(im,2);
s=double(rows^cols);
sum=double(0);
cd
for i=0:j
for r=1: rows
for co=1: cols
pixel=im (r,co);
if (pixel==i)
sum=sum+1;
end
end
end
end
cdf = double(sum/s);
3 Commenti
dpb
il 24 Ott 2018
...
if (pixel==i)
sum=sum+1;
end
will only sum if the value of the image itself is an integer equal to the particular value of i on that pass thru the outer loop.
What is j and what is the normalization for the image? If it were scaled, the values wouldn't ever match which would cause the symptom.
You're not going to get a CDF anyway, because you're only going to return a single value.
Risposte (2)
Steven Lord
il 24 Ott 2018
I think you should use histcounts or histogram. Since you want the CDF you'd specify that as the 'Normalization' in your histcounts or histogram call.
0 Commenti
Abdul Rehman
il 24 Ott 2018
Modificato: Abdul Rehman
il 25 Ott 2018
Basically if u want to calculate cdf of image, then you have to follow three step,
1.Histogram
2.Normalized Histogram
3.CDF
Here you have a code of histogram using for loops (but you can use "Hist" function).
if true
function [hist hist_P]=histor_g(im)
hist=zeros(1,256);
[r c]=size(im);
s=r*c;
for i=1:r
for j=1:c
int_val=im(i,j);
hist(int_val+1)=hist(int_val+1)+1;
end
end
hist_P=hist./s; %Normalized Histogram(PDF)
end
end
As using this function you van get PDF(Normalized Histogram)
then you can calculate CDF.
if true
function [c_hist]=cum_h(hp)
[r c]=size(hp);
c_hist=zeros(1,256);
for j=1:c
if(j ==1)
c_hist(j)=hp(j);
else
c_hist(j)=hp(j)+c_hist(j-1);
end
end
end
Hopefully you get it thanks.
0 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!