How do I remove a white background and only keep

Illustration
Justin Johnson - 2022-04-30T13:53:44+00:00
Question: How do I remove a white background and only keep

How do I remove a white background and only keep certain objects in a binary image on MATLAB? I would like the remove the white pixels in the background, but somehow keep the white lines. How would I go about doing that. Sorry I didn't mean to remove the question. I was trying to comment on my phone, but I must have unknowingly modified the question.  

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

To remove white backgroud you need to follow this:

%===============================================================================
baseFileName = 'concrete_inverted.webp';
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);

%===============================================================================
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
  grayImage = rgb2gray(grayImage);
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;

mask = grayImage > 128;
mask(1:2, :) = false;
subplot(2, 2, 2);
imshow(mask);
axis on;
axis image;
title('Binary Image Mask', 'fontSize', fontSize);
drawnow;

% Get rid of small blobs.
mask = bwareaopen(mask, 500);
% Label the image.
labeledImage = bwlabel(mask);
% Find the areas and perimeters
props = regionprops(labeledImage, 'Area', 'Perimeter');
allAreas = [props.Area];
sortedAreas = sort(allAreas, 'descend')
allPerimeters = [props.Perimeter];
% Compute circularities
circularities = allPerimeters .^ 2 ./ (4 * pi * allAreas)
sortedC = sort(circularities, 'descend')
% Keep conly blobs that are nowhere close to circular or compact.
minAllowableCircularity = 10;
keeperIndexes = find(circularities >= minAllowableCircularity);
mask = ismember(labeledImage, keeperIndexes);

% Display the mask image.
subplot(2, 2, 3);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Intermediate Cleaned Mask', 'FontSize', fontSize);
drawnow;

% Get rid of black islands (holes) in struts without filling large black areas.
subplot(2, 2, 4);
mask = ~bwareaopen(~mask, 1000);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Final Cleaned Mask', 'FontSize', fontSize);
drawnow;
I think the results look pretty good, like what I think you're expecting. Don't you? If not, let me know what I could do to improve it. Basically the same original crack lines are in the image (no change in exterior shape), except that the small black islands in them are filled in, all all the little isolated white island are removed. Is that what you want? There are a few parameters/variables in there, with obvious names, that you can adjust if you want to.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!