/
aedes_copy_roi.m
149 lines (129 loc) · 3.58 KB
/
aedes_copy_roi.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
function ROI = aedes_copy_roi(ROI,roi_label,dir_ind,slice_ind,vol_ind,data_ind,copytype)
% AEDES_COPY_ROI - Copy ROI in X, Y, or Z direction
%
%
% Synopsis:
%
% Description:
%
% Examples:
%
% See also:
%
% This function is a part of Aedes - A graphical tool for analyzing
% medical images
%
% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
%
% Department of Physics, Department of Neurobiology
% University of Kuopio, FINLAND
%
% This program may be used under the terms of the GNU General Public
% License version 2.0 as published by the Free Software Foundation
% and appearing in the file LICENSE.TXT included in the packaging of
% this program.
%
% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
% Check input arguments
if nargin<5
error('Too few input arguments')
end
% Return immediately if ROI is an empty structure
% or if ROI is not structure type
if isempty(ROI) || ~isstruct(ROI)
return
end
%% Check if ROI is for normal or mixed type data
if length(ROI(1).voxels)>1
isMixedType = true;
else
isMixedType = false;
end
% Check if the ROI is identied as an index
% or label
if ischar(roi_label)
roi_ind = find(strcmp({ROI(:).label},roi_label));
if isempty(roi_ind)
error(['ROI labeled "' roi_label '" was not found!'])
return
end
elseif isnumeric(roi_label)
roi_ind = roi_label;
if roi_ind<1 || roi_ind>length(ROI)
error('ROI index exceeds ROI dimensions.')
return
end
else
error('Second input argument not valid!')
return
end
if isMixedType
dir_ind = 3;
% Current ROI size
currentRoiSz = size(ROI(roi_ind).voxels{data_ind});
currentRoiSlice = ROI(roi_ind).voxels{data_ind};
% Copy "current slice" of the selected ROI
for ii=1:length(ROI(roi_ind).voxels)
if any(ii==slice_ind)
% Resize the ROI slice before adding
sz = size(ROI(roi_ind).voxels{ii});
newRoiSlice = imresize(currentRoiSlice,sz,'nearest');
newRoiInd = find(newRoiSlice);
if strcmpi(copytype,'append')
ROI(roi_ind).voxels{ii}(newRoiInd)=true;
else
ROI(roi_ind).voxels{ii}(:,:)=false;
ROI(roi_ind).voxels{ii}(newRoiInd)=true;
end
end
end
else
% if dir_ind==1
% dir_ind=3;
% elseif dir_ind==3
% dir_ind=1;
% end
% Calculate the projection that is copied
roi_sz=size(ROI(roi_ind).voxels{1}(:,:,:,vol_ind));
%tmp=zeros(sz([1 2 3]~=dir_ind),'uint8');
%keyboard
if dir_ind == 4
tmp=find(ROI(roi_ind).voxels{1}(:,:,:,vol_ind)>0);
[r,c,x]=ind2sub(size(ROI(roi_ind).voxels{1}(:,:,:,vol_ind)),...
tmp);
else
[r,c]=find(squeeze(sum(ROI(roi_ind).voxels{1}(:,:,:,vol_ind),dir_ind))>0);
end
% Generate indices
lr = length(r);
r=repmat(r,length(slice_ind),1);
c=repmat(c,length(slice_ind),1);
if dir_ind==4
x=repmat(x,length(slice_ind),1);
end
slice_ind = slice_ind(:).';
slice_ind=repmat(slice_ind,lr,1);
slice_ind=slice_ind(:);
v=ones(size(slice_ind))*vol_ind;
% Copy to slices
if dir_ind==3
% Z direction
ind = sub2ind(size(ROI(roi_ind).voxels{1}),r,c,slice_ind,v);
elseif dir_ind==2
% Y direction
ind = sub2ind(size(ROI(roi_ind).voxels{1}),r,slice_ind,c,v);
elseif dir_ind==1
% X direction
ind = sub2ind(size(ROI(roi_ind).voxels{1}),slice_ind,r,c,v);
elseif dir_ind==4
% V direction
ind = sub2ind(size(ROI(roi_ind).voxels{1}),r,c,x,slice_ind);
end
if strcmpi(copytype,'append')
ROI(roi_ind).voxels{1}(ind) = true;
else
ROI(roi_ind).voxels{1}(:) = false;
ROI(roi_ind).voxels{1}(ind) = true;
end
end