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?
Show older comments
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 Comments
Image Analyst
on 24 Oct 2018
It looks like lots of things. But let's get some information first. What is im - a gray scale image, a color image, or a binary image? What is (the very badly-named) j - its value and it's purpose? Are you just trying to count the number of pixels in the image that have value j? Is cdf the cumulative distribution function? If so, why not simply call histcounts() and cumsum()? Why are you using sum - a very important built-in function - as the name of your variable?
dpb
on 24 Oct 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.
Reihaneh Khoshghadam
on 24 Oct 2018
Answers (2)
Steven Lord
on 24 Oct 2018
0 votes
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.
Abdul Rehman
on 24 Oct 2018
Edited: Abdul Rehman
on 25 Oct 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.
Categories
Find more on Blocked Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!