[b4b313]: / Image Segmentation / Image Utility Functions / extraction / removeDisjointStructures.m

Download this file

37 lines (26 with data), 1.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function [ image ] = removeDisjointStructures( image )
%removeDisjointStructures
%Otsu thresholding may not always provide a single structure that would
%represent a nodule, this function will attempt to remove additional
%structures.
%
% param Image
% Must be an image that has had otsu segmentation done on it.
% return fixedImage
% an image with a single class to be used for segmentation
%find all connected components
CC = bwconncomp(image);
%Count the number of pixels belonging to each component
numPixels = cellfun(@numel,CC.PixelIdxList);
%find the maximum area connected component, provides index and count
[biggest,idx] = max(numPixels);
%remove all other connected components that are not the largest
%component
for i=1:CC.NumObjects
if(i == idx)
continue;
end
%erase smaller components
image(CC.PixelIdxList{i}) = 0;
end
end