How to segment lung from 2D images

Good day,
I have multiple 2D images from which I would like to segment the lung field from each. I successfully developed a code to do that, but it does not work for all the images as the contrast slightly vary from one image to the other (Image1 and 2 attached). I try using ‘imhistmatch’ to match the contrast between the images but without success. Please see my code below and the results obtained in fig.1. Could someone please help me modify the code to suit this purpose or suggest a better method to achieve this?
clear;clc;close all
% Ia=imread('image1.jpg');
load image1
II = adapthisteq(Ia,'clipLimit',0.02,'Distribution','rayleigh');
subplot(2,4,2);imshow(II);title('Adjusted contrast')
II(II>35000)=0;
II = bwareaopen(II,50); %remove connected components
mask = bwareafilt(II, 2); %There are small noise blobs. Extract the two largest objects.
z=ImageAnalyst(mask,-1); %Extract the 2nd largest objects.
subplot(2,4,1);imshow(Ia);title('Original image1')
subplot(2,4,3);imshow(z);title('segmented lung')
subplot(2,4,4);imshowpair(z,Ia);title('Image1/lung pair')
%%
% image2
% Ib=imread('image2.jpg');
load image2
I=imhistmatch(Ib, Ia);% adjust the histogram of 2D image to macth histogram of image1 image
I = adapthisteq(Ib,'clipLimit',0.02,'Distribution','rayleigh');
subplot(2,4,6);imshow(I);title('Adjusted contrast')
I(I>35000)=0;
I = bwareaopen(I,50); %remove connected components
mask = bwareafilt(I, 2); %There are small noise blobs. Extract the two largest objects.
z=ImageAnalyst(mask,-1); %Extract the 2nd largest objects ising ImageAnalyst funtion
subplot(2,4,5);imshow(Ib);title('Original image2')
subplot(2,4,7);imshow(z);title('segmented lung')
subplot(2,4,8);imshowpair(z,Ib);title('Image2/lung pair')

3 Comments

darova
darova on 20 Feb 2021
Edited: darova on 20 Feb 2021
Please use this button for code inserting
More @AUWAL ABUBAKAR Are the images properly attached?
Thanks for your response @KALYAN ACHARJYA and @darova. There was an error attaching the images. I have attached the variables now. Thanks

Sign in to comment.

Answers (1)

Hi,
Use the Image Segmenter App to segment the lungs from the images and generate binary mask easily. You can import the original image and export the mask directly to MATLAB Workspace.

Products

Release

R2020b

Asked:

on 20 Feb 2021

Community Treasure Hunt

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

Start Hunting!