Creating a binary image with rgb values

I have to genarate a binary image of an ellipse having its inside as white while outside as black.But , the problem i am facing is that whenever the foreground and background of my input ellipse is either light or dark then the binary image becomes all black or all white.

6 Comments

What function and parameter values are you using to create the binary image?
redChannel = imag(:, :, 1);
greenChannel = imag(:, :, 2);
blueChannel = imag(:, :, 3);
maxGrayLevelR = max(redChannel(:));
minGrayLevelR = min(redChannel(:));
maxGrayLevelG = max(greenChannel(:));
minGrayLevelG = min(greenChannel(:));
maxGrayLevelB = max(blueChannel(:));
minGrayLevelB = min(blueChannel(:));
%Convert percentage threshold into an actual number.
thresholdLevelR = minGrayLevelR + 0.5*(maxGrayLevelR - minGrayLevelR);
thresholdLevelG = minGrayLevelG + 0.5*(maxGrayLevelG - minGrayLevelG);
thresholdLevelB = minGrayLevelB + 0.5*(maxGrayLevelB - minGrayLevelB);
image_binary = redChannel > (thresholdLevelR,thresholdLevelG,thresholdLevelB)/3;
image_binary = greenChannel > max(thresholdLevelR,thresholdLevelG,thresholdLevelB)/3;
image_binary = blueChannel > max(thresholdLevelR,thresholdLevelG,thresholdLevelB)/3;
But ,it is not separating light background on light foregorund.
Have you tried imbinarize()?
Explore the various methods and optional parameters.
this function works very well.But, i specifically want the foreground of an image to be white and background to be black.So, i am using not(image) function. Is there any way by which i can know if the center pixel of the image is black or white. If it is white then i wont use not function .Otherwise,i will use not function.
Adam Danz
Adam Danz on 15 Apr 2020
Edited: Adam Danz on 17 Apr 2020
See my answer.

Sign in to comment.

Answers (1)

Use imbinarize(). The output can be used to determine whether the center pixel is black (0) or white (1).
BW = imbinarize(. . .)
centerPixelValue = BW(round(size(BW,1)/2),(round(size(BW,2)/2)));

Products

Release

R2020a

Tags

Asked:

on 15 Apr 2020

Edited:

on 17 Apr 2020

Community Treasure Hunt

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

Start Hunting!