-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfindBonesCT.m
61 lines (45 loc) · 1.3 KB
/
findBonesCT.m
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function mask = findBonesCT(im, units, ignore)
% Find the lungs in a CT scan using thresholding and morphology. Assumes
% that im is in Hounsfield units. Anything in 'ignore' is not treated as
% bone, e.g. other organs which were previously found.
% Depednencies
addpath(genpath('aimutil')) % for ballMask
if iscolumn(units)
units = units';
end
% Bones window
threshLo = 0; % Most of the bone should be greater than this
%threshHi = 200;
threshHi = 175; % This is mostly bone, but also some soft tissues
% Apply the high threshold
mask = im >= threshHi;
% Remove 'ignore' pixels
haveIgnore = nargin > 2 && ~isempty(ignore);
if haveIgnore
mask = mask & ~ignore;
end
% The skeleton is the largest CC
mask = largestCC(mask);
% Close, to fill gaps
closeRad = 25;
mask = closeMm(mask, units, closeRad);
% Apply the low threshold
mask = mask & (im >= threshLo);
% Fill AXIAL holes
for k = 1 : size(mask, 3)
mask(:, :, k) = imfill(mask(:, :, k), 'holes');
end
% Ignore again
if haveIgnore
mask = mask & ~ignore;
end
end
function im = closeMm(im, units, mm)
% Morphological closing by the given number of mm
% Get the width, in voxel
closeWidth = ceil(mm ./ units);
% Get a 3D ball
center = 1 + (closeWidth - 1) / 2;
ball = ballMask(closeWidth, center, mm / 2, units);
im = bwCloseN(im, ball);
end