subtract each pixel from average pixel value

i have taken mean pixel value of an image and now i have to subtract from each pixel value of the image the average pixel value...how can i do it?

 Respuesta aceptada

Matt J
Matt J el 17 de Mayo de 2019
It seems deceptively simple...
Image-meanValue

4 comentarios

Harshit Kaushik
Harshit Kaushik el 17 de Mayo de 2019
yeah it was very deceptive ,sometimes we try to do much more than needed,well this worked and the intensity was decreased,thank you :)
Really surprised you accepted this answer even after my explanation below. Just be aware that if you have an integer image (uint8 or uint16) that this answer clips all the resulting values below zero to zero, and all resulting values above zero will get rounded to the nearest integer. See this illustration:
grayImage = uint8(randi(255, 10, 10)) % Create sample uint8 image.
meanValue = mean(grayImage(:)) % Compute the mean.
newImage = grayImage - meanValue % Subtract mean then clip and round
My answer below will give you the accurate answer, but if you'd want to do rounding and clipping anyway, then this answer is fine. Just be careful and aware.
Sara AR
Sara AR el 11 de Nov. de 2019
how can i remove the mean value of the 7x7 pixels around a picture? and lets say the picture is 512*512?
Use conv2().
kernel = ones(7, 7);
kernel(4, 4) = 0; % Don't include central pixel when computing mean.
kernel = kernel / sum(kernel(:)); % Normalize. Mean is the sum of the 48 pixels divided by 48
blurredImage = conv2(double(grayImage), kernel, 'same'); % Get mean in a 7x7 window around every pixel.
output = double(grayImage) - blurredImage; % Subtract local mean.
You can do it without creating a temporary image if you construct the kernel differently:
kernel = -ones(7, 7); % Minus 1 means subtract the values.
kernel(4, 4) = 0; % Don't include central pixel when computing mean.
kernel = kernel / sum(kernel(:)); % Normalize. Mean is the sum of the 48 pixels divided by 48
kernel(4,4) = 1;
output = conv2(double(grayImage), kernel, 'same');

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 17 de Mayo de 2019
Try (for an integer gray level image):
newImage = double(yourImage) - meanValue;
where you said you already have the meanValue, or
meanValue = mean2(yourImage);
if you don't.
newImage will be a floating point image, which is the only way to handle negative values in the subtraction.

2 comentarios

Ahmad Alenezi
Ahmad Alenezi el 10 de Oct. de 2019
I think the last answer is right !
Image Analyst
Image Analyst el 12 de Oct. de 2019
Correct. And since about half the values will be negative if you subtract the mean, the accepted answer will be wrong for about half the pixels, whereas my code won't be wrong for any of them.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 17 de Mayo de 2019

Comentada:

el 11 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by