segmenting the lung region

i have converted the CXR to binary image, my aim is to obtain only those lung region,
i have used the same code as lung segmentation.m posted by @Image Analyst which was off great help, i would like further asisst in extracting only the lung region.
Any help is appreciated. Thankyou

2 Comments

Did you try function imclearborder?
l=imread('E:\project\dataset\CHNCXR_0042_0.png');
k=imresize(l,[256,256]);
p=imnoise(k,'salt & pepper',0.04);
h=medfilt2(p);
d=adapthisteq(h);
e=imbinarize(d);
f = imclearborder(e);
f = bwareafilt(e,5);
f = ~bwareaopen(~f, 1000);
i have used this code..

Sign in to comment.

 Accepted Answer

Try
mask = imclearborder(mask); % Get rid of blobs touching the edge of the image.
mask = bwareafilt(mask, 2); % Take the largest 2 of the remaining blobs.

5 Comments

Thankyou.
I have included this snippet in my code.
But when i give 2 for bwareafilt, i did not acquire the desired results.
l=imread('E:\project\dataset\CHNCXR_0042_0.png');
k=imresize(l,[256,256]);
p=imnoise(k,'salt & pepper',0.04);
h=medfilt2(p);
d=adapthisteq(h);
e=imbinarize(d);
f = imclearborder(e);
f = bwareafilt(e,2);
f = ~bwareaopen(~f, 1000);
N=edge(f,'canny');
figure,subplot(1,1,1),imshow(k),title('resized image');
figure,subplot(1,1,1),imshowpair(p,h,'montage');
figure,subplot(2,2,1),imshow(k),title('original image'),subplot(2,2,2),imhist(k);
subplot(2,2,3),imshow(h),title('filtered image'),subplot(2,2,4),imhist(h);
subplot(1,1,1),imshow(d),title('adaptive histogram equalization');
subplot(1,1,1),imshow(e),title('binarized');
subplot(1,1,1),imshow(f),title('removal of extra blobs');
subplot(1,1,1),imshow(BW),title('edge detected');
It works. You were just doing a whole bunch of unnecessary stuff like adding noise, histogram equalization, edge detection, etc. Don't do that and just do what I say on your binary Image:
folder = pwd; % 'E:\project\dataset'
fullFileName = fullfile(folder, 'CHNCXR_0042_0.jpg');
rgbImage=imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage);
axis('on', 'image')
title('Original RGB image')
% Convert to gray scale.
if numberOfColorChannels == 3
grayImage = rgb2gray(rgbImage);
else
% It's already gray scale.
grayImage = rgbImage;
end
% Resize.
grayImage = imresize(grayImage,[256,256]);
subplot(2, 2, 2);
imshow(grayImage);
axis('on', 'image')
title('Resized gray scale image')
% Binarize
binaryImage = grayImage > 128;
subplot(2, 2, 3);
imshow(binaryImage);
binaryImage = imclearborder(binaryImage);
binaryImage = bwareafilt(binaryImage,2);
subplot(2, 2, 3);
imshow(binaryImage);
axis('on', 'image')
title('Lungs Only')
I don't even think the resizing is needed. Why are you doing that?
As i am new to matlab, i was trying those commands sir.
Thankyou so much for the help, i tried the same code, it was off great help.
i tried the same code for a bunch of images, it did work for few , for rest it dint,
I have attached few input images for which i dint get the output, and a screenshot of output images in output folder.jpg. Can you please help me with the changes to be done.
Thankyou sir for the reference.

Sign in to comment.

More Answers (1)

rgbImage = imread('https://in.mathworks.com/matlabcentral/answers/uploaded_files/858110/image.jpg') ;
% Removing the extra white background around the image (credit to Image
% Analyst)
grayImage = min(rgbImage, [], 3);
binaryImage = grayImage < 200;
binaryImage = bwareafilt(binaryImage, 1);
[rows, columns] = find(binaryImage);
row1 = min(rows);
row2 = max(rows);
col1 = min(columns);
col2 = max(columns);
% Crop
croppedImage = rgbImage(row1:row2, col1:col2, :);
BW = imbinarize(rgb2gray(croppedImage)) ;
[B,L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end

1 Comment

hi, Thankyou for the response, this was of great help.
One more clarification,
I need the output to be in binary form i.e ROI.
I have attached a reference image named as ROI.png.

Sign in to comment.

Categories

Find more on Medical Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!