how to detect background color of image ?

조회 수: 2 (최근 30일)
ElizabethR
ElizabethR 2016년 4월 22일
편집: DGM 2022년 5월 12일
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
MJ Thangaraj
MJ Thangaraj 2016년 4월 23일
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 .

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2016년 4월 23일
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
Image Analyst
Image Analyst 2016년 4월 24일
OK, then this should do it
bw = ~bw; % Change black into white and white into black.
ElizabethR
ElizabethR 2016년 4월 24일
okay .. Thank You so Much Image Analyst. God Bless ^^

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2016년 4월 23일
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
ElizabethR
ElizabethR 2016년 4월 24일
thanks for answare my question Walter ^^

댓글을 달려면 로그인하십시오.


Luis Rosety
Luis Rosety 2022년 5월 12일
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
DGM
DGM 2022년 5월 12일
편집: DGM 2022년 5월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by