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 个评论

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.
See my answer.

请先登录,再进行评论。

回答(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)));

类别

帮助中心File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

产品

版本

R2020a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by