/
background.m
executable file
·375 lines (326 loc) · 11.1 KB
/
background.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
function [image_stack_b]=background(image_stack_reg,name_array,stack);
[~,~,Z]=size(image_stack_reg);
% User Input-Decide which background removal method
for Z=1:Z; % selects image from its z value
image_slice=image_stack_reg(:,:,Z);
% This starts loop as if user was unhappy about image change
C=0;
% Used later for printing text on images
BPF_Print=0;
count=1;
while C==0 || User_Error==0 %this loop allows user to change filter if unhappy about analysed image
% If input incorrect starts loop regardless wether user inputs 1 or 0
clc
%Display key & User selects background
string_image=['Processing background for image:',num2str(Z)];
msgbox(string_image);
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
msgbox('Please ensure that if you are using a BPF image to now apply OUT, RB or N','Icon','warn');
pause(5)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.5)
prompt = {'Please enter method of background removal."RB"=rolling ball, "BPF" for bandpass filter, "OUT" to select an ROI outside of cell to remove background, "N"=no background removal:'};
dlg_title = 'Input for background removal';
num_lines = 2;
def = {'N'};
background = inputdlg(prompt,dlg_title,num_lines,def);
% If cancel is hit background is 0x0 array this makes sure it is readable by switch
[X,~]=size(background);
if X==0
background_switch='K';
else
background_switch=background{1,1};
end
% Selects Max intensity value of uint16 image
[h,~] = max(image_slice(:));
%%
switch(background_switch)
%========================================================================
% User selects rolling ball
case 'RB'
count=1;
prompt = {'Please enter radius of rolling ball: '};
dlg_title = 'Input for rolling ball';
num_lines = 2;
def = {'10'};
size_r = inputdlg(prompt,dlg_title,num_lines,def);
% If cancel is hit size_r is 0x0 array this makes sure it is readable by switch
[X,~]=size(size_r);
if X==0
size1=10; % if cancel hit default used
else
size1=size_r{1,1};
size1=str2num(size1);
end
se = strel('ball',size1,size1,0);% Creates ball
J = imtophat(image_slice,se);% Opens,dilates and subtracts background
% Allows manipulation of figure and Subplot
figure_1=figure;SP1=subplot(1,2,1);imshow(image_slice,[0 h]);title('Original Image')
% Set figure size, Positon
set(figure_1,'Position',[50 50 800 600]);
set(SP1,'Position',[0.05 0.05 0.45 0.96]);
SP2=subplot(1,2,2);imshow(J,[0 h]);title('New Image')
set(SP2,'Position',[0.53 0.05 0.45 0.96]);
uiwait()
User_Error=1;
Print_Text=1;
BPF_Used=0;
prompt = {'Do you wish to apply threshold to remove low level background and cytoplasm not removed by RB? Type Y to apply, N to continue, If previously applied BPF to this image please type N'} ;
dlg_title = 'Input for user threshold';
num_lines = 2;
def = {'N'};
thresh = inputdlg(prompt,dlg_title,num_lines,def);
[X,~]=size(thresh);
if X==0
thresh{1,1}='K';
else
end
switch thresh{1,1}
case 'Y'
prompt = {'Please enter threshold percentage value i.e 5=5%, 5% will remove lowest 5% of image usually non uniform background, 20% will higlight defined structures:'} ;
dlg_title = 'Input for threshold';
num_lines = 2;
def = {'5'};
threshold = inputdlg(prompt,dlg_title,num_lines,def);
[X,~]=size(threshold);
if X==0
threshold{1,1}='0';
else
end
threshold=str2num(threshold{1,1});
K=J;
percent_threshold=(max(J(:))/100)*threshold;
[Y,X]=find(J<percent_threshold);
thresh_idx=sub2ind(size(J),Y',X');
J(thresh_idx)=0;
string=[num2str(threshold),'%= intensity value ',num2str(percent_threshold)];
msgbox(string)
pause(3);
figure_1=figure;SP1=subplot(1,2,1);imshow(K,[0 h]);title('Original Image')
% Set figure size, Positon
set(figure_1,'Position',[50 50 800 600]);
set(SP1,'Position',[0.05 0.05 0.45 0.96]);
SP2=subplot(1,2,2);imshow(J,[0 h]);title('New Image')
set(SP2,'Position',[0.53 0.05 0.45 0.96]);
uiwait()
case'N'
otherwise
clc
h = errordlg('Unknown input starting background removal on Original image');
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.1)
User_Error=0;
end
%=========================================================================
%User selects region outside cell
case'OUT'
count=1;
msgbox('Please select area outside area to draw Region of Interest in upcoming figure')
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
figure;imshow(image_slice,[0 h]);
J=image_slice;
%Plots ROI
roi=impoly;
% Creates mask based on freehand ROI above shown image
BW=createMask(roi);
% Uses maximum value outside cell
Max_Bground=regionprops(BW,J,'MaxIntensity');
Max_Bground=Max_Bground.MaxIntensity;
J=J-Max_Bground;
% Allows manipulation of figure and Subplot
figure_1=figure;SP1=subplot(1,2,1);imshow(image_slice,[0 h]);title('Original Image')
% Set figure size, Positon
set(figure_1,'Position',[50 50 800 600]);
set(SP1,'Position',[0.05 0.05 0.45 0.96]);
SP2=subplot(1,2,2);imshow(J,[0 h]);title('New Image')
set(SP2,'Position',[0.53 0.05 0.45 0.96]);
uiwait()
clc
User_Error=1;
Print_Text=2;
BPF_Used=0;
%==========================================================================
%User selects BP filter
case 'BPF'
if count==2
msgbox('Only use BPF once on an image,starting from original unmodified image','Icon','warn');
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
User_Error=0;
image_slice=image_stack_reg(:,:,Z);
BPF_Print=2;
Print_Text=0;
BPF_Used=0;
else
% User inputs variables just like they would in ImageJ
prompt = {'Filter large structures down to:','Filter small structures down to:'};
dlg_title = 'Input for BPF function';
num_lines = 2;
def = {'40','2'};
BPF_Size = inputdlg(prompt,dlg_title,num_lines,def);
[X,~]=size(BPF_Size);
if X==0
large_struct=40;
small_struct=2;
else
large_struct=str2double(BPF_Size{1,1});
small_struct=str2double(BPF_Size{2,1});
end
% Export Image to ImageJ
MIJ.createImage(image_slice);
% String defining variables in macro
strng2=['filter_large=',num2str(large_struct),' filter_small=',num2str(small_struct),' suppress=None tolerance=5'];
% Macro FFT Bandpass filter
MIJ.run('Bandpass Filter...', strng2);
J=MIJ.getCurrentImage();
MIJ.run('Close');
J=uint16(J);
% Allows manipulation of figure and Subplot
figure_1=figure;SP1=subplot(1,2,1);imshow(image_slice,[0 h]);title('Original Image')
% Set figure size, Position
set(figure_1,'Position',[50 50 800 600]);
set(SP1,'Position',[0.05 0.05 0.45 0.96]);
[mi,~] = min(J(:));
SP2=subplot(1,2,2);imshow(J,[mi h]);title('New Image')
set(SP2,'Position',[0.53 0.05 0.45 0.96])
uiwait()
clc
prompt = {'Are you happy with BPF_Image? . Type N to start different Background removal on original image. Type BPF to apply another background removal to BPF image. Type L to leave BPF Image and stack it for image analysis:'} ;
dlg_title = 'Input for user BPF processing';
num_lines = 2;
def = {'N'};
BPF_2 = inputdlg(prompt,dlg_title,num_lines,def);
[X,~]=size(BPF_2);
if X==0
BPF_2{1,1}='K';
else
end
switch BPF_2{1,1}
case 'N'
msgbox('Restarting backgorund removal')
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.1)
User_Error=0;
BPF_Print=0;
case 'BPF'
image_slice=J;
User_Error=1;
BPF_Print=1;
BPF_Used=1;
count=count+1;
Print_Text=0;
case 'L'
User_Error=1;
Print_Text=3;
BPF_Used=1;
otherwise
clc
h = errordlg('Unknown input starting background removal on Original image');
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.1)
User_Error=0;
end
end
%==========================================================================
% User stated no background to be applied
case 'N'
msgbox('No background applied');
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.1)
J=image_slice;
User_Error=1;
Print_Text=4;
BPF_Used=0;
%==========================================================================
% Error loop started again
otherwise
h = errordlg('Unknown input starting background removal on Original image');
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.1)
User_Error=0;
BPF_Used=0;
Print_Text=0;
end
%==========================================================================
% This ask user if they are happy with input, if BPF has been used it skips
if BPF_Used==1
elseif User_Error==0;%Starts loop if any errors found
image_slice=image_stack_reg(:,:,Z);
C=0;
BPF_Print=0;
elseif BPF_Used==0;
% If last method used is RB, OUT or N asks user if they are happy with
% image
prompt = {'Please enter Y if you are happy with image, enter N to restart background removal on original image: '} ;
dlg_title = 'Input for user happy with background';
num_lines = 2;
def = {'N'};
User_happy = inputdlg(prompt,dlg_title,num_lines,def);
[X,~]=size(User_happy);
if X==0
User_happy{1,1}='K';
else
end
switch User_happy{1,1}
case 'Y'
C=1;
User_Error=1;
case 'N'
image_slice=image_stack_reg(:,:,Z);
C=0;
BPF_Print=0;
User_Error=0;
otherwise
h = errordlg('Unknown input starting background removal on Original image');
pause(3)
delete(findobj(allchild(0), '-regexp', 'Tag', '^Msgbox_'));
pause(0.1)
C=0;
BPF_Print=0;
User_Error=0;
end
clc
end
close all
%Once user is happy with image this then produces text to overlay images
if Print_Text==1 && BPF_Print==1;
image_text=['BPF image,L Structures:',num2str(large_struct),' S structures: ',num2str(small_struct), ', followed by RB radius= ',num2str(size1)];
elseif Print_Text==2 && BPF_Print==1
image_text=['BPF image,L Structures:',num2str(large_struct),' S structures: ',num2str(small_struct), ', followed by Region Outside Cell'];
elseif Print_Text==4 && BPF_Print==1
image_text=['BPF image,L Structures:',num2str(large_struct),' S structures: ',num2str(small_struct), ', followed by None'];
elseif Print_Text==1;
image_text=['RB radius= ',num2str(size1)];
elseif Print_Text==2;
image_text='Region outside cell';
elseif Print_Text==3;
image_text=['BPF image,L Structures:',num2str(large_struct),' S structures: ',num2str(small_struct)];
elseif Print_Text==4
image_text='No background';
elseif Print_Text==5 && BPF_Print==1
image_text=['BPF image,L Structures:',num2str(large_struct),' S structures: ',num2str(small_struct), ', followed by RB radius= ',num2str(size1),'followed by threshold',string];
elseif Print_Text==5
image_text=['RB radius= ',num2str(size1),'followed by threshold', string];
else
end
if BPF_Used==1 || User_Error==0;
count=1;
else
%Printing of figure with text, array for next function is sorted
fig_to_file=figure;imshow(J, [0 h]);
set(fig_to_file,'visible','off')
TXT=text(20,20,image_text);
set(TXT,'color',[1 0 0]);
print(fig_to_file, '-dtiffn',name_array{stack,Z});
image_stack_b(:,:,Z)=J;
end
end
clc
end