diff options
author | Eugeniy Mikhailov <evgmik@gmail.com> | 2012-09-18 15:31:22 -0400 |
---|---|---|
committer | Eugeniy Mikhailov <evgmik@gmail.com> | 2012-09-18 15:31:22 -0400 |
commit | 9994dc85ac594d04c1c0d348e948dcd6c78bd36c (patch) | |
tree | 9590b55b7337d9dfac9da7cf592e708360579386 | |
parent | 4633f3a8e9660201525ee822818981de7c0c7c5a (diff) | |
download | beam_profiler-9994dc85ac594d04c1c0d348e948dcd6c78bd36c.tar.gz beam_profiler-9994dc85ac594d04c1c0d348e948dcd6c78bd36c.zip |
Fitting callback cleaned up
-rw-r--r-- | gui_win.m | 165 |
1 files changed, 57 insertions, 108 deletions
@@ -314,14 +314,8 @@ 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 +if (~isworking) isworking = 1; set(handles.button2clean,'String','Working'); %read the attenuation from the GUI @@ -350,128 +344,83 @@ function button2fit_Callback(hObject, eventdata, handles) % 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 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); +if (~isworking) + isworking = 1; + set(handles.button2fit,'String','Working'); -% Display them into the table in GUI -set(handles.fitparameters,'Data',p') + axes(handles.image2display); + [x,y] = ginput(1); + cx = x(1); + cy = y(1); -% This is for drawing fitted gaussian -[sizey sizex] = size(m); -[x,y] = meshgrid(1:sizex,1:sizey); + % Read the tolerance value from the GUI + tol = str2num(get(handles.readtolerance,'String')); -% This takes parameters 'p' and produces fit array (same size as initial 'm' array) -fit = Gaussian2D(p,x,y); + % Here we do our fit. Output is a vector with gauss parameters + p = fitgaussian2D(double(image_display),cx,cy,tol); -% 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; + % 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') -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; + % 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); -% 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; + % 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); -%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; + % 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); -set(handles.button2fit,'String','Fit'); end; isworking = 0; |