-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumor_extraction.m
49 lines (37 loc) · 1.32 KB
/
tumor_extraction.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
function [binary_mask, tumor] = tumor_extraction(img, brain, varargin)
%TUMOR_EXTRACTION Summary of this function goes here
% Detailed explanation goes here
verbose = false;
if ~isempty(varargin)
verbose = varargin{1};
end
% thresholding
thr = 125;
binary = brain > thr;
% morphological operations
se = strel('diamond', 3);
binary_eroded = imerode(binary, se);
CC = bwconncomp(binary_eroded);
numPixels = cellfun(@numel, CC.PixelIdxList);
[~, idx] = max(numPixels);
binary_comp = false(size(binary_eroded));
binary_comp(CC.PixelIdxList{idx}) = true;
binary_dilated = imdilate(binary_comp, se);
binary_mask = imfill(binary_dilated, "holes");
% brain extraction
tumor = double(brain) .* double(binary_mask);
tumor = uint8(tumor);
% output
if (verbose == true)
figure('Units','normalized','OuterPosition',[0.1 0.1 0.8 0.8], ...
'Name','TUMOR EXTRACTION');
subplot(2,4,1), imshow(brain, []), title('brain')
subplot(2,4,2), imshow(binary), title('thresholding')
subplot(2,4,3), imshow(binary_eroded), title('erosion')
subplot(2,4,4), imshow(binary_comp), title('bwconncomp')
subplot(2,4,5), imshow(binary_dilated), title('dilation')
subplot(2,4,6), imshow(binary_mask), title('filling holes')
subplot(2,4,7), imshow(tumor), title('tumor')
subplot(2,4,8), imshow(img, []), title('original')
end
end