How do I remove the background from this binary image?

85 views (last 30 days)
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;
Image Analyst
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:

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.
% 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);
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.
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% 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);
% 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);
% 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);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by