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 17-Sep-2012 11:55:33 % 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 image_name; global isworking; global image_display; global image_original; global image_cleaned; global image_fitted; global image_cross; % Initial values background_m = 0; image_display = 0; image_original = 0; image_cleaned = 0; image_fitted = 0; image_name = 'test.png'; 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; % This funcion just calls the external program which takes an image from camera % We call it with parameters read from the GUI fields function grabimage(handles) global image_name; global image_display; global image_original; run_string = horzcat('grabNimages_1394cam.exe -s ',num2str(get(handles.readshutterspeed,'String')),' -n ',num2str(get(handles.readnumber,'String'))); [status,result] = system(run_string); img=imread(image_name); image_display=double(img); image_original=image_display; 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.button2substract,'Value') > 0 m = image_display - background_m; else m = image_display; end; % Read the tolerance value from the GUI tol = str2num(get(handles.readtolerance,'String')); % Display what we have before doing fit axes(handles.image2display); imagesc(m); set(handles.image2display,'PlotBoxAspectRatio',[4 3 1],'DataAspectRatio',[1 1 1]); drawnow; % 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.showcross,'Value',0); set(handles.chooseoriginal,'Value',1); if image_display > 0 image_display = image_original; display_stuff(handles); end; 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 an image from camera grabimage(handles); display_stuff(handles); end; % This executes when we do the continious run % Same as the single fit exept that we have a while cycle if get(handles.button2comtinuousrun,'Value') while get(handles.button2start,'Value') > 0 grabimage(handles); display_stuff(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; global image_cleaned; global image_fitted; image_cleaned = 0; image_fitted = 0; isworking =1; set(handles.chooseoriginal,'Value',1); set(handles.showcross,'Value',0); if image_display > 0 image_display = image_original; display_stuff(handles); end; % This calls a file-open dialog and runs the beam profiler on the choosen % file if get(handles.button2loadimage,'Value') set(handles.showcross,'Value',0); set(handles.button2start,'Value',1); set(handles.button2start,'BackgroundColor','Red'); set(handles.button2start,'String','STOP'); drawnow; if ~isstr(pathname) % undefined variables are 0 pathname = 'Z:\beam_profier images'; end; [filename, pathname, filterindex] = uigetfile( ... { '*.png','PNG-files (*.png)';},'DefaultName',pathname); if isequal(filename,0) else img=imread(fullfile(pathname,filename)); image_display=double(img); image_original=image_display; display_stuff(handles); end; set(handles.button2loadimage,'Value',0); set(handles.button2start,'BackgroundColor','Green'); set(handles.button2start,'Value',0); set(handles.button2start,'String','START'); end; 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 background subtraction is on if get(handles.button2substract,'Value') > 0 image_display = image_display - background_m; end; if (isworking == 1) else isworking = 1; set(handles.button2clean,'String','Working'); if get(handles.showcross,'Value') set(handles.showcross,'Value',0); image_display=image_display+image_negative; end; %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) global isworking; global image_display; global background_m; global image_fitted; global image_cross; if (isworking == 1) else isworking = 1; set(handles.button2fit,'String','Working'); axes(handles.image2display); [x,y] = ginput(1); cx = x(1); cy = y(1); % If we need to substract the background, we do it % background_m is a global variable if get(handles.button2substract,'Value') > 0 m = image_display - background_m; else m = image_display; end; % 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(m,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 [sizey sizex] = size(m); [x,y] = meshgrid(1:sizex,1:sizey); % This takes parameters 'p' and produces fit array (same size as initial 'm' array) fit = Gaussian2D(p,x,y); % Now we want to draw sections of our gaussians % If we have some wierd image and the center is outside of arrays, we do crossections on the closest border % Need it because there will be an error if we try to acces some data that is not in our arrays tcx = round(cx); if isnan(tcx) > 0 tcx = sizex/2; end; if round(cx) > sizex tcx = sizex; end; if round(cx) < 1 tcx = 1; end; tcy = round(cy); if isnan(tcy) > 0 tcy = sizey/2; end; if round(cy) > sizey tcy = sizey; end; if round(cy) < 1 tcy = 1; end; % Here we draw crossections hold off; axes(handles.ysectiondisplay); h = plot(m(:,tcx)); hold on; set(h,'Color','red','LineWidth',2); h = plot(fit(:,tcx)); set(h,'Color','blue','LineWidth',2); hold off; axes(handles.xsectiondisplay); h = plot(m(tcy,:)); set(h,'Color','red','LineWidth',2); hold on; h = plot(fit(tcy,:)); set(h,'Color','blue','LineWidth',2); hold off; %To display the intersection max1=max(image_display(:)); image_cross=zeros(sizey,sizex); image_cross(:,tcx) = max1; image_cross(tcy,:) = max1; %Here we plot the ideal fitted image for x=1:sizex for y=1:sizey Xn = (x-cx)*cos(theta) - (y-cy)*sin(theta); Yn = (x-cx)*sin(theta) + (y-cy)*cos(theta); image_fitted(y,x) = amp*(exp(-2*((Xn).^2./(wx^2)+(Yn).^2./(wy^2)))) + background; end; end; set(handles.button2fit,'String','Fit'); end; isworking = 0; %background substraction %here we only toggle on/off % --- Executes on button press in button2substract. function button2substract_Callback(hObject, eventdata, handles) % hObject handle to button2substract (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 button2substract if get(handles.button2substract,'Value') > 0 set(handles.button2substract,'String','ON'); else set(handles.button2substract,'String','OFF'); end; %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_name; global image_display; %fname_to_save = horzcat(pwd,filesep,'saved_images',filesep,datestr(now, 'yyyy_mm_dd_HHMMSS'),'.png'); fname_to_save = horzcat('Z:\beam_profiler images',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); % This executes when we want to grab the background from the camera % --- Executes on button press in Grabfromcamera. function Grabfromcamera_Callback(hObject, eventdata, handles) % hObject handle to Grabfromcamera (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; global isworking; isworking =1; grabimage(handles); background_m = image_display; axes(handles.backgrounddisplay); imagesc(background_m); set(handles.backgrounddisplay,'PlotBoxAspectRatio',[4 3 1],'DataAspectRatio',[1 1 1],'YTick',[],'XTick',[]); drawnow; isworking =0; % This executes when we want to choose the background from a file % --- Executes on button press in readfromfile. function readfromfile_Callback(hObject, eventdata, handles) % hObject handle to readfromfile (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; [filename, pathname, filterindex] = uigetfile( ... { '*.png','PNG-files (*.png)';}); if isequal(filename,0) else img=imread(fullfile(pathname,filename)); background_m = double(img); axes(handles.backgrounddisplay); imagesc(background_m); set(handles.backgrounddisplay,'PlotBoxAspectRatio',[4 3 1],'DataAspectRatio',[1 1 1],'YTick',[],'XTick',[]); drawnow; 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; set(handles.showcross,'Value',0); 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; %To show the cross on the screen % --- 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 image_display; global image_cross; global image_negative; global background_m; if get(handles.button2substract,'Value') > 0 image_display = image_display - background_m; end; [sizey sizex] = size(image_display); image_cross=image_display; if get(handles.showcross,'Value') if image_display == 0 set(handles.showcross,'Value',0) else [x0,y0]=ginput(1); x0=round(x0); y0=round(y0); max2 = max(image_display(:)); % Here we draw crossections hold off; axes(handles.ysectiondisplay); h = plot(image_display(:,x0)); hold on; set(h,'Color','red','LineWidth',2); hold off; axes(handles.xsectiondisplay); h = plot(image_display(y0,:)); set(h,'Color','red','LineWidth',2); %draw cross on the image for y=1:sizey image_cross(y,x0) = max(max2,image_display(y,x0)); end; for x=1:sizex image_cross(y0,x) = max(max2,image_display(y0,x)); end; image_negative = image_display-image_cross; image_display = image_cross; display_stuff(handles); end; else image_display=image_display+image_negative; display_stuff(handles); end; % 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) % --- Executes on key press with focus on button2comtinuousrun and none of its controls. function button2comtinuousrun_KeyPressFcn(hObject, eventdata, handles) % hObject handle to button2comtinuousrun (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)