Reputation: 169
I have a proplem when dealing with images in matlab, i have a white image and when i try to print the gray level of the image and increment it by 1 , it gives me 255, it never give me 256. and here is the code. and the count is 0.
function [ count ] = white( I )
[row,col]=size(I);
count=0;
for x=1:row
for y=1:col
g=I(x,y); %the value of the gray level on each pixel
if((g+1) == 256)
count=count+1;
256
end
end
end
Upvotes: 1
Views: 533
Reputation: 26069
Your image class is probably uint8 and 255 is the maximal value of this class . For example:
>> uint8(inf)
ans =
255
Instead try to cast to a different class, for example I=uint32(I)
...
Following @Aganders3, I'll also offer a solution to your code that doesn't use for loops:
count=sum(I(:)>threshold); % Credit to @Jonas and @Aganders3
where threshold is the gray level you want to threshold
Upvotes: 4
Reputation: 5945
I think nate is correct on why this is not working.
Also, consider a much simpler solution to your problem (given I
is full of integers):
count = sum(vector(I == intmax(class(I))));
Upvotes: 2