How do I remove the background from this binary image?

30 views (last 30 days)
Hello,
I am working on the lung segmentation, I got the output after thresholding which is shown in figure. But my output is not correct, I am interested in the lung region only. I want to remove the white background from the image, such that my output should contain the lung region represented in the white region on the black background.
Thanks in advance.

Accepted Answer

Alessandro Masullo
Alessandro Masullo on 20 May 2016
If you found your mask for your object, you can simply remove the background by using:
image % your image
mask % your background
new_image = image;
new_image(~mask) = 0;
  2 Comments
Image Analyst
Image Analyst on 11 Nov 2016
Edited: Image Analyst on 11 Nov 2016
harshal did not include the original gray scale image so not really much can be done. If you want, you can start your own question and attach your own image. However, I'm pretty sure I've done lung segmentation, with code and images, at least once before so you might want to search the Answers forum. For example here: http://www.mathworks.com/matlabcentral/answers/154027-how-can-imfill-an-image-with-a-lot-of-edge#comment_236068

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 11 Nov 2016
Try the attached.
% Program to find the two lung regions in a CT cross sectional image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%===============================================================================
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in the lungs gray scale demo image.
folder = pwd;
baseFileName = 'lungs_CT.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
drawnow;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
%===============================================================================
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
% The first and last bin of pixelCount are so huge that it suppresses the height
% of the rest of the histogram when plotted. Zero out these bins so we can see the other bins.
pixelCount(1) = 0;
pixelCount(end) = 0;
subplot(2, 3, 2);
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
%===============================================================================
% Threshold (binarize) the image.
thresholdValue = 45000;
% Draw a red line on the histogram at this value.
line([thresholdValue, thresholdValue], ylim, 'LineWidth', 2, 'Color', 'r');
% Label the regions for the two body zones.
text(22000, 7500, 'Lungs', 'Color', 'r', 'FontSize', 20, 'FontWeight', 'bold');
text(51000, 7500, 'Body', 'Color', 'r', 'FontSize', 20, 'FontWeight', 'bold');
binaryImage = grayImage < 45000; % Do the thresholding.
% Display the binary image.
subplot(2, 3, 3);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
drawnow;
% Get rid of stuff touching the border
binaryImage = imclearborder(binaryImage);
% Extract only the two largest blobs.
binaryImage = bwareafilt(binaryImage, 2);
% Fill holes in the blobs to make them solid.
binaryImage = imfill(binaryImage, 'holes');
% Display the binary image.
subplot(2, 3, 4);
imshow(binaryImage, []);
axis on;
title('Lungs-Only Binary Image', 'FontSize', fontSize);
drawnow;
% Mask image with lungs-only mask.
% This will produce a gray scale image in the lungs and black everywhere else.
maskedImage = grayImage; % Initialize
maskedImage(~binaryImage) = 0;
% Display the masked gray scale image of only the lungs.
subplot(2, 3, 5);
imshow(maskedImage, []);
axis on;
title('Masked Lungs-Only Image', 'FontSize', fontSize);
  10 Comments
Image Analyst
Image Analyst on 5 Jul 2021
Yes, but do it on grayImage, not grayLevels.
binaryImage = grayImage < 164 & grayImage>130 ; % Do the thresholding.
Tthat will work to select only pixels with a gray level in the range 131 to 163, inclusive.

Sign in to comment.


muzaiyanah ahmad supian
muzaiyanah ahmad supian on 22 Feb 2019
the output as per attached.

Community Treasure Hunt

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

Start Hunting!