How to change white on the outside to black?

 Accepted Answer

Some of the objects are connected to the surrounding white blob. You'll need to deal with that. You might be able to do something like:
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1004025/image.png');
A = rgb2gray(A)>127; % binarize
B = imopen(A,ones(3)); % try to break connectivity
B = imclearborder(B); % clear border
B = A & ~B; % difference of original and smoothed copy sans border
B = bwareafilt(B,1); % select only the border object, delete speckles
B = A & ~B; % remove border object from original image
imshow(B)

4 Comments

Error in Code (line 10)
A = rgb2gray(BW)>127; % binarize
Thank you for your answer, but i have some difficulty to combine it with my code, do you have any solution?
Error using rgb2gray>parse_inputs (line 79)
MAP must be a m x 3 array. Use im2gray for RGB and grayscale images.
Error in rgb2gray (line 51)
isRGB = parse_inputs(X);
Error in Code (line 15)
A = rgb2gray(L)>127; % binarize
this is the full error that i get!
i already deal with that error, Thank you so much
As you've probably already figured out, your working image is probably still a single-channel logical image, whereas the attached PNG file is a uint8-scale RGB image. In that case, the conversion and thresholding isn't necessary unless dealing with a copy like the one attached.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 May 2022

Commented:

DGM
on 19 May 2022

Community Treasure Hunt

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

Start Hunting!