summaryrefslogtreecommitdiff
path: root/gui_win.m
blob: 39c5350a73855105826a84cdfae1eefe2433f7e3 (plain)
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
function varargout = gui_win(varargin)
% gui_win MATLAB code for gui_win.fig
%      gui_win, by itself, creates a new gui_win or raises the existing
%      singleton*.
%
%      H = gui_win returns the handle to a new gui_win or the handle to
%      the existing singleton*.
%
%      gui_win('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in gui_win.M with the given input arguments.
%
%      gui_win('Property','Value',...) creates a new gui_win or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the gui_win before gui_win_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_win_OpeningFcn via varargin.
%
%      *See gui_win Options on GUIDE's Tools menu.  Choose "gui_win allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_win

% Last Modified by GUIDE v2.5 18-Sep-2012 16:35:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @gui_win_OpeningFcn, ...
    'gui_OutputFcn',  @gui_win_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 gui_win is made visible.
function gui_win_OpeningFcn(hObject, eventdata, handles, varargin)
% 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 gui_win (see VARARGIN)

% Choose default command line output for gui_win
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


% Set color of the strart button to green
set(handles.button2start,'BackgroundColor','Green');

% Declare global variables
global background_m;
global isworking;
global image_display;
global image_original;
global image_cleaned;
global image_fitted;
global image_cross;

global default_data_path;
default_data_path='Z:\beam_profiler_images';

% Initial values
background_m = 0;
image_display = 0;
image_original = 0;
image_cleaned = 0;
image_fitted = 0;
image_cross = 0;
isworking = 0;

% Don`t show ticks for the minor windows
set(handles.backgrounddisplay,'PlotBoxAspectRatio',[4 3 1],'DataAspectRatio',[1 1 1],'YTick',[],'XTick',[]);
set(handles.xsectiondisplay,'PlotBoxAspectRatio',[4 3 1],'DataAspectRatio',[1 1 1],'YTick',[],'XTick',[]);
set(handles.ysectiondisplay,'PlotBoxAspectRatio',[4 3 1],'DataAspectRatio',[1 1 1],'YTick',[],'XTick',[]);



% --- Outputs from this function are returned to the command line.
function varargout = gui_win_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 display_stuff(handles)
global image_display;
global background_m;


% If we need to substract the background, we do it
% background_m is a global variable
if get(handles.button2subtract_backround,'Value') > 0
%    m = image_display - background_m;
else
%    m = image_display;
end;

[Ny, Nx] = size(image_display);

% Display what we have before doing fit
axes(handles.image2display);
imagesc(image_display);
% we need specific aspect ratio for images for them not to be stretched
set(handles.image2display,'PlotBoxAspectRatio',[Nx Ny 1],'DataAspectRatio',[1 1 1]);
%colorbar;
%colormap hot;
drawnow;

% display cross lines if needed
if get(handles.showcross,'Value')
    display_cross_marks_and_cross_sections(handles)
end
    

function show_cross_marks(handles)
global cross_coordinates;
global image_display;
[Ny, Nx] = size(image_display);
if  any( isnan(cross_coordinates) )
    % cross coordinates are undefined no need to draw cross
    axes(handles.image2display);
    hold off;
else
    x0=cross_coordinates(1);
    y0=cross_coordinates(2);
    hline_x=[1,Nx];
    hline_y=[y0,y0];
    vline_x=[x0,x0];
    vline_y=[1,Ny];
    cross_color = 'white';
    cross_line_width =2;
    % show cross marks
    axes(handles.image2display);
    hold on;
    plot(hline_x, hline_y, 'LineWidth', cross_line_width, 'Color', cross_color);
    plot(vline_x, vline_y, 'LineWidth', cross_line_width, 'Color', cross_color);
    hold off;
end


function [hsection, vsection] = get_cross_sections_of_image(img,x0,y0)
% define actual image cross sections
[Ny, Nx] = size(img);
[~, x_adjusted] = check_bound(x0, 1, Nx);
[~, y_adjusted] = check_bound(y0, 1, Ny);

if  any(isnan([x0,y0]))||x_adjusted||y_adjusted
    % we just assign bunch of zeros for not set cross section positions
    vsection = img(:,1)*NaN;
    hsection = img(1,:)*NaN;
else
    % define displayed image cross section
    vsection = img(:,x0);
    hsection = img(y0,:);
end

function plot_cross_section(handles, hsection, vsection, color, label)
% draw above cross sections
% vertical
axes(handles.ysectiondisplay);
h = plot(vsection);
set(h,'Color',color,'LineWidth',2);
xlim('auto');
ylim('auto');
hold on
% horizontal
axes(handles.xsectiondisplay);
h = plot(hsection);
set(h,'Color',color,'LineWidth',2);
xlim('auto');
ylim('auto');
hold on



function display_cross_marks_and_cross_sections(handles)
global cross_coordinates;
global image_original;
global image_display;
global image_fitted;
global image_cleaned;

[Ny, Nx] = size(image_display);
x0=cross_coordinates(1);
y0=cross_coordinates(2);
% define actual image cross sections
%axes(handles.ysectiondisplay);
%hold off;
%axes(handles.xsectiondisplay);
%hold off;
[hsection, vsection] = get_cross_sections_of_image(image_display,x0,y0);
plot_cross_section(handles, hsection, vsection, 'red', 'displayed');
[hsection, vsection] = get_cross_sections_of_image(image_fitted,x0,y0);
plot_cross_section(handles, hsection, vsection, 'blue', 'fitted');
[hsection, vsection] = get_cross_sections_of_image(image_cleaned,x0,y0);
plot_cross_section(handles, hsection, vsection, 'green', 'cleaned');
[hsection, vsection] = get_cross_sections_of_image(image_original,x0,y0);
plot_cross_section(handles, hsection, vsection, 'black', 'original');
axes(handles.ysectiondisplay);
hold off;
axes(handles.xsectiondisplay);
hold off;

% update cross marks
show_cross_marks(handles)


function img_new=subtract_background(handles,img)
global background_m;
if get(handles.button2subtract_backround,'Value')
    set(handles.button2subtract_backround,'String','ON');
    img_new = img - background_m;
else
    set(handles.button2subtract_backround,'String','OFF');
    img_new = img;
end;

function grab_image_and_update_display(handles)
global image_display;
global image_original;
shutter_time = num2str(get(handles.readshutterspeed,'String'));
Naverages = get(handles.readnumber,'String');
image_original = double(grabimage(shutter_time, Naverages));
image_display = subtract_background(handles,image_original);
display_stuff(handles);



% This function is executed when START button is pressed.
% We do different stuff depending on the radiobutton selection

% --- Executes on button press in button2start.
function button2start_Callback(hObject, eventdata, handles)
% hObject    handle to button2start (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 button2start

global isworking;
global image_display;
global image_cleaned;
global image_fitted;
image_cleaned = 0;
image_fitted = 0;
set(handles.chooseoriginal,'Value',1);
isworking =1;
% Paint the button and change text
set(handles.button2start,'BackgroundColor','Red');
set(handles.button2start,'String','STOP');
drawnow;    % needed to draw this before doing fit

% This is for the single run
if get(handles.button2singlerun,'Value')
    grab_image_and_update_display(handles);
end;

% This executes when we do the continuous run
if get(handles.button2continuous_run,'Value')   
    while get(handles.button2start,'Value') > 0      
        grab_image_and_update_display(handles);
    end; 
end;



% Paint it back
set(handles.button2start,'Value',0);
set(handles.button2start,'BackgroundColor','Green');
set(handles.button2start,'String','START');
drawnow;
isworking =0;




% If we change the radiobutton setting we unpress the start button which will stop any activity.. eventually
% Also here we check if we want to choose a file from a the file-open
% dialog

% --- Executes when selected object is changed in workstylechoise.
function workstylechoise_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in workstylechoise
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

set(handles.button2start,'Value',0);
global isworking;
global image_display;
global image_original;
global pathname;
isworking =1;
set(handles.chooseoriginal,'Value',1);

% This calls a file-open dialog and runs the beam profiler on the choosen
% file
if get(handles.button2loadimage,'Value')
    set(handles.button2start,'Value',1);
    set(handles.button2start,'BackgroundColor','Red');
    set(handles.button2start,'String','STOP');
    drawnow;
    
    img=choose_and_read_image_from_file();
    if ~any(isnan(img))
        image_display=double(img);
        image_original=image_display;
        image_display = subtract_background(handles,image_original);
        display_stuff(handles);
    end
    
    set(handles.button2start,'BackgroundColor','Green');
    set(handles.button2start,'Value',0);
    set(handles.button2start,'String','START');
    drawnow;
end;
set(handles.button2loadimage,'Value',0)
isworking =0;



% Executes when we press Clean button

% --- Executes on button press in button2clean.
function button2clean_Callback(hObject, eventdata, handles)
% hObject    handle to button2clean (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global isworking;
global image_display;
global image_cleaned;
global image_negative;
global background_m;


if (~isworking)
    isworking = 1;
    set(handles.button2clean,'String','Working');
    %read the attenuation from the GUI
    filter = str2num(get(handles.readstrength,'String'));
    radius = str2num(get(handles.readrange,'String'));
    
    %clean the image with the stripe eraser
    [image_cleaned,~]=stripeeraser(image_display,radius,filter);
    image_cleaned=abs(image_cleaned);
    image_display=image_cleaned;
    display_stuff(handles);
    
    set(handles.button2clean,'String','Clean');
    set(handles.choosecleaned,'Value',1);
    
end;
isworking = 0;



% Executes when we press Fit button

% --- Executes on button press in button2fit.
function button2fit_Callback(hObject, eventdata, handles)
% hObject    handle to button2fit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% We will fit displayed image

global isworking;
global image_display;
global image_fitted;

if (~isworking)
    isworking = 1;
    set(handles.button2fit,'String','Working');

    axes(handles.image2display);
    [x,y] = ginput(1);
    cx = x(1);
    cy = y(1);


    % Read the tolerance value from the GUI
    tol = str2num(get(handles.readtolerance,'String'));


    % Here we do our fit. Output is a vector with gauss parameters
    p = fitgaussian2D(double(image_display),cx,cy,tol);


    % Parameters we got
    cx = p(1);
    cy = p(2);
    wx = p(3);
    wy = p(4);
    amp = p(5);
    theta = p(6);
    background=p(7);

    % Display them into the table in GUI
    set(handles.fitparameters,'Data',p')

    % This is for drawing fitted gaussian
    [Ny Nx] = size(image_display);
    [x,y] = meshgrid(1:Nx,1:Ny);


    % This takes parameters 'p' and produces fit array (same size as initial 'm' array)
    image_fitted = Gaussian2D(p,x,y);


    % Now we want to draw sections of our Gaussians
    % If we have some weird image and the center is outside of arrays, 
    % we do cross sections on the closest border
    % Need it because there will be an error if we try to access 
    % some data that is not in our arrays
    cx=check_bound(cx, 1, Nx);
    cy=check_bound(cy, 1, Ny);
    tcx = round(cx);
    tcy = round(cy);


    % Here we draw crossections
    hold off;
    axes(handles.ysectiondisplay);
    h = plot(image_display(:,tcx));
    hold on;
    set(h,'Color','red','LineWidth',2);
    h = plot(image_fitted(:,tcx));
    set(h,'Color','blue','LineWidth',2);

    hold off;
    axes(handles.xsectiondisplay);
    h = plot(image_display(tcy,:));
    set(h,'Color','red','LineWidth',2);
    hold on;
    h = plot(image_fitted(tcy,:));
    set(h,'Color','blue','LineWidth',2);
    hold off;

    set(handles.button2fit,'String','Fit');
    display_stuff(handles);

end;
isworking = 0;






%background subtraction
%here we only toggle on/off
% --- Executes on button press in button2subtract_backround.
function button2subtract_backround_Callback(hObject, eventdata, handles)
% hObject    handle to button2subtract_backround (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 button2subtract_backround

global image_original;
global background_m;
global image_display;
image_display = subtract_background(handles,image_original);
display_stuff(handles);







%image saving
% --- Executes on button press in button2save.
function button2save_Callback(hObject, eventdata, handles)
% hObject    handle to button2save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global image_display;
global default_data_path;
fname_to_save = horzcat(default_data_path,filesep,datestr(now, 'yyyy_mm_dd_HHMMSS'),'.png');
set(handles.picsavepath,'String',horzcat('Image saved to: ',fname_to_save));
image_to_save=uint16(image_display);
imwrite(image_to_save,fname_to_save,'png','bitdepth',16);






function draw_backround_image(handles)
global background_m;
[Ny, Nx] = size(background_m);
axes(handles.backgrounddisplay);
imagesc(background_m);
set(handles.backgrounddisplay,'PlotBoxAspectRatio',[Nx Ny 1],'DataAspectRatio',[1 1 1],'YTick',[],'XTick',[]);
display_stuff(handles);


% This executes when we want to grab the background from the camera
% --- Executes on button press in use_currently_displayed_as_background.
function use_currently_displayed_as_background_Callback(hObject, eventdata, handles)
% hObject    handle to use_currently_displayed_as_background (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global background_m;
global image_display;
background_m = image_display;
draw_backround_image(handles);



% This executes when we want to choose the background from a file
% --- Executes on button press in read_background_from_file.
function read_background_from_file_Callback(hObject, eventdata, handles)
% hObject    handle to read_background_from_file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global background_m;
img=choose_and_read_image_from_file();
if ~any(isnan(img))
    background_m = double(img);
    draw_backround_image(handles);
end


function img=choose_and_read_image_from_file()
global pathname;
global default_data_path;

% check if we have defined a pathname to look
if ~isstr(pathname) % undefined variables are 0
    pathname = default_data_path;
end;

% choose file to read
[filename, pathname, filterindex] = uigetfile( ...
{  '*.png','PNG-files (*.png)';},'DefaultName',pathname);

% read and update
if ~isequal(filename,0)
    img=double(imread(fullfile(pathname,filename)));
else
    img=NaN;
end;


function readshutterspeed_Callback(hObject, eventdata, handles)
% hObject    handle to readshutterspeed (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 readshutterspeed as text
%        str2double(get(hObject,'String')) returns contents of readshutterspeed as a double


% --- Executes during object creation, after setting all properties.
function readshutterspeed_CreateFcn(hObject, eventdata, handles)
% hObject    handle to readshutterspeed (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 readnumber_Callback(hObject, eventdata, handles)
% hObject    handle to readnumber (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 readnumber as text
%        str2double(get(hObject,'String')) returns contents of readnumber as a double


% --- Executes during object creation, after setting all properties.
function readnumber_CreateFcn(hObject, eventdata, handles)
% hObject    handle to readnumber (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 readtolerance_Callback(hObject, eventdata, handles)
% hObject    handle to readtolerance (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 readtolerance as text
%        str2double(get(hObject,'String')) returns contents of readtolerance as a double


% --- Executes during object creation, after setting all properties.
function readtolerance_CreateFcn(hObject, eventdata, handles)
% hObject    handle to readtolerance (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 during object creation, after setting all properties.
function image2display_CreateFcn(hObject, eventdata, handles)
% hObject    handle to image2display (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 image2display




function readstrength_Callback(hObject, eventdata, handles)
% hObject    handle to readstrength (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 readstrength as text
%        str2double(get(hObject,'String')) returns contents of readstrength as a double


% --- Executes during object creation, after setting all properties.
function readstrength_CreateFcn(hObject, eventdata, handles)
% hObject    handle to readstrength (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 readrange_Callback(hObject, eventdata, handles)
% hObject    handle to readrange (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 readrange as text
%        str2double(get(hObject,'String')) returns contents of readrange as a double


% --- Executes during object creation, after setting all properties.
function readrange_CreateFcn(hObject, eventdata, handles)
% hObject    handle to readrange (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


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over button2loadimage.
function button2loadimage_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to button2loadimage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --------------------------------------------------------------------
function workstylechoise_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to workstylechoise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function displaychoise_CreateFcn(hObject, eventdata, handles)
% hObject    handle to displaychoise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

%Choose imgae to display

% --- Executes when selected object is changed in displaychoise.
function displaychoise_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in displaychoise 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
global image_display;
global image_original;
global image_cleaned;
global image_fitted;
if get(handles.chooseoriginal,'Value')
    image_display=image_original;
    display_stuff(handles);
else
    if get(handles.choosecleaned,'Value')
        if image_cleaned == 0
            set(handles.chooseoriginal,'Value',1);
        else
            image_display=image_cleaned;
            display_stuff(handles);
        end;
    else
        if get(handles.choosefitted,'Value')
            if image_fitted == 0
                set(handles.chooseoriginal,'Value',1);
                if image_display == 0
                else
                    image_display = image_original;
                    display_stuff(handles);
                end;
            else
                image_display=image_fitted;
                display_stuff(handles);
            end;
        end;
    end;
end;


function [x,adjusted]=check_bound(x,xleft,xright)
% check if number out of bound and then assign it to the mid of the region
if xleft > xright
    % sanity check for bounds
    tmp=xright;
    xright=xleft;
    xleft=tmp;
end
if x<xleft || x>xright
    x=(xleft+xright)/2;
    adjusted = true;
else
    adjusted = false;
end


%To show the cross on the screen we need to get its coordinates
% --- Executes on button press in showcross.
function showcross_Callback(hObject, eventdata, handles)
% hObject    handle to showcross (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global cross_coordinates;
global image_display;
[Ny, Nx] = size(image_display);

if get(handles.showcross,'Value')
    [x0,y0]=ginput(1);
    x0=check_bound(x0, 1, Nx);
    y0=check_bound(y0, 1, Ny);
    x0=round(x0);
    y0=round(y0);
    cross_coordinates = [x0,y0];
else
    cross_coordinates = [NaN,NaN];
end;
display_stuff(handles);






% Hint: get(hObject,'Value') returns toggle state of showcross


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over button2fit.
function button2fit_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to button2fit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% vim: tabstop=4 shiftwidth=4 expandtab


% --- Executes on key press with focus on button2continuous_run and none of its controls.
function button2continuous_run_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to button2continuous_run (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)