Remove Black Background from segmented binary image

how can i remove the black background after i segmented the lung ct image i need to save only lungs in another directory

2 commentaires

DGM
DGM le 18 Mai 2021
Modifié(e) : DGM le 18 Mai 2021
Are you asking how to remove the black part of the binary image? What do you intend to replace it with?
Or are you asking how to apply the binary image to remove the background from the source image?
I want to remove the black background and replace the black pixels with NaN Or To Extract only the lungs to the different place and save it on another png file

Connectez-vous pour commenter.

 Réponse acceptée

DGM
DGM le 19 Mai 2021
Modifié(e) : DGM le 19 Mai 2021
I'm still not sure what you're after or how you intend to use the results, so I'll just try a few things. I'm going to use an example image and mask:
A = imread('coins.png'); % this is a grayscale (I) image
m = imread('coinmask.tif'); % this is a precalculated logical mask
You could replace the BG of either the source or the mask (you haven't really been clear about which)
% replace mask BG with NaN
m_nan = im2single(m);
m_nan(~m) = NaN;
% replace source BG with NaN
A_nan = im2single(A);
A_nan(~m) = NaN;
These would have to be saved in a file format that supports floating point data. Most image formats don't, but you could use a TIFF container with some effort. The easiest way would simply be to use a .mat file. Image readers wouldn't know what to do with a float image full of NaNs anyway.
Alternatively, you could add an alpha channel. This might be visually useful, but probably cumbersome and unnecessary for processing purposes.
% add an alpha channel
alpha = im2uint8(m);
% save the mask with alpha
imwrite(alpha,'m_alpha.png','alpha',alpha)
% save the source with alpha
imwrite(A,'A_alpha.png','alpha',alpha)
There are two images there. The one is just invisible because it's white on a white page background. Right click on it to see.
In either case, imshow() isn't going to know what to do with the image. NaNs will be rendered as black, and imshow() doesn't support alpha. Any utility for visualization is moot unless you're using other tools for viewing the images.
Since I'm guessing here, a common thing people do is apply the mask to the image.
% apply mask to the source
A_masked = A;
A_masked(~m) = 0;
imwrite(A_masked,'A_masked.png')

Plus de réponses (1)

Images must remain rectangular so you can't "remove" the black pixels and still have an image. There has to be something there. If you want, you could crop the image, but I don't see any use in that, other than saving an insignificant amount of disk space. You can replace the 0's with nans if you want
binaryImage = double(binaryImage); % Change from logical to floating point.
binaryImage(binaryImage == 0) = nan;
Not sure what use there is for that either. What do you really want to do? Why do you think you want to do this? So let's say you could remove 0's -- what would you do with the remaining output of all 1's?

2 commentaires

I want to be able to detect the amount of Covid-19 involvement on the lungs by removing the background
I don't think that is necessary. All you need is the mask saying what is lungs and what is not. Why do you think you need to erase/blacken part of the original image? You don't really need to unless you just want to see it for curiosity.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by