convert images to binary with different gray level backgroung

9 views (last 30 days)
Hi all !
I have the following code:
clc;
OriginalImage = imread('te3.jpg');
subplot(2,2,1);
imshow(OriginalImage);
g = rgb2gray(OriginalImage);
subplot(2,2,2);
imshow(g);
BW = imbinarize(g);
subplot(2,2,3);
imshow(BW);
% level = multithresh(g);
% I = imquantize(g,level);
% subplot(2,2,4);
% imshow(I);
I used two images, see the RESULT below:
the second image:
..
I wonder why this worked in images with dark gray background and didn't work with light gray background !
Can you please explain to me why ! And how can I make it works in the second image ..
Thank you !

Answers (1)

Image Analyst
Image Analyst on 17 Feb 2018
It may not know what you consider to be the background. For your computer graphics images, you can just take the upper left pixel and then threshold.
backgroundGrayLevel = OriginalImage (1,1);
brightStuff = OriginalImage > backgroundGrayLevel;
darkStuff = OriginalImage < backgroundGrayLevel;
nonBackgroundStuff = OriginalImage ~= backgroundGrayLevel;

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!