MATLAB: How to remove the background from this binary image

image processingImage Processing ToolboxlungMATLAB

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.

Best Answer

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;