Remove Black pixels and averaging the pixels

3 visualizaciones (últimos 30 días)
VINOTHINI S
VINOTHINI S el 12 de En. de 2022
Comentada: VINOTHINI S el 13 de En. de 2022
Hello,
I have image as like below.
I want to remove the black pixels in the background and find the average value for only flower containing part.
How to remove the black pixels in all the 3 channels and do the averaging?
Thank you.
  2 comentarios
Matt J
Matt J el 12 de En. de 2022
Editada: Matt J el 12 de En. de 2022
What do you mean by the average value? Doesn't each pixel have 3 values (R,G,B)? Do you want the different color channels averaged across each other as well?
VINOTHINI S
VINOTHINI S el 13 de En. de 2022
I want to remove black pixel only in all 3 channels(R, G, B) and take mean of R, G, and B channel. if we remove the black pixels, the matrix in all channels is not n*n. in that case, how to store it?

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 12 de En. de 2022
This will average the non-black pixel values in each channel independently:
data = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/861080/image.png');
[m,n,p] = size(data);
idx = find(~all(data == 0,3));
val = NaN(1,p);
for i = 1:p
val(i) = mean(data(idx+(i-1)*m*n));
end
display(val);
val = 1×3
180.0183 150.4229 29.7907
If you want to replace the non-black pixels with their average color calculated this way, you could do this:
new_data = data;
for i = 1:p
new_data(idx+(i-1)*m*n) = val(i);
end
figure();
subplot(2,1,1);
imshow(data);
subplot(2,1,2);
imshow(new_data);
If you need to strictly distinguish between the background and the foreground of the image (as opposed to here, where I'm going by whether or not the pixel is black only), I'm sure there are functions in the Image Processing Toolbox you can use.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by