how to detect background color of image ?
Afficher commentaires plus anciens
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 commentaire
MJ Thangaraj
le 23 Avr 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 .
Réponse acceptée
Plus de réponses (2)
Walter Roberson
le 23 Avr 2016
0 votes
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.
1 commentaire
ElizabethR
le 24 Avr 2016
Luis Rosety
le 12 Mai 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 commentaire
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.
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
