-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMJSeg.m
160 lines (102 loc) · 3.6 KB
/
TMJSeg.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
% author : serge brosset
% condyle segmentation of small FOV CBCT scans
function TMJSeg(filename)
display(filename);
volume = niftiread(filename);
S = size(volume);
info = niftiinfo(filename);
info.Datatype = 'int8';
info.BitsPerPixel = 8;
info.Filemoddate = {};
% median filtering + guided filter / denoisiong
Imedian = medfilt3(volume,[7 7 7]);
Ifiltered = imguidedfilter(Imedian);
% delete value out of image (< 0)
Ifiltered(Ifiltered<0) = 0;
% contrast adjustement
Idb = im2double(Ifiltered);
M = max(Idb(:));
m = min(Idb(:));
Iadjust = (imadjustn(Idb,[m M],[0 1]));
% get contour of the shape
Iedge = edge3(Iadjust,"approxcanny",graythresh(Iadjust));
% remove small objects
BW = bwareaopen(Iedge,100000);
% remove all components but the condyle
CC = bwconncomp(BW);numPixels = cellfun(@numel,CC.PixelIdxList);numOb = CC.NumObjects;
% check for cond/bones without separations
if numOb < 2
sigma = 0.1;
Iedge = edge3(Iadjust,"approxcanny",graythresh(Iadjust),sigma);
BW = bwareaopen(Iedge,100000);
CC = bwconncomp(BW);numPixels = cellfun(@numel,CC.PixelIdxList);numOb = CC.NumObjects;
while numOb < 2
sigma = sigma + 0.5;
Iedge = edge3(Iadjust,"approxcanny",graythresh(Iadjust),sigma);
BW = bwareaopen(Iedge,100000);
CC = bwconncomp(BW);numPixels = cellfun(@numel,CC.PixelIdxList);numOb = CC.NumObjects;
% break while condition
if sigma > 10
break;
disp('error');
end
end
[~,idMax] = max(numPixels);
BW(CC.PixelIdxList{idMax}) = 0;
else
[~,idMax] = max(numPixels);
BW(CC.PixelIdxList{idMax}) = 0;
CC = bwconncomp(BW);numPixels = cellfun(@numel,CC.PixelIdxList);
while CC.NumObjects > 2
[~,idMax] = max(numPixels);
BW(CC.PixelIdxList{idMax}) = 0;
CC = bwconncomp(BW);
numPixels = cellfun(@numel,CC.PixelIdxList);
end
CC = bwconncomp(BW);numPixels = cellfun(@numel,CC.PixelIdxList);
end
condyle = BW;
% 2D convex uhull to fill the shape
newImg1 = zeros(S);
newImg2 = zeros(S);
newImg3 = zeros(S);
for j = 1:S(3)
ch = bwconvhull(condyle(:,:,j),'objects');
newImg1(:,:,j) = ch;
end
for j = 1:S(2)
ch = bwconvhull(squeeze(condyle(:,j,:)),'objects');
newImg2(:,j,:) = ch;
end
for j = 1:S(1)
ch = bwconvhull(squeeze(condyle(j,:,:)),'objects');
newImg3(j,:,:) = ch;
end
CHe = newImg1 & newImg2 & newImg3;
shape = CHe;
% active contour to get more accurate contour
SE = strel("sphere",11);
Iclose = imclose(shape,SE);
Ifill = imfill(Iclose,'holes');
I = niftiread(filename);
I = imguidedfilter(I);
I16 = uint16(I);
Id = im2double(I16);
% 30 -> 20 iterations
Iac = activecontour(Id,Ifill,20);
Iac = bwareaopen(Iac,10000); % remove small pixels alone
% save output
% read format : name.nii / name.nii.gz
[filepath,name,ext] = fileparts(filename);
name = strcat(name,ext);
filenamesplit = split(name, '.');
nameChar = cell2mat(filenamesplit(1));
seg_name = strcat(nameChar, '_seg', '.', cell2mat(filenamesplit(2)));
seg_fullname = strcat(filepath, '/', seg_name);
info.Filename = seg_name;
niftiwrite(int8(Iac),seg_fullname,info);
if (contains(filename, '.gz'))
gzip(seg_fullname);
delete(seg_fullname);
end
end