-1

I have CT scan image and I want to extract (segment) only lungs part from that using python or MATLAB.

enter image description here

codewarrior
  • 101
  • 1
  • 8

2 Answers2

1

The solution which worked for me in MATLAB is Hill Climbing algorithm. Here is it's MATLAB implementation. It uses an unsupervised machine learning algorithm: k-means. You will need to experiment with different number of segments depending on your requirement. See example below:

Download the above code (files HillClimbingSegment.m and RGB2Lab.m ) from MATLAB Central and place it in your workspace. Then you can segment like this:

image_path = 'CT.jpg';
segmented_img = HillClimbingSegment(image_path,4);

where the second argument in the second line (4) is the number of desired segments.

enter image description here

In my image, using just 4 segments, the abnormality in right lung is observable, but the other organ at top of right lung is merged with lung. Using 7 segments, seperates it out. Using 7-9 segments give the best view. Above 9 leads to clutter.

Abhi25t
  • 3,703
  • 3
  • 19
  • 32
0

I recommend you convert the png into NIfTI file (nii) and then working with python. For the conversion, you can use it: Convert .png files to .nii (NiFti files)

After that, you can load the image use

nii = nibabel.load(path to the nifti file)

get the numpy array

np_array = nii.get_fdata()

Segment all the body from the nifti file by filter threshold between -500 to 2000. All the air inside and outside the body will be 1- body segmentation.

Then take the largest connected component and remove it from the segmentation (which is the outside air that is larger than the lungs)

air_seg = skimage.measure.label(air_seg)
largest_connected_air = air_seg == numpy.argmax(nump.bincount(air_seg.flat)[1:]) + 1

do it again to take the lungs now. This what I got:

3D lungs

Please reply if you need more explanation.

avivd
  • 76
  • 1
  • 1
  • 6