From c52fd32ec913a8d8bf7b54c1ded489275b5915eb Mon Sep 17 00:00:00 2001 From: Eugeniy Mikhailov Date: Tue, 18 Sep 2012 17:07:22 -0400 Subject: gui background subtraction rewrked --- gui_win.m | 130 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 87 insertions(+), 43 deletions(-) diff --git a/gui_win.m b/gui_win.m index 9ec90e8..6409e2c 100644 --- a/gui_win.m +++ b/gui_win.m @@ -22,7 +22,7 @@ function varargout = gui_win(varargin) % Edit the above text to modify the response to help gui_win -% Last Modified by GUIDE v2.5 18-Sep-2012 14:56:06 +% Last Modified by GUIDE v2.5 18-Sep-2012 16:35:19 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; @@ -118,7 +118,7 @@ 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 +if get(handles.button2subtract_backround,'Value') > 0 % m = image_display - background_m; else % m = image_display; @@ -139,10 +139,7 @@ if get(handles.showcross,'Value') end - - - -function display_cross_marks_and_cross_sections(handles) +function show_cross_marks(handles) global cross_coordinates; global image_display; [Ny, Nx] = size(image_display); @@ -164,38 +161,91 @@ else plot(vline_x, vline_y, 'LineWidth', cross_line_width, 'Color', cross_color); end + +function [hsection, vsection] = get_cross_sections_of_image(img,x0,y0) % define actual image cross sections -if any( isnan(cross_coordinates) ) - % we just assign bunch of zeros for dummy cross sections - vsection = image_display(:,1)*NaN; - hsection = image_display(1,:)*NaN; +[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 - x0=cross_coordinates(1); - y0=cross_coordinates(2); % define displayed image cross section - vsection = image_display(:,x0); - hsection = image_display(y0,:); + vsection = img(:,x0); + hsection = img(y0,:); end + +function plot_cross_section(handles, hsection, vsection, color, label) % draw above cross sections % vertical axes(handles.ysectiondisplay); -hold off; h = plot(vsection); -set(h,'Color','red','LineWidth',2); +set(h,'Color',color,'LineWidth',2); +xlim('auto'); +ylim('auto'); +hold on % horizontal axes(handles.xsectiondisplay); -hold off; h = plot(hsection); -set(h,'Color','red','LineWidth',2); +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 = grabimage(shutter_time, Naverages); -image_display = image_original; +image_original = double(grabimage(shutter_time, Naverages)); +image_display = subtract_background(handles,image_original); display_stuff(handles); @@ -266,16 +316,8 @@ 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); -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 @@ -289,13 +331,14 @@ if get(handles.button2loadimage,'Value') 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.button2loadimage,'Value',0); set(handles.button2start,'BackgroundColor','Green'); set(handles.button2start,'Value',0); set(handles.button2start,'String','START'); + drawnow; end; isworking =0; @@ -429,23 +472,21 @@ isworking = 0; -%background substraction +%background subtraction %here we only toggle on/off -% --- Executes on button press in button2substract. -function button2substract_Callback(hObject, eventdata, handles) -% hObject handle to button2substract (see GCBO) +% --- 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 button2substract +% Hint: get(hObject,'Value') returns toggle state of button2subtract_backround -if get(handles.button2substract,'Value') > 0 - - set(handles.button2substract,'String','ON'); - -else - set(handles.button2substract,'String','OFF'); -end; +global image_original; +global background_m; +global image_display; +image_display = subtract_background(handles,image_original); +display_stuff(handles); @@ -524,7 +565,7 @@ end; % read and update if ~isequal(filename,0) - img=imread(fullfile(pathname,filename)); + img=double(imread(fullfile(pathname,filename))); else img=NaN; end; @@ -718,7 +759,7 @@ else end; -function x=check_bound(x,xleft,xright) +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 @@ -728,6 +769,9 @@ if xleft > xright end if xxright x=(xleft+xright)/2; + adjusted = true; +else + adjusted = false; end -- cgit v1.2.3