how to detect background color of image ?

i want to make function, so if the background color if black like image 1, it will change to white.
But, if the background color image is white like in image 2, so it doesn't change.
How to make it ?? thanks

1 comentario

MJ Thangaraj
MJ Thangaraj el 23 de Abr. de 2016
The image is Binary so it's obviously going to have only White and BLACK values.Check whether the background in white and then Complement the image .

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 23 de Abr. de 2016
If you can make the assumption that the majority of the pixels in the image are the background color, then
[rows, columns] = size(binaryImage);
numWhitePixels = sum(binaryImage);
numBlackPixels = rows * columns - numWhitePixels;
if numWhitePixels > numBlackPixels
% Background is white.
% Do nothing at all.
else
% Background is black.
% Make image all white, everywhere at every pixel.
binaryImage(:) = true;
end

5 comentarios

ElizabethR
ElizabethR el 23 de Abr. de 2016
thanks for answare my question. i try it but, the output is only image withh all of white pixel
Image Analyst
Image Analyst el 23 de Abr. de 2016
Yes, exactly. Because you said "background color if black like image 1, it will change to white." So if the background is black, and it changes to white, and if the foreground is white and it stays white, then of course your whole image is white, exactly as you specified.
If you want something different than that, then say exactly what you want. Like if you want the black background to become white and want the white foreground to become white then say you want to invert the image, like bw=~bw.
ElizabethR
ElizabethR el 24 de Abr. de 2016
thanks for answare, yes you are right, thanks for you explanation. Sorry, because my english is not good enough. I mean that i have image like image one, then the background will change into black and the foreground will change into white. And if the image is like image 2, then the backgroun and the foreground will not change. hanks you ^^
OK, then this should do it
bw = ~bw; % Change black into white and white into black.
ElizabethR
ElizabethR el 24 de Abr. de 2016
okay .. Thank You so Much Image Analyst. God Bless ^^

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 23 de Abr. de 2016

0 votos

Foreground and background are matters of intent. For example, often binary images are white for the parts that contain the information of interest, but binary images might be representing text and text is often represented in black (corresponding to books, which use dark ink on a white page.) Chess diagrams often use both black and white for the pieces. It is therefore not possible to detect which color is the "foreground" and which color is the "background" by computer program alone.
Luis Rosety
Luis Rosety el 12 de Mayo de 2022
This is a very old question but I am learning Matlab and I got the same problem and just in case anybody else has the same question, I contribute with my own solution.
I realized it was quite straightforward the answer.
Assuming the input image is IM:
if(size(find(IM),1) > size(find(~IM),1))
% IM is white background
else
% IM is black background
end

1 comentario

DGM
DGM el 12 de Mayo de 2022
Editada: DGM el 12 de Mayo de 2022
Or much faster:
nz = nnz(IM);
if nz > (numel(IM)-nz)
% IM is white background
else
% IM is black background
end
You might also be able to consider the dominant value around the image periphery as some indicator of "background". I agree with Walter that the general solution requires knowledge of content and intent.

Iniciar sesión para comentar.

Preguntada:

el 22 de Abr. de 2016

Editada:

DGM
el 12 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by