-
Notifications
You must be signed in to change notification settings - Fork 25
/
roiextract.m
66 lines (66 loc) · 2.27 KB
/
roiextract.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
function [ flag ] = roiextract(trainpath, scale )
%ROIEXTRACT Summary of this function goes here
% Detailed explanation goes here
filedir = dir([trainpath '*massori.mat']);
for i = 1 : length(filedir)
fn = filedir(i).name(1:end-11);
im = imread([trainpath 'im+mask\' fn 'im+mask.jpeg']);
mask = load([trainpath fn 'massori.mat']);
mask = mask.tmptmp;
[minx, miny, maxx, maxy] = rectbox(mask); %%% x is col, y is row
% maskroi = mask(miny:maxy, minx:maxx);
% imwrite(maskroi, [trainpath fn 'maskroi.jpeg']);
mask = uint8(zeros(size(im,1), size(im,2)));
centerx = (minx+maxx) / 2;
centery = (miny+maxy) / 2;
xr = maxx - centerx;
yr = maxy - centery;
xr = xr * scale;
yr = yr * scale;
minx = int32(centerx-xr);
miny = int32(centery-yr);
maxx = int32(centerx+xr);
maxy = int32(centery+yr);
if minx <= 0
minx = 1;
maxx = int32(2 * xr + 1);
end
if miny <= 0
miny = 1;
maxy = int32(2 * yr + 1);
end
if maxx > size(im,2)
maxx = size(im,2);
minx = int32(size(im,2) - 2 * xr);
end
if maxy > size(im,1)
maxy = size(im,1);
miny = int32(size(im,1) - 2 * yr);
end
massgt = load([trainpath fn 'mass.mat']);
massgt = massgt.tmptmp;
massgt = massgt(miny:maxy, minx:maxx);
imwrite(massgt, [trainpath fn 'massgt.jpeg']);
save([trainpath fn 'massgt.mat'], 'massgt');
mask(miny, minx:maxx) = 255;
mask(maxy, minx:maxx) = 255;
mask(miny:maxy, minx) = 255;
mask(miny:maxy, maxx) = 255;
mask = imdilate(mask, strel('disk',10));
im(mask==255) = 255;
imwrite(im, [trainpath fn 'im+mask+roi.jpeg']);
dcmim = dicomread([trainpath fn(1:end-1) '.dcm']);
roiim = dcmim(miny:maxy, minx:maxx);
save([trainpath fn 'roi.mat'], 'roiim');
im = imread([trainpath fn(1:end-1) '.jpeg']);
roiim = im(miny:maxy, minx:maxx);
imwrite(roiim, [trainpath fn 'roi.jpeg']);
end
mkdir([trainpath 'im+mask+roi\']);
movefile([trainpath '*im+mask+roi.jpeg'], [trainpath 'im+mask+roi\']);
mkdir([trainpath 'massgt\']);
movefile([trainpath '*massgt.jpeg'], [trainpath 'massgt\']);
mkdir([trainpath 'roi\']);
movefile([trainpath '*roi.jpeg'], [trainpath 'roi\']);
flag = 1;
end