MATLAB: How to find the intensity percentage in a gray scale image

find decimal value from intensity imageImage Acquisition ToolboxImage Processing ToolboxMATLAB

Dear all,
I have 8bit gray scale image "I". I want to find "X" which is the 99.5% of the maximum intensity value. So if the image has at least one white pixel (255), then "X" should be 253.725.
I tried the following code but I'm getting different answer:
I = getimage;
X = (max(I(:))/100) *99.5;
Why the value for "X" is 255 not 253.725?
Any help will be appreciated.
Meshoo

Best Answer

Meshoo - what is the answer that you are getting? 255? From your question, I think it is safe to assume that the data type for your image is an 8-bit unsigned integer. So if your maximum value is 255 then
maxVal = uint8(255);
maxValBy100 = maxVal/100; % this is now 3 because the data type is uint8
maxValBy100*99.5 % this is now 255 since 3*99.5 is greater than the max
% allowed 8-bit unsigned int (255) so is capped at that
% maximum
Since you are mixing doubles with 8-bit unsigned ints, then just cast the maximum value of I to a double before dividing by 100
X = (double(max(I(:)))/100) *99.5;
and the above will return the desired 253.725. Try this and see what happens!
Related Question