# How to find the intensity percentage in a gray scale image?

7 visualizaciones (últimos 30 días)
Meshooo el 30 de Jul. de 2014
Movida: DGM el 20 de Feb. de 2023
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
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Meshooo el 4 de Ag. de 2014
Movida: DGM el 20 de Feb. de 2023
Thank you Image Analyst, Geoff, Anand. Now I could understand the problem.

Iniciar sesión para comentar.

Geoff Hayes el 30 de Jul. de 2014
Editada: Geoff Hayes el 30 de Jul. de 2014
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!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Más respuestas (2)

Image Analyst el 30 de Jul. de 2014
Your code should be okay. Though it would be better to just use the original image if you have it rather than use getimage();
clc;
workspace;
% Read in sample image and divide by 2.
grayImage = imread('pout.tif') / 2;
subplot(1, 2, 1);
% Display at full range 0-255.
imshow(grayImage, [])
title('Original image, scaled to 0-255 for display', 'FontSize', 22);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get max of original image and what is returned from getimage
maxGL = max(grayImage(:))
I = getimage(gca);
maxGL2 = max(I(:))
% They should be the same. maxGL2 won't be 255 even though displayed image is 255.
X = (max(I(:))/100) *99.5
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(1, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', 22);
xlim([0 grayLevels(end)]); % Scale x axis manually.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Anand el 31 de Jul. de 2014
If I is a uint8 image, then max(I(:)) will be a uint8 value and multiplying that by .995 will mean its still 255. You can see this as follows:
>> uint8(255)*99.5
ans =
255
To fix this, cast the maximum to a double, something like this:
X = double(max(I(:))/100) *99.5;
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Image Analyst el 31 de Jul. de 2014
Yes, if it goes more than 255 (like if you multiply by 99.5 instead of .995), it clips. If it's less than 255, it rounds to nearest integer. Compare this:
uint8(255)*.995
ans = 254

Iniciar sesión para comentar.

### Categorías

Más información sobre Image Arithmetic en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by