-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathROI_picker.m
189 lines (154 loc) · 6.93 KB
/
ROI_picker.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
function ROI_picker( directoryname,mov_prd,prct_frms,append_str)
%% ROI_picker
% updated BPI 8/12/16
% This function allows a user to pick an ROI in a series of movies and
% write a new tif stack movie of just that ROI.
% directoryname is the name of the directory where the movies will be
% selected, if there is an error finding the directory the program will
% open uigetfile in the current working directory
%
% mov_prd is the period of movies that you will be prompted to
% choose/check the ROI of, e.g., if you choose 7 movies and mov_prd=3 then
% you will be shown movies 1,4,7. If you are choosing only one movie this
% input doesn't matter
%
% prct_frms is the percentage of frames that will be used when averaging.
% If you want to use the entire movie simply set prct_frms to 100. Default
% is 10
%
% append_str is the string which will be appended to the input movie name.
% Default is '_ROI'
%
% the function will also write a .txt file called filename_ROI_info.txt
% with the ROI coordinates.
%
%%%% optional use to specify ROI %%%%
% if you have the coordinates of the ROI that you want, you can input those
% instead of going through the clicking process. The way to do this is to
% input the coordinates in the place of mov_prd. The coordinates are input
% as [ row # right side , col # top ; row # left side , col # bottom]
%%%% Dependencies %%%%
% TIFFStack
% saveastiff
% Copyright (C) 2017 Benjamin P Isaacoff
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
if nargin<3; prct_frms=10;end
if nargin<4; append_str='_ROI';end
%% Select the movies
display('Select the movies.')
try
[datalist,dataloc,findex]=uigetfile([directoryname filesep '*.tif*'],'multiselect','on');
catch
curdir=pwd;
[datalist,dataloc,findex]=uigetfile([curdir filesep '*.tif*'],'multiselect','on');
end
if findex==0
fprintf('no data selected\n')
return
end
if ~iscell(datalist); datalist={datalist}; end
for ii=1:numel(datalist); datalist{ii}=[dataloc datalist{ii}]; end
[dlocs,dnames,~]=cellfun(@fileparts,datalist,'uniformoutput',false);
%% Pick the ROIs
if isscalar(mov_prd)
%the movies to show
movs_show=1:mov_prd:numel(dlocs);
ROIs=cell(1,length(movs_show));
for ii=1:length(movs_show);
filename=[dlocs{movs_show(ii)},filesep,dnames{movs_show(ii)},'.tif'];
%create A `TIFFStack` object which behaves like a read-only memory
%mapped TIFF file
tfstk=TIFFStack(filename);
movsz=size(tfstk);%the size of the movie
%the frames to use
frms_show=round(linspace(1,movsz(3),prct_frms/100*movsz(3)));
movshow=mean(tfstk(:,:,frms_show),3);
disp(dnames{movs_show(ii)});
firstthru=1;%for showing the previous ROI
ROItxtin = 110; %110 is the ASCII code for the letter n
while ROItxtin ~= 121
close(figure(11))
figure(11);
imshow(movshow,prctile(movshow(:),[.1,99.8]))
%this doesn't work with small frame sizes, need to fix this
% htit=title(['The new ROI will be a box \newline First click on the upper right corner',...
% '\newline Then click on the lower left corner']);
% axpos = get(gca,'pos');
% set(htit,'units','normalized');
% extent = get(htit,'extent');
% set(gca,'pos',[axpos(1) axpos(2) axpos(3) axpos(4)-0.66*extent(4)])
if ii>1 && firstthru
ROIclicks=ROIs{ii-1};
ROIpos=[min(ROIclicks(:,1)),min(ROIclicks(:,2)),abs(ROIclicks(1,1)-ROIclicks(2,1)),...
abs(ROIclicks(2,2)-ROIclicks(1,2))];
hold on
rectangle('Position',ROIpos,'EdgeColor','r')
hold off
ROItxtin=input('Does this ROI selection look OK to you (y/n)? ','s');
if isempty(ROItxtin);ROItxtin=110;end
firstthru=0;
end
if ROItxtin ~= 121
% Click and choose fiduciaries. ginput returns
% [col_1,row_1;col_2,row_2;...;col_n,row_n];
ROIclicks = round(ginput(2));
%check the boundaries
if ROIclicks(1,1)>movsz(2);ROIclicks(1,1)=movsz(2);end
if ROIclicks(1,2)<1;ROIclicks(1,2)=1;end
if ROIclicks(2,2)>movsz(1);ROIclicks(2,2)=movsz(1);end
if ROIclicks(2,1)<1;ROIclicks(2,1)=1;end
%the position vector for the rectangle object [x,y,w,h], where x,y
%is the lower left corner
ROIpos=[min(ROIclicks(:,1)),min(ROIclicks(:,2)),abs(ROIclicks(1,1)-ROIclicks(2,1)),...
abs(ROIclicks(2,2)-ROIclicks(1,2))];
hold on
rectangle('Position',ROIpos,'EdgeColor','r')
hold off
ROItxtin=input('Does this ROI selection look OK to you (y/n)? ','s');
if isempty(ROItxtin);ROItxtin=110;end
end
ROIs{ii}=ROIclicks;
end
end
else
ROIs=mov_prd;
end
%% Pull out the selection and write the new movies
for ii=1:numel(dlocs);
tic;
filename=[dlocs{ii},filesep,dnames{ii},'.tif'];
[pathstr,fname,~] = fileparts(filename);
disp(['Pulling out and writing ',fname,append_str])
%create A `TIFFStack` object which behaves like a read-only memory
%mapped TIFF file
tfstk=TIFFStack(filename);
if isscalar(mov_prd)
%which ROI click corresponds to this movie?
ROIclicks=ROIs{find(movs_show<=ii,1,'last')};
else
ROIclicks=ROIs;
end
mov=tfstk(min(ROIclicks(:,2)):max(ROIclicks(:,2)),min(ROIclicks(:,1)):max(ROIclicks(:,1)),:);
%%%%Save the movie%%%%
save([pathstr,filesep,fname,append_str,'.mat'],'mov','-v7.3');
tictoc=toc;%the time to run the entire program
%save a .txt file with the parameters
fileID = fopen([pathstr,filesep,fname,'_ROI_info.txt'],'w');
fprintf(fileID,['ROI corners:\t',mat2str(ROIclicks),'\n']);
fprintf(fileID,['time to run:\t',num2str(tictoc)]);
fclose(fileID);
end
end