From 3913275e25a0d76c7897ed44cfb0e6685ed0dca5 Mon Sep 17 00:00:00 2001 From: Shanmukha Innovations Date: Thu, 26 Apr 2018 11:08:39 +0000 Subject: [PATCH] Commit of MATLAB file for cytocube image acquisition app. --- IAD_v_2_3.m | 1197 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1197 insertions(+) create mode 100644 IAD_v_2_3.m diff --git a/IAD_v_2_3.m b/IAD_v_2_3.m new file mode 100644 index 0000000..3fcfd75 --- /dev/null +++ b/IAD_v_2_3.m @@ -0,0 +1,1197 @@ +function varargout = IAD_v_2_3(varargin) +% IAD_V_2_3 MATLAB code for IAD_v_2_3.fig +% IAD_V_2_3, by itself, creates a new IAD_V_2_3 or raises the existing +% singleton*. +% +% H = IAD_V_2_3 returns the handle to a new IAD_V_2_3 or the handle to +% the existing singleton*. +% +% IAD_V_2_3('CALLBACK',hObject,eventData,handles,...) calls the local +% function named CALLBACK in IAD_V_2_3.M with the given input arguments. +% +% IAD_V_2_3('Property','Value',...) creates a new IAD_V_2_3 or raises the +% existing singleton*. Starting from the left, property value pairs are +% applied to the GUI before IAD_v_2_3_OpeningFcn gets called. An +% unrecognized property name or invalid value makes property application +% stop. All inputs are passed to IAD_v_2_3_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 IAD_v_2_3 + +% Last Modified by GUIDE v2.5 12-Aug-2017 18:51:51 + +% Begin initialization code - DO NOT EDIT +gui_Singleton = 1; +gui_State = struct('gui_Name', mfilename, ... + 'gui_Singleton', gui_Singleton, ... + 'gui_OpeningFcn', @IAD_v_2_3_OpeningFcn, ... + 'gui_OutputFcn', @IAD_v_2_3_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 + +% --- Outputs from this function are returned to the command line. +function varargout = IAD_v_2_3_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; + + + +% --- Executes just before IAD_v_2_3 is made visible. +function IAD_v_2_3_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 IAD_v_2_3 (see VARARGIN) + +% Choose default command line output for IAD_v_2_3 +handles.output = hObject; + +% Update handles structure +guidata(hObject, handles); + + +% Choose default command line output for Cytocube_v1_4 +handles.output = hObject; +disp('Booting.............'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Start serial port +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +s = serial('COM3'); +set(s,'BaudRate',57600); set(s,'Terminator','#'); +set(s,'Timeout',30); % s.RecordName = 'CytoSerialTxnLog.txt'; +fopen(s); % record(s); +handles.s = s; + +msg=strcat(datestr(clock,'yyyy-mm-dd-HHMM'),'m',datestr(clock,'ss'),'s'); +fid = fopen(strcat(msg,'.txt'),'w'); +handles.fid = fid; +if (fid == -1) + disp('could not open logging file. You probably need to change working directory to the one where the MATLAB file is located.'); + return; +end +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Start cam +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +tic +fprintf(fid, '\r\nStarting camera...'); +% choose which webcam (winvideo-1) and which mode (YUY2_176x144) +%vid = videoinput('winvideo', 1, 'RGB32_744x480'); +vid = videoinput('gentl', 1, 'BGRA8Packed'); +% Configure the object for manual trigger mode. +triggerconfig(vid, 'manual'); +% only capture one frame per trigger, we are not recording a video +vid.FramesPerTrigger = 1; +% output would image in RGB color space +vid.ReturnedColorspace = 'rgb'; +% tell matlab to start the webcam on user request, not automatically +triggerconfig(vid, 'manual'); +% we need this to know the image height and width +vidRes = get(vid, 'VideoResolution'); +% image width +imWidth = vidRes(1); +% image height +imHeight = vidRes(2); +% number of bands of our image (should be 3 because it's RGB) +nBands = get(vid, 'NumberOfBands'); +% create an empty image container and show it on axPreview +hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axPreview); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% do auto white balance +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +src = getselectedsource(vid); +src.BalanceWhiteAuto = 'Continuous'; +% begin the webcam preview +preview(vid, hImage); +% expose the objects so we can handle them in other functions +handles.vid = vid; +handles.src = src; +guidata(hObject,handles); +%start(vid); +drawnow; + +% set and display the exposure +src.ExposureTime = 360; +set(handles.expDisp,'string', src.ExposureTime ); +Time_to_init_cam = toc + +% clear the message +set(handles.camwait,'string',' '); + +% setup the timer for pos update +tmr = timer('ExecutionMode', 'fixedSpacing','Period',1,'TimerFcn', {@timerCallback,handles}); +start(tmr); handles.tmr = tmr; + +% this is the slide name +setslidename = 'Slide'; +handles.setslidename = setslidename; + +%start(vid); + +% Update handles structure +guidata(hObject, handles); + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% The timer function +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +function timerCallback( ~,~,handles )%varargin) ~,struct2,~ +try + query_X(handles); query_Y(handles); query_Z(handles); +catch + %disp('timer error'); +end + + +% --- Executes on button press in pushbutton1. +function pushbutton1_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +fid=handles.fid; src = handles.src; +set(handles.camwait,'string','Updating exposure value...'); +fprintf(fid, '\r\nUpdating exposure value...'); +newExp = get(handles.camExpEdit,'string'); +numVal = str2num(newExp); src.ExposureTime = numVal; +set(handles.expDisp,'string',newExp); +fprintf(fid, '\r\nExposure value updated to %d microsecond',numVal); +set(handles.camwait,'string','Camera exposure changed.'); + + +function camExpEdit_Callback(hObject, eventdata, handles) +% hObject handle to camExpEdit (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 camExpEdit as text +% str2double(get(hObject,'String')) returns contents of camExpEdit as a double + + +% --- Executes during object creation, after setting all properties. +function camExpEdit_CreateFcn(hObject, eventdata, handles) +% hObject handle to camExpEdit (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 pushbutton2. +function pushbutton2_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton2 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + fullAFvar = fullAF(handles,6000); + +% --- Executes on button press in pushbutton3. +function pushbutton3_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton3 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; fprintf(s, 'B'); fprintf(s, '0000000'); + + +function edit10_Callback(hObject, eventdata, handles) +% hObject handle to edit10 (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 edit10 as text +% str2double(get(hObject,'String')) returns contents of edit10 as a double + + +% --- Executes during object creation, after setting all properties. +function edit10_CreateFcn(hObject, eventdata, handles) +% hObject handle to edit10 (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 pushbutton4. +function pushbutton4_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton4 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; +% format the number properly -------------------------------------------- + str = get(handles.edit10,'string') ; + check = str2double(str); + % ensure that too big values dont go in + if (check < -250000) + check = -250000; + elseif (check > 250000) + check = 250000; + end; + + str = sprintf('%06d',abs(check)); % abs needed so that no +&- in neg numbers + + if(check < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end; +% end formatting ---------- -------------------------------------------- + fprintf(s,'B'); + fprintf(s, newstr ); + + + +% --- Executes on slider movement. +function slider1_Callback(hObject, eventdata, handles) +% hObject handle to slider1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +sliderVal = get(hObject,'Value'); set(handles.edit10,'string', sliderVal ); + + + +% --- Executes during object creation, after setting all properties. +function slider1_CreateFcn(hObject, eventdata, handles) +% hObject handle to slider1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles empty - handles not created until after all CreateFcns called + +% Hint: slider controls usually have a light gray background. +if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) + set(hObject,'BackgroundColor',[.9 .9 .9]); +end +set(hObject,'Max',250000,'Min',-250000) + + +% --- Executes on button press in pushbutton5. +function pushbutton5_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton5 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; fprintf(s, 'N'); fprintf(s, '0000000'); + + +function edit11_Callback(hObject, eventdata, handles) +% hObject handle to edit11 (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 edit11 as text +% str2double(get(hObject,'String')) returns contents of edit11 as a double + + +% --- Executes during object creation, after setting all properties. +function edit11_CreateFcn(hObject, eventdata, handles) +% hObject handle to edit11 (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 pushbutton6. +function pushbutton6_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton6 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; +% format the number properly -------------------------------------------- + str = get(handles.edit11,'string'); check = str2double(str); + % ensure that too big values dont go in + if (check < -250000) + check = -250000; + elseif (check > 250000) + check = 250000; + end; + + str = sprintf('%06d',abs(check)); % abs needed so that no +&- in neg numbers + + if(check < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end; +% end formatting ---------- -------------------------------------------- + fprintf(s,'N'); fprintf(s, newstr ); + +% --- Executes on slider movement. +function slider2_Callback(hObject, eventdata, handles) +% hObject handle to slider2 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +sliderVal = sprintf('%06d',get(hObject,'Value')); +set(handles.edit11,'string', sliderVal ); + + +% --- Executes during object creation, after setting all properties. +function slider2_CreateFcn(hObject, eventdata, handles) +% hObject handle to slider2 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles empty - handles not created until after all CreateFcns called + +% Hint: slider controls usually have a light gray background. +if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) + set(hObject,'BackgroundColor',[.9 .9 .9]); +end + + +% --- Executes on button press in pushbutton7. +function pushbutton7_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton7 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; fprintf(s, 'M'); fprintf(s, '0000000'); + + +function edit12_Callback(hObject, eventdata, handles) +% hObject handle to edit12 (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 edit12 as text +% str2double(get(hObject,'String')) returns contents of edit12 as a double + + +% --- Executes during object creation, after setting all properties. +function edit12_CreateFcn(hObject, eventdata, handles) +% hObject handle to edit12 (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 pushbutton8. +function pushbutton8_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton8 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; +% format the number properly -------------------------------------------- + str = get(handles.edit12,'string'); check = str2double(str); + % ensure that too big values dont go in + if (check < -50000) + check = -50000; + elseif (check > 250000) + check = 250000; + end; + + str = sprintf('%06d',abs(check)); % abs needed so that no +&- in neg numbers + + if(check < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end; +% end formatting ---------- -------------------------------------------- + fprintf(s,'M'); fprintf(s, newstr ); + +% --- Executes on slider movement. +function slider3_Callback(hObject, eventdata, handles) +% hObject handle to slider3 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +sliderVal = sprintf('%06d',get(hObject,'Value')); +set(handles.edit12,'string', sliderVal ); + + +% --- Executes during object creation, after setting all properties. +function slider3_CreateFcn(hObject, eventdata, handles) +% hObject handle to slider3 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles empty - handles not created until after all CreateFcns called + +% Hint: slider controls usually have a light gray background. +if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) + set(hObject,'BackgroundColor',[.9 .9 .9]); +end + + +% --- Executes on button press in pushbutton9. +function pushbutton9_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton9 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + s = handles.s; vid = handles.vid ; fid=handles.fid; resetbutton=handles.resetbutton; + set(handles.camwait,'string','Custom area scan started.'); + + tmr = handles.tmr; stop(tmr); + setslidename = handles.setslidename; + fprintf(fid, '\r\nStarting area scan...'); + XFoVSize = 1823; YFoVSize = 2323; + XPulseRate = 38400; YPulseRate = 38400 ; + Xzadjustdelay = 0.002; + autoContrTime = 0.1; + + XImDelay = (XFoVSize / XPulseRate) ; + YImDelay = (YFoVSize / YPulseRate) ; + + % send command to Arduino + fprintf(s,'Z'); + + % get the co-ods + xA = str2double(handles.xA); xB = str2double(handles.xB); + yC = str2double(handles.yC); yB = str2double(handles.yB); + + % logging co-ordinates + logline = strcat('\r\n Going from X co-od ',handles.xA,' to ',handles.xB); + fprintf(fid, logline); + logline = strcat('\r\n Going from Y co-od ',handles.yB,' to ',handles.yC); + fprintf(fid, logline); + + % find num of FoV + numofX = (xB - xA) / XFoVSize; + numofY = (yC - yB) / YFoVSize; + + % wait for the scan to begin + while fscanf(s) ~= 'S' + end; + % start counting time + tic; + + % NOW TAKING PICTURES + rowcount = 0; + filecount = 0; + + msg = '\r\n Starting image capture ...'; + fprintf(fid, msg); + + % Dircn = false; %We believe we start from top + while(rowcount <= numofY) + while(filecount <= numofX) + pause(XImDelay); + snapshot=getsnapshot(vid); + %enh_snapshot=autocontrast(snapshot); + filename = strcat(setslidename,'_',num2str(rowcount),'-',num2str(filecount), '.bmp'); + %imwrite(enh_snapshot,filename); + imwrite(snapshot,filename); + filecount = filecount + 1; + end + % wait for shift by one FoV in Y direction + filecount = 0; + pause(YImDelay); + rowcount = rowcount + 1; + end + % display time elapsed + time_to_scan_area = toc +%numofY*numofX + msg = sprintf('\r\nImages captured : %d x %d', numofY,numofX); + fprintf(fid, msg); + start(tmr); +set(handles.camwait,'string','Custom area scan completed.'); + +% --- Executes on button press in pushbutton10. +function pushbutton10_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton10 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; vid = handles.vid ; fid=handles.fid; resetbutton=handles.resetbutton; +tmr = handles.tmr; stop(tmr); +tic +setslidename = char(handles.setslidename); + +set(handles.camwait,'string','Auto WSI started.'); +fprintf(fid, '\r\nAuto WSI started.'); + +XFoVSize = 350; YFoVSize = 450; +XPulseRate = 3800 ; YPulseRate = 3800 ; % MICRO METER PER SEC +Xzadjustdelay = 0.002; +autoContrTime = 0.1; +XImDelay = (XFoVSize / XPulseRate) * 0.45; % reduction of 60% needed to compensate for MATLAB processing delays +YImDelay = (YFoVSize / YPulseRate) * 0.45; + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% go home first. Send command and wait for sync +fprintf(s,'Q'); +while fscanf(s) ~= 'S' +end; + +%Af at origin and set this z as zero +%fullAF(handles,16000); fprintf(s,'L'); +disp('let go'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Locations of points : +% A: +% B: +% C: +% D: +% E: +% F: +% G: +% H: +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % GoTo POINTS, AF and Store var + gotoX(50505,handles); gotoY(25250,handles); handles.Avar = fullAF(handles,16000); + fprintf(s,'E'); + gotoX(126260,handles); handles.Bvar = fullAF(handles,16000); + fprintf(s,'R'); + gotoX(202020,handles); handles.Cvar = fullAF(handles,16000); + fprintf(s,'Y'); + + % Update handles structure + guidata(hObject,handles); + + % Take another value and find if it is > the smallest of the three. If yes, replace it + gotoX(166670,handles); gotoY(50505,handles); Xvar = fullAF(handles,16000); YourImage=getsnapshot(vid); + if( mean(YourImage(:)) < 230) + bestpoints(hObject,handles, Xvar); + end + gotoX(90910,handles); Xvar = fullAF(handles,16000); YourImage=getsnapshot(vid); + if( mean(YourImage(:)) < 230) + bestpoints(hObject,handles, Xvar); + end + gotoX(50505,handles); gotoY(75750,handles); Xvar =fullAF(handles,16000); YourImage=getsnapshot(vid); + if( mean(YourImage(:)) < 230) + bestpoints(hObject,handles, Xvar); + end + gotoX(126260,handles); Xvar =fullAF(handles,16000); YourImage=getsnapshot(vid); + if( mean(YourImage(:)) < 230) + bestpoints(hObject,handles, Xvar); + end + gotoX(202020,handles); Xvar =fullAF(handles,16000); YourImage=getsnapshot(vid); + if( mean(YourImage(:)) < 230) + bestpoints(hObject,handles, Xvar); + end +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +disp('The time for 8 pt AF is'); +toc + +% now asking Arduino to do WSI +fprintf(s,'F'); + +% logging co-ordinates +fprintf(fid, '\r\n Going to Origin and starting WSI '); + +% find num of FoV +numofX = 50000 / XFoVSize; numofY = 25000 / YFoVSize; + +% wait for the scan to begin +while fscanf(s) ~= 'S' + disp('waiting'); +end; + +% NOW TAKING PICTURES +rowcount = 0; +filecount = 0; + + msg = '\r\n Starting image capture ...'; + fprintf(fid, msg); + +while(rowcount <= numofY) + while(filecount <= numofX) + % take image and save + %pause(XImDelay+Xzadjustdelay+autoContrTime); + pause(XImDelay); + snapshot=getsnapshot(vid); + %enh_snapshot=autocontrast(snapshot); + filename = strcat(setslidename,'_',num2str(rowcount),'-',num2str(filecount), '.bmp'); + imwrite(snapshot,filename); + filecount = filecount + 1; + end + % wait for shift by one FoV in Y direction + filecount = 0; + pause(YImDelay); + rowcount = rowcount + 1; +end +set(handles.camwait,'string','Auto WSI completed.'); +msg = sprintf('\r\nImages captured : %d x %d', numofY,numofX); +fprintf(fid, msg); +toc + + + +function pushtoimage_Callback(hObject, eventdata, handles) +% hObject handle to pushtoimage (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +disp('unused func'); + +% --- Executes on button press in pushbutton11. +function pushbutton11_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton11 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; +xA = get(handles.currxpos,'string'); handles.xA = xA; +yA = get(handles.currypos,'string'); handles.yA = yA; +zA = get(handles.currzpos,'string'); handles.zA = zA; +data = strcat(xA,' , ',yA,' , ',zA); set(handles.xyzA,'string',data); +fprintf(s,'E'); +guidata(hObject, handles); + + +% --- Executes on button press in pushbutton12. +function pushbutton12_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton12 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; + +xB = get(handles.currxpos,'string'); handles.xB = xB; +yB = get(handles.currypos,'string'); handles.yB = yB; +zB = get(handles.currzpos,'string'); handles.zB = zB; + +data = strcat(xB,',',yB,',',zB); set(handles.xyzB,'string',data); + +% send command that sets pt B in Arduino +fprintf(s,'R'); guidata(hObject, handles); + +% --- Executes on button press in pushD. +function pushD_Callback(hObject, eventdata, handles) +% hObject handle to pushD (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; + +xD = get(handles.currxpos,'string'); handles.xD = xD; +yD = get(handles.currypos,'string'); handles.yD = yD; +zD = get(handles.currzpos,'string'); handles.zD = zD; + +data = strcat(xD,',',yD,',',zD); set(handles.xyzD,'string',data); + +% send command that sets pt D in Arduino +fprintf(s,'U'); guidata(hObject, handles); + +% --- Executes on button press in pushC. +function pushC_Callback(hObject, eventdata, handles) +% hObject handle to pushC (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; + +xC = get(handles.currxpos,'string'); handles.xC = xC; +yC = get(handles.currypos,'string'); handles.yC = yC; +zC = get(handles.currzpos,'string'); handles.zC = zC; + +data = strcat(xC,',',yC,',',zC); set(handles.xyzC,'string',data); + +% send command that sets pt C in Arduino +fprintf(s,'Y'); guidata(hObject, handles); + + +function editboxslidename_Callback(hObject, eventdata, handles) +% hObject handle to editboxslidename (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 editboxslidename as text +% str2double(get(hObject,'String')) returns contents of editboxslidename as a double + + +% --- Executes during object creation, after setting all properties. +function editboxslidename_CreateFcn(hObject, eventdata, handles) +% hObject handle to editboxslidename (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 pushbutton15. +function pushbutton15_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton15 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +newName = get(handles.editboxslidename,'string'); +handles.setslidename = newName; + +% Update handles structure +guidata(hObject, handles); + +% --- Executes on button press in pushbutton16. +function pushbutton16_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton16 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +vid = handles.vid; +% calc var +newimg = double(getsnapshot(vid)); newvar=var(newimg(:)); +%disp on the window +set(handles.var_disp,'string',newvar); + +% --- Executes on button press in pushbutton17. +function pushbutton17_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton17 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +vid = handles.vid; s = handles.s; +% Move Z up +fprintf(s,'T'); +% calc var +newimg = double(getsnapshot(vid)); newvar=var(newimg(:)); +%disp on the window +set(handles.var_disp,'string',newvar); + +% --- Executes on button press in pushbutton18. +function pushbutton18_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton18 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +vid = handles.vid; s = handles.s; +% Move Z down +fprintf(s,'G'); +% calc var +newimg = double(getsnapshot(vid)); newvar=var(newimg(:)); +%disp on the window +set(handles.var_disp,'string',newvar); + +% --- Executes on button press in pushbutton19. +function pushbutton19_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton19 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + vid=handles.vid; + snapshot=getsnapshot(vid); +% % enh_snapshot=autocontrast(snapshot); + imwrite(snapshot,'snap.jpg'); + +% --- Executes on button press in pushbutton20. +function pushbutton20_Callback(hObject, eventdata, handles) +% this is the debug code +% hObject handle to pushbutton20 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; src = handles.src; vid=handles.vid; tmr = handles.tmr; + +stop(tmr); + +setslidename = '1kFS_point6_'; +set(handles.camwait,'string','Starting Focal stack acquisition'); +% take a focal stack +currzpos = str2double(get(handles.currzpos,'string')) +stackcount=500; filecount=0; + +% take FS going -Z (down) +while(filecount <= stackcount) + snapshot=getsnapshot(vid); + filename = strcat(setslidename,'_down_',num2str(filecount), '.jpg'); + imwrite(snapshot,filename); + filecount = filecount + 1; + + newZ = currzpos - (10*filecount) ; + if(newZ < 0) + newZ = strcat('-',sprintf('%06d',abs(newZ ))); + else + newZ = strcat('+',sprintf('%06d',abs(newZ ))); + end; + fprintf(s,'M'); fprintf(s, newZ ); pause(0.1); +end + +% return to home +newZ = currzpos +if(newZ < 0) + newZ = strcat('-',sprintf('%06d',abs(newZ ))); + else + newZ = strcat('+',sprintf('%06d',abs(newZ ))); + end; +fprintf(s,'M'); fprintf(s, newZ ); pause(1); +filecount=0; + +% take FS going +Z (up) +while(filecount <= stackcount) + snapshot=getsnapshot(vid); + filename = strcat(setslidename,'_up_',num2str(filecount), '.jpg'); + imwrite(snapshot,filename); + filecount = filecount + 1; + + newZ = currzpos + (10*filecount) ; + if(newZ < 0) + newZ = strcat('-',sprintf('%06d',abs(newZ ))); + else + newZ = strcat('+',sprintf('%06d',abs(newZ ))); + end; + fprintf(s,'M'); fprintf(s, newZ ); pause(0.1); +end + +set(handles.camwait,'string','Focal stack acquisition completed'); +start(tmr); +return; + + + +% --- Executes on button press in pushbutton21. +function pushbutton21_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton21 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + s = handles.s; fprintf(s,'W'); + +% --- Executes on button press in pushbutton22. +function pushbutton22_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton22 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + s = handles.s; fprintf(s,'S'); + +% --- Executes on button press in pushbutton23. +function pushbutton23_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton23 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + s = handles.s; fprintf(s,'D'); + +% --- Executes on button press in pushbutton24. +function pushbutton24_Callback(hObject, eventdata, handles) +% hObject handle to pushbutton24 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + s = handles.s; fprintf(s,'A'); + +% --- Executes when user attempts to close figure1. +function figure1_CloseRequestFcn(hObject, eventdata, handles) +% hObject handle to figure1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +% Hint: delete(hObject) closes the figure +delete(hObject); + + +function [] = query_X(handles) +s = handles.s; fwrite(s,'I'); data = fgetl(s); + +if(data == 'X') + set(handles.currxpos,'string',fgetl(s)); +else + data = fgetl(s); disp(sprintf('not X data - %s',data)); +end + +function [] = query_Y(handles) +s = handles.s; fwrite(s,'O'); data = fgetl(s); + +if(data == 'Y') + set(handles.currypos,'string',fgetl(s)); +else + data = fgetl(s); disp(sprintf('not Y data - %s',data)); +end + +function [] = query_Z(handles) +s = handles.s; fwrite(s,'P'); data = fgetl(s); + +if(data == 'Z') + set(handles.currzpos,'string',fgetl(s)); +else + data = fgetl(s); disp(sprintf('not Y data - %s',data)); +end + + + +% AUTOCONTRAST Automatically adjusts contrast of images to optimum level. +% e.g. autocontrast('Sunset.jpg','Output.jpg') + +function output_img = autocontrast(input_img) + +low_limit=0.004; +up_limit=0.995; +img=input_img; +[m1 n1 r1]=size(img); +img=double(img); +%--------------------calculation of vmin and vmax---------------------- +for k=1:r1 + arr=sort(reshape(img(:,:,k),m1*n1,1)); + v_min(k)=arr(ceil(low_limit*m1*n1)); + v_max(k)=arr(ceil(up_limit*m1*n1)); +end +%---------------------------------------------------------------------- +if r1==3 + v_min=rgb2ntsc(v_min); + v_max=rgb2ntsc(v_max); +end +%---------------------------------------------------------------------- +img=(img-v_min(1))/(v_max(1)-v_min(1)); +output_img = uint8(img.*255); + + + +function gotoX(check,handles) +s=handles.s; + %%%%%%% check if Arduino is responsive and if not, wait %%%%%% + fprintf(s,'7'); while fscanf(s) ~= '7' + end + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +str = sprintf('%06d',abs(check)); % abs needed so that no +&- in neg numbers + % ensure that too big values dont go in + if (check < -750000) check = -750000; + elseif (check > 750000) check = 750000; + end; + + if(check < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end; + % end formatting ---------- -------------------------------------------- + fprintf(s,'B'); fprintf(s, newstr ); + +function gotoY(check,handles) +s=handles.s; + %%%%%%% check if Arduino is responsive and if not, wait %%%%%% + fprintf(s,'7'); while fscanf(s) ~= '7' + end + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +str = sprintf('%06d',abs(check)); % abs needed so that no +&- in neg numbers + % ensure that too big values dont go in + if (check < -750000) check = -750000; + elseif (check > 750000) check = 750000; + end; + + if(check < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end; + % end formatting ---------- -------------------------------------------- + fprintf(s,'N'); fprintf(s, newstr ); + +function gotoZ(check,handles) +s=handles.s; + %%%%%%% check if Arduino is responsive and if not, wait %%%%%% + fprintf(s,'7'); while fscanf(s) ~= '7' + end + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +str = sprintf('%06d',abs(check)); % abs needed so that no +&- in neg numbers + % ensure that too big values dont go in + if (check < -30000) check = -30000; + elseif (check > 30000) check = 30000; + end; + + if(check < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end; + % end formatting ---------- -------------------------------------------- + fprintf(s,'M'); fprintf(s, newstr ); pause(0.035); + + +function [currmaxvar] = fullAF(handles,travel) +tmr=handles.tmr; + toTurnOnOrNot=1; + if strcmp(get(tmr,'Running'),'off') + toTurnOnOrNot=0; + else + stop(tmr); + end + +%tic +set(handles.camwait,'string','Autofocus in progress...'); +vid=handles.vid; fid=handles.fid; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% CRUDE AF. +Z_travel_for_crude = travel; crude_step_count = 200; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +crude_pulse_count = Z_travel_for_crude / crude_step_count; +crude_curr_var=0; crude_max_var=0; crude_loc_max_var=0; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% go to starting Z loc (we've hit limit switch via arduino already so its z=0) +crude_start_loc = str2double(get(handles.currzpos,'string')) - (Z_travel_for_crude/2); +gotoZ(crude_start_loc,handles); pause(0.2); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% now keep going up and taking var +crude_curr_loc = crude_start_loc; + +for i=1: crude_pulse_count + % variance + crude_img = double(getsnapshot(vid)); crude_curr_var=var(crude_img(:)); + + if (crude_curr_var > crude_max_var) + crude_max_var = crude_curr_var; + crude_loc_max_var = crude_curr_loc; + end + % update loc + crude_curr_loc = crude_curr_loc + crude_step_count ; + gotoZ(crude_curr_loc,handles); +end +gotoZ(crude_loc_max_var,handles); +pause(1); +%snapshot=getsnapshot(vid); imwrite(snapshot,'aftercrude.bmp'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% fINECRUDE AF. +Z_travel_for_crude = 2000; crude_step_count = 50; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +crude_pulse_count = Z_travel_for_crude / crude_step_count; +crude_curr_var=0; crude_max_var=0; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% go to starting Z loc (we've hit limit switch via arduino already so its z=0) +crude_start_loc = crude_loc_max_var - (Z_travel_for_crude/2); +gotoZ(crude_start_loc,handles); pause(0.2); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% now keep going up and taking var +crude_curr_loc = crude_start_loc; + +for i=1: crude_pulse_count + % variance + crude_img = double(getsnapshot(vid)); crude_curr_var=var(crude_img(:)); + + if (crude_curr_var > crude_max_var) + crude_max_var = crude_curr_var; + crude_loc_max_var = crude_curr_loc; + end + % update loc + crude_curr_loc = crude_curr_loc + crude_step_count ; + gotoZ(crude_curr_loc,handles); +end +gotoZ(crude_loc_max_var,handles); +pause(1); +%snapshot=getsnapshot(vid); imwrite(snapshot,'afterfinecrude.bmp'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% The fine AF begins here +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +Ztotaltravel = 200; final_go_backpulses = 0 ; +currZ = crude_loc_max_var; + +% first go above +start_above_loc = currZ - (Ztotaltravel/2); +gotoZ(start_above_loc,handles); +pause(1); +% now build the var array +localAF_loc_array = zeros(1,Ztotaltravel); localAF_var_array = zeros(1,Ztotaltravel); +localAF_curr_loc = start_above_loc; +for q=1:(Ztotaltravel/5) + % location + localAF_loc_array(q) = localAF_curr_loc; + % variance + localAF_img = double(getsnapshot(vid)); localAF_var_array(q)=var(localAF_img(:)); + + % move and update loc + localAF_curr_loc = localAF_curr_loc + 5; gotoZ(localAF_curr_loc,handles); +end + +% Finally go to the max var location +index_of_max_var = find(localAF_var_array == max(localAF_var_array)); +loc_of_max_var = localAF_loc_array(index_of_max_var) - final_go_backpulses ; + +gotoZ(loc_of_max_var,handles); +fprintf(fid,'\r\nCrude/Fine AF (var): %d / %d (%d)',crude_loc_max_var,loc_of_max_var,max(localAF_var_array)); +currmaxvar = max(localAF_var_array) ; +% toc % loc_of_max_var +set(handles.camwait,'string','AF done.'); + +if toTurnOnOrNot ==1 + start(tmr); +end +pause(1); + + + +% --- Executes on button press in resetbutton. +function resetbutton_Callback(hObject, eventdata, handles) +% hObject handle to resetbutton (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +s = handles.s; tmr=handles.tmr; resetbutton=handles.resetbutton; +set(resetbutton,'enable','off'); +set(handles.camwait,'string','System resetting...'); +stop(tmr); +fclose(s); fopen(s); +pause(2); +fprintf(s,'Q'); +while fscanf(s) ~= 'S' + end; +start(tmr); +set(handles.camwait,'string','system at home location.'); +set(resetbutton,'enable','on'); +return; + + +% --- Executes during object deletion, before destroying properties. +function figure1_DeleteFcn(hObject, eventdata, handles) +% hObject handle to figure1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isempty(instrfind) + fclose(instrfind); + delete(instrfind); +end + +try + stop(tmr); + fid=handles.fid; fclose(fid); + vid = handles.vid; stoppreview(vid); delete(vid); +catch + clear all; + clc; +end + +%s=handles.s; fclose(s); delete(s); +disp('Killed all before exiting'); + + +% --- Executes during object creation, after setting all properties. +function figure1_CreateFcn(hObject, eventdata, handles) +% hObject handle to figure1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles empty - handles not created until after all CreateFcns called + + +function [] = bestpoints(hObject,handles,Xvar) +handles = guidata(hObject); +s=handles.s; +Avar = handles.Avar; Bvar = handles.Bvar; Cvar = handles.Cvar; + if ((Cvar < Bvar) && (Cvar < Avar)) + if ( Xvar > Cvar) + fprintf(s,'Y'); + handles.Cvar = Xvar; + disp('changed C'); + end + end + if ((Avar < Bvar) && (Avar < Cvar)) + if ( Xvar > Avar) + fprintf(s,'E'); + handles.Avar = Xvar; + disp('changed A'); + end + end + if ((Bvar < Avar) && (Bvar < Cvar)) + if ( Xvar > Bvar) + fprintf(s,'R'); + handles.Bvar = Xvar; + handles.Bvar + disp('changed B'); + end + end + + %update handles + guidata(hObject, handles);