-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP2_prepareMasks.m
90 lines (68 loc) · 2.5 KB
/
P2_prepareMasks.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
%% create image tiles for the training of the random forest (ilastik)
clc
pathIm = 'D:\proj_PNN-Atlas\DATASET';
pathOut = 'D:\proj_PNN-Atlas\mask_training';
numSamples = 120;
cropSize = [300,300];
l = dir([pathIm filesep '**']);
names = string({l.name})';
folds = string({l.folder})';
paths = folds+filesep+names;
match = regexp(paths, '.*-thumb\.png$', 'match');
lf = paths(~cellfun(@isempty, match));
% lf = listfiles(pathIm, '.png');
selIm = randsample(length(lf), numSamples);
for i = 1:length(selIm)
im = imread(lf{selIm(i)});
if any(size(im,[1,2])<cropSize)
continue
end
imSmall = cropImg(im, cropSize);
imOutName = [pathOut filesep 'crop' sprintf('%02d', i) '.png']; %Leo will definitely like this line
imwrite(imSmall, imOutName);
end
%% create masks with ilastik classifier
% open ilastik project and perform training and batch processing of the
% images in the thumbnails folder
% NB. do not move the ilastik project once it has been saved!
%% rename ilastik ouput files
% rename ilastik output and convert segmented images into bitmap images
% where background and foreground are 0 and 1, respectively
% NB. files will be overwritten if deleteSegmentation is set to true
datasetPath = 'D:\proj_PNN-Atlas\DATASET';
mouse = 'CC6A';
deleteSegmentation = true;
maskPath = [datasetPath filesep mouse filesep 'masks'];
[imgsP, imgsN, ~ ] = listfiles(maskPath, '.png');
if any(contains(imgsP,'-thumb-mask') == 0)
error('Masks may be already pre-processed!')
end
for i = 1:size(imgsP,2)
mask = imread(imgsP{i});
mask = mask ==1;
fileN = erase(imgsN{i}, '-thumb');
newFileN = [maskPath filesep fileN];
imwrite(mask, newFileN);
if deleteSegmentation
delete(imgsP{i});
end
end
%% edit masks
% masks can be manually edited with batchEditSliceMask script after the
% info xml file has been created!
%--------------------------------------------------------------------------
%%
function crop = cropImg(bigIm, tileSize)
% crop = cropImg(bigIm, tileSize)
%bigIm : image (matrix) from which the sample is taken
%tileSize : 1x2 array specifying dimensions of the crop (rounded for even numbers)
% arguments
% bigIm uint8
% tileSize (1,2) uint8 = [512,512];
% end
tileX = ceil(tileSize(1)/2);
tileY = ceil(tileSize(2)/2);
cropCenterX = randsample([tileX : (size(bigIm,1)-tileX)],1);
cropCenterY = randsample([tileY : (size(bigIm,2)-tileY)],1);
crop = bigIm((cropCenterX-tileX+1):(cropCenterX+tileX-1),(cropCenterY-tileY+1):(cropCenterY+tileY-1),:);
end