-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.m
More file actions
1883 lines (1485 loc) · 65.4 KB
/
Main.m
File metadata and controls
1883 lines (1485 loc) · 65.4 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = Main(varargin)
% MAIN MATLAB code for Main.fig
% MAIN, by itself, creates a new MAIN or raises the existing
% singleton*.
%
% H = MAIN returns the handle to a new MAIN or the handle to
% the existing singleton*.
%
% MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN.M with the given input arguments.
%
% MAIN('Property','Value',...) creates a new MAIN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Main_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Main_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Main
% Last Modified by GUIDE v2.5 08-Apr-2015 23:19:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Main_OpeningFcn, ...
'gui_OutputFcn', @Main_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Main is made visible.
function Main_OpeningFcn(hObject, eventdata, handles, varargin) %#ok<*INUSL>
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Main (see VARARGIN)
% Choose default command line output for Main
handles.output = hObject;
root = [fileparts(fileparts(mfilename('fullpath'))) filesep];
set(handles.dirfile,'string',[root 'Images' filesep]);
set(handles.savedir,'string',[root 'Files' filesep]);
set(handles.iminit,'string','1');
set(handles.imstep,'string','1');
set(handles.imfinal,'string','1000');
set(handles.amin,'string','40');
set(handles.amax,'string','400');
set(handles.xmin,'string','700');
set(handles.xmax,'string','1000');
set(handles.ymin,'string','700');
set(handles.ymax,'string','1000');
set(handles.neurons,'string','0');
set(handles.excentmax,'string','0.85');
% Set toolbar to figure
set(hObject, 'Toolbar', 'figure')
clc
handles.D = struct('directory', '', ...
'image', NaN, ...
'dt', NaN, ...
'mask',NaN, ...
'ROI', NaN, ...
'drift',NaN, ...
'imagenorm',NaN,...
'STATS',NaN, ...
'L',NaN, ...
'wneuron',NaN, ...
'Lcorrect',NaN, ...
'signal',NaN, ...
'background',NaN, ...
'DFF',NaN, ...
'baseline',NaN, ...
'calcium',NaN, ...
'Nneurons',NaN, ...
'range', NaN, ...
'STATSCOMPLET',NaN, ...
'PCuse',NaN);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Main_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function dirfile_Callback(hObject, eventdata, handles)
% hObject handle to dirfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of dirfile as text
% str2double(get(hObject,'String')) returns contents of dirfile as a double
directory=get(handles.dirfile,'String');
filelist=dir([directory '*.tif']);
Nimages=size(filelist,1);
% set(handles.savedir,'String',get(hObject,'String'));
set(handles.Nfiles,'string',int2str(Nimages));
set(handles.imfinal,'string',int2str(Nimages));
im=imread([directory filelist(1).name],'tif');
im2=imread([directory filelist(2).name],'tif');
handles.D.image=im;
dt=get_timestamp(im2)-get_timestamp(im);
handles.D.range=[1 1 Nimages];
handles.D.dt=dt;
handles.D.directory=directory;
info=imfinfo([directory filelist(1).name],'tif');
width=info.Width;
height=info.Height;
xmin=width-width; xmax=width;ymin=height-height;ymax=height;
ROIpreset(hObject,handles,im,xmin,xmax,ymin,ymax); handles=guidata(handles.output);%important for stable transfer
handles=guidata(handles.output);
himageall = imhandles(gcf);
delete(himageall);
%im=transpose((1:100))*(1:100);
imshow(rescalegd(im),'parent',handles.axes1);
% imcontrast(gcf);
guidata(handles.output);
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function dirfile_CreateFcn(hObject, eventdata, handles)
% hObject handle to dirfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in ROI.
function ROI_Callback(hObject, eventdata, handles)
% hObject handle to ROI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of ROI
axes(handles.axes1);
set(handles.message,'String','Select Region Of interest');
% guidata(hObject, handles);
wpoly=roipoly;
set(handles.message,'String','');
guidata(hObject, handles);
[xpoly, ypoly]=find(wpoly==1);
xmin=min(xpoly);
xmax=max(xpoly);
ymin=min(ypoly);
ymax=max(ypoly);
%x=[xmin xmin xmax xmax xmin];
%y=[ymin ymax ymax ymin ymin];
wpoly=double(wpoly);
mask=wpoly(xmin:xmax,ymin:ymax);
im=double(handles.D.image);
imcrop=mask.*im(xmin:xmax,ymin:ymax);
imshow(rescalegd(imcrop),'DisplayRange', [0 1]);
handles.D.ROI=[xmin xmax ymin ymax];
handles.D.mask=mask;
set(handles.xmin,'string',num2str(xmin));
set(handles.ymin,'string',num2str(ymin));
set(handles.xmax,'string',num2str(xmax));
set(handles.ymax,'string',num2str(ymax));
set(hObject,'Value',0);
guidata(handles.output,handles);guidata(hObject, handles);
function Nfiles_Callback(hObject, eventdata, handles)
% hObject handle to Nfiles (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Nfiles as text
% str2double(get(hObject,'String')) returns contents of Nfiles as a double
% --- Executes during object creation, after setting all properties.
function Nfiles_CreateFcn(hObject, eventdata, handles)
% hObject handle to Nfiles (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ymin_Callback(hObject, eventdata, handles)
% hObject handle to ymin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ymin as text
% str2double(get(hObject,'String')) returns contents of ymin as a double
% --- Executes during object creation, after setting all properties.
function ymin_CreateFcn(hObject, eventdata, handles)
% hObject handle to ymin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function xmin_Callback(hObject, eventdata, handles)
% hObject handle to xmin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of xmin as text
% str2double(get(hObject,'String')) returns contents of xmin as a double
% --- Executes during object creation, after setting all properties.
function xmin_CreateFcn(hObject, eventdata, handles)
% hObject handle to xmin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ymax_Callback(hObject, eventdata, handles)
% hObject handle to ymax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ymax as text
% str2double(get(hObject,'String')) returns contents of ymax as a double
% --- Executes during object creation, after setting all properties.
function ymax_CreateFcn(hObject, eventdata, handles)
% hObject handle to ymax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function xmax_Callback(hObject, eventdata, handles)
% hObject handle to xmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of xmax as text
% str2double(get(hObject,'String')) returns contents of xmax as a double
% --- Executes during object creation, after setting all properties.
function xmax_CreateFcn(hObject, eventdata, handles)
% hObject handle to xmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton4
% --- Executes on button press in togglebutton5.
function togglebutton5_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton5
function iminit_Callback(hObject, eventdata, handles)
% hObject handle to iminit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
range=handles.D.range;
range(1)=str2double(get(hObject,'String'));%changed to str2double TB
handles.D.range=range;
guidata(hObject, handles);
% handles.D.range
% Hints: get(hObject,'String') returns contents of iminit as text
% str2double(get(hObject,'String')) returns contents of iminit as a double
% --- Executes during object creation, after setting all properties.
function iminit_CreateFcn(hObject, eventdata, handles)
% hObject handle to iminit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function imstep_Callback(hObject, eventdata, handles)
% hObject handle to imstep (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
range=handles.D.range;
range(2)=str2double(get(hObject,'String'));
handles.D.range=range;
guidata(hObject, handles);
handles.D.range
% Hints: get(hObject,'String') returns contents of imstep as text
% str2double(get(hObject,'String')) returns contents of imstep as a double
% --- Executes during object creation, after setting all properties.
function imstep_CreateFcn(hObject, eventdata, handles)
% hObject handle to imstep (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function imfinal_Callback(hObject, eventdata, handles)
% hObject handle to imfinal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
range=handles.D.range;
range(3)=str2double(get(hObject,'String'));
handles.D.range=range;
guidata(hObject, handles);
handles.D.range
% Hints: get(hObject,'String') returns contents of imfinal as text
% str2double(get(hObject,'String')) returns contents of imfinal as a double
% --- Executes during object creation, after setting all properties.
function imfinal_CreateFcn(hObject, eventdata, handles)
% hObject handle to imfinal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in cor4drift.
function cor4drift_Callback(hObject, eventdata, handles)
% hObject handle to cor4drift (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of cor4drift
% --- Executes on button press in GO_createim.
function GO_createim_Callback(hObject, eventdata, handles)
% hObject handle to GO_createim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
changeimarray=get(handles.changeimarray,'value');
writeaverage=get(handles.writeaverage,'value');
cor4drift=get(handles.cor4drift,'value');
init=str2double(get(handles.iminit,'String'));
final=str2double(get(handles.imfinal,'String'));
steps=str2double(get(handles.imstep,'String'));
Nimages=final-init+1;
directory=get(handles.dirfile,'String');
filelist=dir([directory '*.tif']);
xmin=str2double(get(handles.xmin,'String'));
xmax=str2double(get(handles.xmax,'String'));
ymin=str2double(get(handles.ymin,'String'));
ymax=str2double(get(handles.ymax,'String'));
set(handles.message,'String',' ');drawnow;
if cor4drift
dx1=zeros(Nimages,1);
dy1=dx1;
imref=0;
for i=init:(init+4) %steps
im=imread([directory filelist(i).name]);
imref=double(im)+imref;
end
imref=imref/i;%steps TB CHANGE;
% [xim,yim]=meshgrid(1:size(imref,2),1:size(imref,1));surf(xim,yim); tb
imshow(rescalegd(imref(xmin:xmax,ymin:ymax)),[0 1],'parent',handles.axesplot);drawnow;
end
new=0;
Fxy=0;
tic
for i=1:steps:Nimages;
h=i/Nimages;
mesg=hbargd(h,100,'-');
set(handles.message,'String',['calculating...' mesg]);drawnow;
% guidata(hObject, handles);
im=imread([directory filelist(i+init-1).name]);
im=double(im);
if cor4drift
dXinit=0;
dYinit=0;
if i>1
dXinit=round(dx1(i-steps));
dYinit=round(dy1(i-steps));
end
[dx1(i),dy1(i)]=correct_drift4(imref(xmin:xmax,ymin:ymax),im(xmin:xmax,ymin:ymax),dXinit,dYinit,20,handles);
if i>1
plot(dx1(dx1 ~= 0),dy1(dx1 ~= 0),'Parent',handles.axesplot)
title('2-D Line Plot');
xlabel('shift x');
ylabel('shift y')
% xlim([floor(min(dx1)) ceil(max(dx1))]);
% ylim([floor(min(dy1)) ceil(max(dy1))]);
axis equal
drawnow;
end
im=bilinearshift(im,dx1(i),dy1(i));
%[yim,xim]=meshgrid(1:size(imref,2),1:size(imref,1));
%im=interp2(yim,xim,im,yim-dy(i),xim-dx(i),'Linear',0);
%im=interp2(xim,yim,im,xim-dx(i),yim-dy(i),'Linear',0);
end
% if changeimarray
% newpicarray(:,:,i)=Fxy;
% writeframes(hObject,handles,im,writeaverage,changeimarray,newfilename)
% end
if changeimarray
if new==0
overwrite='overwrite';
new=1;
else
overwrite='append';
end
newfilename='seq_calcium';
if cor4drift
diffile=strcat('Dif_',newfilename,'.tif');
else
diffile=strcat('Origin_',newfilename,'.tif');
end
writeframes(hObject,handles,im(xmin:xmax,ymin:ymax),diffile,overwrite);
end
Fxy=Fxy+im;
end
if cor4drift
t1=(1:steps:Nimages);
t=(1:Nimages);
%dxfilt=medfilt1(dx(t1),3*steps);
%dyfilt=medfilt1(dy(t1),3*steps);
%dx1=interp1(t1,dxfilt,t,'linear',0);
%dy1=interp1(t1,dyfilt,t,'linear',0);
dx=interp1(t1,dx1(t1),t,'spline',0);
dy=interp1(t1,dy1(t1),t,'spline',0);
% plot(handles.axes1,dx1(t1),dy1(t1))
% hold on
% plot(handles.axes1,dx(t),dy(t),'Parent',handles.axes1);
% % pause(10)
% hold off
% plot(dx1,dy1,'parent',handles.axes1);
handles.D.drift=[transpose(dx) transpose(dy)];
end
Fxy=Fxy/fix(Nimages/steps);
handles.D.image=Fxy;
imshow(rescalegd(Fxy(xmin:xmax,ymin:ymax)),[0 1],'parent',handles.axes1);drawnow;
if writeaverage
if cor4drift
newfilename='driftcorrected';
else
newfilename='notcorrected';
end
avefile=strcat('Ave_',newfilename,'.tif');
overwrite='overwrite';
writeframes(hObject,handles,Fxy(xmin:xmax,ymin:ymax),avefile,overwrite);
end
% i=45
toc
set(hObject,'Value',0);
set(handles.message,'String','finished');drawnow;
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of GO_createim
% --- Executes during object creation, after setting all properties.
function plot1_CreateFcn(hObject, eventdata, handles)
% hObject handle to plot1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate plot1
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% --- Executes on button press in segment.
function segment_Callback(hObject, eventdata, handles)
% hObject handle to segment (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%
% h = get(handles.axes1,'Children')
haxesplot = findobj(handles.axesplot,'Type','image');
himageall = imhandles(gcf);
delete(himageall);
set(handles.message,'String','segmentation start...');drawnow;
im=handles.D.image;
mask=handles.D.mask;
xmin=str2double(get(handles.xmin,'String'));
xmax=str2double(get(handles.xmax,'String'));
ymin=str2double(get(handles.ymin,'String'));
ymax=str2double(get(handles.ymax,'String'));
% create a well contrasted image for display
smwindow=str2double(get(handles.pixkernel,'String'));
minvalue=floor(smwindow^2*0.1);
maxvalue=floor(smwindow^2*0.8);
im2=im(xmin:xmax,ymin:ymax);
B = ordfilt2(im2,minvalue,true(smwindow));
C = ordfilt2(im2,maxvalue,true(smwindow)); % prend la valeur du dernier décile
% imagenorm=rescalegd((im2-B)./(C-B));
imagenorm=im;%(im2-B)./(C-B);
imagenorm=imclearborder(imagenorm);
imshow(imagenorm(xmin:xmax,ymin:ymax),[0 max(imagenorm(:))/20],'parent',handles.axes1);
set(handles.message,'String','segmentation normalized image ...');drawnow;
handles.D.imagenorm=imagenorm;
areamax=str2double(get(handles.amax,'String'));
areamin=str2double(get(handles.amin,'String'));
excentmax=str2double(get(handles.excentmax,'String'));
%---------------------------------------------------
L=segmentgd(handles,imagenorm(xmin:xmax,ymin:ymax));
%---------------------------------------------------
STATS = regionprops(double(L).*mask, 'Area','PixelIdxList','Eccentricity','Centroid');
a=field2num(STATS,'Area');
ex=field2num(STATS,'Eccentricity');
wneuron=ones(size(a,1),1);
w= a>areamax | a<areamin | ex>excentmax;
wneuron(w)=0;
% Nneurons=squeeze(sum(wneuron));
handles.D.STATSCOMPLET=STATS;
handles.D.STATS=STATS(wneuron==1);
% size(STATS(wneuron(find(wneuron==1))))
handles.D.L=L;
handles.D.wneuron=wneuron;
guidata(handles.output);
set(handles.message,'String','segmentation ROIs ...');drawnow;
handles=disp_segment(hObject, handles);
set(hObject,'Value',0);
set(handles.message,'String','finished');drawnow;
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of segment
function amin_Callback(hObject, eventdata, handles)
% hObject handle to amin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of amin as text
% str2double(get(hObject,'String')) returns contents of amin as a double
handles=update_constraints(hObject,handles);
guidata(hObject, handles);
handles=disp_segment(hObject,handles);
% --- Executes during object creation, after setting all properties.
function amin_CreateFcn(hObject, eventdata, handles)
% hObject handle to amin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function amax_Callback(hObject, eventdata, handles)
% hObject handle to amax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=update_constraints(hObject,handles);
handles=disp_segment(hObject,handles);
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of amax as text
% str2double(get(hObject,'String')) returns contents of amax as a double
% --- Executes during object creation, after setting all properties.
function amax_CreateFcn(hObject, eventdata, handles)
% hObject handle to amax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function excentmax_Callback(hObject, eventdata, handles)
% hObject handle to excentmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=update_constraints(hObject,handles);
handles=disp_segment(hObject,handles);
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of excentmax as text
% str2double(get(hObject,'String')) returns contents of excentmax as a double
% --- Executes during object creation, after setting all properties.
function excentmax_CreateFcn(hObject, eventdata, handles)
% hObject handle to excentmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function neurons_Callback(hObject, eventdata, handles)
% hObject handle to neurons (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of neurons as text
% str2double(get(hObject,'String')) returns contents of neurons as a double
% --- Executes during object creation, after setting all properties.
function neurons_CreateFcn(hObject, eventdata, handles)
% hObject handle to neurons (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of deleteneuron
% --- Executes on button press in delreseneuron.
function delreseneuron_Callback(hObject, eventdata, handles)
% hObject handle to delreseneuron (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
L=handles.D.L;
while get(hObject,'Value')==1
wneuron=handles.D.wneuron;
k = waitforbuttonpress;
p = get(gca,'CurrentPoint');
x = ceil(p(1,2));
y = ceil(p(1,1));
if get(hObject,'Value')==0;
break
end
if (x < size(L,1) && x >0 && y < size(L,2) && y >0)
if L(x,y)>0
wneuron(L(x,y))=mod((1+wneuron(L(x,y))),2);
end
end
handles.D.wneuron=wneuron;
guidata(hObject, handles);
handles=disp_segment(hObject, handles);
end
set(hObject,'Value',0);
guidata(hObject, handles);
handles=disp_segment(hObject, handles);
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of delreseneuron
% --- Executes on button press in delregion.
function delregion_Callback(hObject, eventdata, handles)
% hObject handle to delregion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
wneuron=handles.D.wneuron;
L=handles.D.L;
axes(handles.axesplot); title('Select Area');
wpoly=roipoly;
temp=find(wpoly==1);
indneur=unique(L(temp));
indneur=indneur(indneur>0);% indices of the neurons within the polygone
for i=1:size(indneur)
wneuron(indneur(i))=0;%mod((1+wneuron(indneur(i))),2);
end
handles.D.wneuron=wneuron;
guidata(hObject, handles);
handles=disp_segment(hObject, handles);
set(handles.delregion,'Value',0)
% Hint: get(hObject,'Value') returns toggle state of delregion
function [handles_out]=disp_segment(hObject, handles)
% This function plot the segmented image based on the
% current value of active neurons
im=handles.D.imagenorm;
mincentile=fix(min(im(:)));
maxcentile=fix(max(im(:))/100);
im=(im-mincentile)/(maxcentile-mincentile);
xmin=str2double(get(handles.xmin,'String'));
xmax=str2double(get(handles.xmax,'String'));
ymin=str2double(get(handles.ymin,'String'));
ymax=str2double(get(handles.ymax,'String'));
L=handles.D.L;
STATS=handles.D.STATSCOMPLET;%---------------------
mask=handles.D.mask;
wneuron=handles.D.wneuron;
Nneurons=sum(wneuron);
wnoneuron=1-wneuron;
temp=ismember(L,wnoneuron.*transpose((1:size(wneuron,1))));
% produce a matrix of size identical to L with value 1
% where the neuron is to be deleted
w= temp(:) == 1;
L(w)=0;
L=double(L).*mask;
coloredLabels = label2rgb (L, 'hsv', 'k', 'shuffle'); % pseudo random color labe
% Fxy=rescalegd(im)/1.1;
Fxy=im(xmin:xmax,ymin:ymax); %tb
% equalize Fxy
imcolor=zeros(size(Fxy,1),size(Fxy,2),3);
Fxy (L>0)=0;
imcolor(:,:,2)=Fxy;
Fxy (L>0)=0;
imcolor(:,:,3)=Fxy;
% Fxy(L==0)=1;%1.5*Fxy(L==0);
Fxy (L>0)=1;
imcolor(:,:,1)=Fxy;
halphablend = vision.AlphaBlender('Opacity',0.86);
J = step(halphablend,imcolor,double(coloredLabels));
%----------------------------------------------------------
%figure(5);
imshow(J,[0 max(J(:))],'Parent',handles.axesplot);
drawnow;
%----------------------------------------------------------
handles.D.Lcorrect=L;
handles.D.STATS=STATS(wneuron==1);
handles.D.Nneurons=Nneurons;
set(handles.neurons,'string',int2str(Nneurons));
set(handles.axes1,'NextPlot','replacechildren');
% himage = imhandles(gca);
% delete(himage);
handles_out=handles;
guidata(hObject, handles);
function [handles_out]=update_constraints(hObject, handles)
areamin=str2double(get(handles.amin,'String'));
areamax=str2double(get(handles.amax,'String'));
excentmax=str2double(get(handles.excentmax,'String'));
wneuron=handles.D.wneuron;
STATS=handles.D.STATSCOMPLET;
a=field2num(STATS,'Area');
ex=field2num(STATS,'Eccentricity');
wneuron=ones(size(a,1),1);
w=find(a>areamax | a<areamin | ex>excentmax);
wneuron(w)=0;
w=find(a<=areamax & a>=areamin & ex<=excentmax);
wneuron(w)=1;
disp('update constraints')
Nneurons=sum(wneuron)
handles.D.wneuron=wneuron;
handles_out=handles;
guidata(hObject, handles);
% --- Executes on button press in get_signal.
function get_signal_Callback(hObject, eventdata, handles)
% hObject handle to get_signal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.axes1,'NextPlot','replace');
L=handles.D.L;
mask=handles.D.mask;
imref=handles.D.image;
STATS=handles.D.STATS;
wneuron=handles.D.wneuron;
cor4drift=get(handles.cor4drift,'value');
extractfluo=get(handles.extract_fluorescence,'value');
extractDFF=get(handles.extract_DFF,'value');
extractcal=get(handles.extract_calcium,'value');
cor4motionart=get(handles.cor4motionart,'value');
init=str2double(get(handles.iminit,'String'));
final=str2double(get(handles.imfinal,'String'));
steps=str2double(get(handles.imstep,'String'));
Nimages=final-init+1;
directory=get(handles.dirfile,'String');
filelist=dir([directory '*.tif']);
xmin=str2double(get(handles.xmin,'String'));
xmax=str2double(get(handles.xmax,'String'));
ymin=str2double(get(handles.ymin,'String'));
ymax=str2double(get(handles.ymax,'String'));
if cor4drift
drift=handles.D.drift;
dx=drift(:,1);
dy=drift(:,2);
end
w=find(wneuron == 1);
neurons=STATS; %(w);
Nneurons=size(neurons,1);
signalmat=zeros(Nimages,Nneurons);
[xim,yim]=meshgrid(1:(ymax-ymin+1),1:(xmax-xmin+1));
% get background if extract DFF
if extractDFF
set(handles.message,'String','Select background region');
guidata(hObject, handles);
imshow(rescalegd(imref));
wpoly=roipoly;
imshow(rescalegd(imref));
temp=wpoly.*imref;
Npix=size(find(wpoly==1),1);
background=sum(temp(:))/Npix;
handles.D.background=background;
set(handles.message,'String','v ');
guidata(hObject, handles);
end
% Extract fluorescence signal
if extractfluo
set(handles.message,'String','extracting fluorescence ...');
pause(0.001)
%guidata(hObject, handles);
for t=1:Nimages;
h=double(t/Nimages);
mesg=hbargd(h,100,'-');
set(handles.message,'String',['extracting fluorescence ...' mesg]);
pause(0.001)
%guidata(hObject, handles);
% if mod(t,100)==0
% disp(t)
% end
im=imread([directory filelist(t+init-1).name]);
im=double(im(xmin:xmax,ymin:ymax));
if cor4drift