diff --git a/Flowcytometer_matlab _code/flowcytometergui.fig b/Flowcytometer_matlab _code/flowcytometergui.fig new file mode 100644 index 0000000..d449117 Binary files /dev/null and b/Flowcytometer_matlab _code/flowcytometergui.fig differ diff --git a/Flowcytometer_matlab _code/flowcytometergui.m b/Flowcytometer_matlab _code/flowcytometergui.m new file mode 100644 index 0000000..c9ae7ec --- /dev/null +++ b/Flowcytometer_matlab _code/flowcytometergui.m @@ -0,0 +1,217 @@ +function varargout = flowcytometergui(varargin) +% FLOWCYTOMETERGUI MATLAB code for flowcytometergui.fig +% FLOWCYTOMETERGUI, by itself, creates a new FLOWCYTOMETERGUI or raises the existing +% singleton*. +% +% H = FLOWCYTOMETERGUI returns the handle to a new FLOWCYTOMETERGUI or the handle to +% the existing singleton*. +% +% FLOWCYTOMETERGUI('CALLBACK',hObject,eventData,handles,...) calls the local +% function named CALLBACK in FLOWCYTOMETERGUI.M with the given input arguments. +% +% FLOWCYTOMETERGUI('Property','Value',...) creates a new FLOWCYTOMETERGUI or raises the +% existing singleton*. Starting from the left, property value pairs are +% applied to the GUI before flowcytometergui_OpeningFcn gets called. An +% unrecognized property name or invalid value makes property application +% stop. All inputs are passed to flowcytometergui_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 flowcytometergui + +% Last Modified by GUIDE v2.5 09-Jul-2018 11:27:06 + +% Begin initialization code - DO NOT EDIT +gui_Singleton = 1; +gui_State = struct('gui_Name', mfilename, ... + 'gui_Singleton', gui_Singleton, ... + 'gui_OpeningFcn', @flowcytometergui_OpeningFcn, ... + 'gui_OutputFcn', @flowcytometergui_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 flowcytometergui is made visible. +function flowcytometergui_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 flowcytometergui (see VARARGIN) + +% Choose default command line output for flowcytometergui +handles.output = hObject; +s = serial('COM12'); +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('winvideo', 2, 'RGB24_640x480'); +% 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 +currzpos = 0; +preview(vid, hImage); +% expose the objects so we can handle them in other functions +handles.vid = vid; +handles.src = src; +handles.currz = currzpos; +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 + +% Update handles structure +guidata(hObject, handles); + +% UIWAIT makes flowcytometergui wait for user response (see UIRESUME) +% uiwait(handles.figure1) + +% --- Outputs from this function are returned to the command line. +function varargout = flowcytometergui_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 on button press in autofocus. +function autofocus_Callback(hObject, eventdata, handles) +% hObject handle to autofocus (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; currzpos = handles.currz; +[~, ~, ~] = focusstack_1(s,vid,currzpos);pause(2) + + + + +% --- Executes on button press in moveleft. +function moveleft_Callback(hObject, eventdata, handles) +% hObject handle to moveleft (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 on button press in moveright. +function moveright_Callback(hObject, eventdata, handles) +% hObject handle to moveright (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 snapshot. +function snapshot_Callback(hObject, eventdata, handles) +% hObject handle to snapshot (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); +imwrite(snapshot,'snap.jpg') + +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 + 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 on button press in startvid. +function startvid_Callback(hObject, eventdata, handles) +% hObject handle to startvid (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; +vid_trigger = 1; handles.vid_trigger = vid_trigger; +for x=1:1000 + snap = getsnapshot(vid) + imwrite(strcat('Image_',num2str(x),'.jpg'),snap) +end +% aviObject = VideoWriter('newfile.avi','Motion JPEG AVI'); handles.aviObject = aviObject; +% open(aviObject); +% while(vid_trigger == 1) +% snap = getsnapshot(vid); +% writeVideo(aviObject,snap); +% end + +% --- Executes on button press in stopvid. +function stopvid_Callback(hObject, eventdata, handles) +% hObject handle to stopvid (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +aviObject = handles.aviObject; +handles.vid_trigger = 0; +close(aviObject); diff --git a/Flowcytometer_matlab _code/focusplane_4.m b/Flowcytometer_matlab _code/focusplane_4.m new file mode 100644 index 0000000..fb9151c --- /dev/null +++ b/Flowcytometer_matlab _code/focusplane_4.m @@ -0,0 +1,263 @@ +clear;clc; +disp('Scan Inializing........') +tic; +t1 = toc; +s = serial('COM3'); +set(s,'BaudRate',57600); set(s,'Terminator','#'); +set(s,'Timeout',30); %s.RecordName = 'CytoSerialTxnLog.txt'; +fopen(s); %recweblistord(s); +msg=strcat(datestr(clock,'yyyy-mm-dd-HHMM'),'m',datestr(clock,'ss'),'s'); +fid = fopen(strcat(msg,'.txt'),'w'); +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 +disp('\r\nStarting camera...') +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'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% do auto white balance +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +src = getselectedsource(vid); +src.BalanceWhiteAuto = 'Continuous'; + +src.ExposureTime = 800; +start(vid); +Time_to_init_cam = toc + +% this is the slide name +setslidename = 'Slide'; + + +fprintf(s,'J');currxpos = 0; +fprintf(s,'K');currypos = 0; +fprintf(s,'L');currzpos = 0; +pause(0.1); +disp('Origin is set'); + +xFoV = round(1280*5.3/(34*0.159));yFoV = round(1024*5.3/(34*0.159)); +xFoV_mm = 0.1995;yFoV_mm = 0.1596; + +XPulseRate = 38400; YPulseRate = 38400 ; +Xzadjustdelay = 0.002; +autoContrTime = 0.1; +XImDelay = (xFoV / XPulseRate) ; +YImDelay = (yFoV / YPulseRate) ; +%at y = 12.5mm line +Ypos = strcat('+',sprintf('%06d',abs(41359))); +fprintf(s,'N'); fprintf(s,Ypos);pause(41359/YPulseRate);currypos=41359; +param_y0 =[];Zval=[]; +xpos1 = 0; xpos2 = 200000; +[currzpos, temp_var] = focusstack_1(s,vid,currzpos);pause(2) +tf1 = toc; +for xpos = xpos1:17986:xpos2 + Xpos = strcat('+',sprintf('%06d',abs(xpos ))); + fprintf(s,'B'); fprintf(s,Xpos);pause(17986/XPulseRate) + [currzpos, temp_var] = focusstack_2(s,vid,currzpos); + Zval = [Zval currzpos]; + snapshot=getsnapshot(vid); + disp('saving best focussed snapshot..') + imwrite(snapshot,strcat('images/finding_best_region/t7/t7_x_',Xpos,'_',sprintf('%06d',abs(currzpos)),'.bmp')); + param_y0 = [param_y0 parameter(snapshot)]; currxpos = xpos; +end +disp(toc-tf1) +xpos = xpos1:17986:xpos2; +[max_param_y0, Index] = max(param_y0); +xbest1 = abs(xpos(Index)); +Xbest1 = strcat('+',sprintf('%06d',xbest1)); +fprintf(s,'B'); fprintf(s,Xpos);pause(abs(currxpos-xbest1)/XPulseRate) +Zfoc1 = Zval(Index);gotoZ(s,Zfoc1,currzpos);currzpos=Zfoc1;param_y1 = []; Zval=[]; +tf2 =toc; +if(xbest1 <= 17986) + for xpos = (xbest1):7194:(xbest1+17986*2) + Xpos = strcat('+',sprintf('%06d',abs(xpos ))); + fprintf(s,'B'); fprintf(s,Xpos);pause(17986/XPulseRate) + [currzpos, temp_var] = focusstack_2(s,vid,currzpos); + Zval = [Zval currzpos]; + snapshot=getsnapshot(vid); + disp('saving best focussed snapshot..') + imwrite(snapshot,strcat('images/finding_best_region/t7/t7_x_',Xpos,'_',sprintf('%06d',abs(currzpos)),'.bmp')); + param_y1 = [param_y1 parameter(snapshot)]; currxpos = xpos; + end +elseif(xbest1>=200000) + for xpos = (xbest1-17986*2):7194:(xbest1) + Xpos = strcat('+',sprintf('%06d',abs(xpos ))); + fprintf(s,'B'); fprintf(s,Xpos);pause(17986/XPulseRate) + [currzpos, temp_var] = focusstack_2(s,vid,currzpos); + Zval = [Zval currzpos]; + snapshot=getsnapshot(vid); + disp('saving best focussed snapshot..') + imwrite(snapshot,strcat('images/finding_best_region/t7/t7_x_',Xpos,'_',sprintf('%06d',abs(currzpos)),'.bmp')); + param_y1 = [param_y1 parameter(snapshot)]; currxpos = xpos; + end +else + for xpos = (xbest1-17986):7194:(xbest1+17986) + Xpos = strcat('+',sprintf('%06d',abs(xpos ))); + fprintf(s,'B'); fprintf(s,Xpos);pause(17986/XPulseRate) + [currzpos, temp_var] = focusstack_2(s,vid,currzpos); + Zval = [Zval currzpos]; + snapshot=getsnapshot(vid); + disp('saving best focussed snapshot..') + imwrite(snapshot,strcat('images/finding_best_region/t7/t7_x_',Xpos,'_',sprintf('%06d',abs(currzpos)),'.bmp')); + param_y1 = [param_y1 parameter(snapshot)]; currxpos = xpos; + end +end +disp(toc-tf2) +xpos = (xbest1-17896):7194:(xbest1+17896); +param_y1 = (param_y1>0.0999);disp(param_y1) +target1 = sum(param_y1(1:3)); target2 = sum(param_y1(3:5)); +fprintf(s,'N'); fprintf(s,'0000000');pause(abs(41359)/YPulseRate); +tf3=toc; +if((sum(param_y1(2:4))==3)) + xbest2 = xpos(2);xbest1 = xpos(4); + Xbest2 = strcat('+',sprintf('%06d',xbest2)); + Xbest1 = strcat('+',sprintf('%06d',xbest1)); + fprintf(s,'B'); fprintf(s,Xbest1);pause((abs(currxpos-xbest1))/XPulseRate);gotoZ(s,Zval(4),currzpos);currzpos=Zval(4);fprintf(s,'R'); + fprintf(s,'B'); fprintf(s,Xbest2);pause((abs(17986))/XPulseRate);gotoZ(s,Zval(2),currzpos);currzpos=Zval(2);fprintf(s,'E');currxpos = xbest2; + fprintf(s,'N'); fprintf(s,'0078719');pause(abs(78719)/YPulseRate);currypos = 78719; + [currzpos, temp_var] = focusstack_2(s,vid,currzpos);fprintf(s,'Y'); + fprintf(s,'Z'); + xA = xbest2; xB = xbest1; + yC = 78719; yB =0; + numofX = (xB - xA) / xFoV; + numofY = (yC - yB) / yFoV; + while fscanf(s) ~= 'S' + end; + % 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*2); + snapshot=getsnapshot(vid); + %enh_snapshot=autocontrast(snapshot); + filename = strcat(setslidename,'_',num2str(rowcount),'-',num2str(filecount), '.bmp'); + %imwrite(enh_snapshot,filename); + imwrite(snapshot,strcat('images/',filename)); + filecount = filecount + 1; + end + % wait for shift by one FoV in Y direction + filecount = 0; + %pause(YImDelay*2); + rowcount = rowcount + 1; + end +elseif(target1>target2) + xbest2 = xpos(1);xbest1 = xpos(3); + Xbest2 = strcat('+',sprintf('%06d',xbest2)); + Xbest1 = strcat('+',sprintf('%06d',xbest1)); + fprintf(s,'B'); fprintf(s,Xbest1);pause((abs(currxpos-xbest1))/XPulseRate);gotoZ(s,Zval(3),currzpos);currzpos=Zval(3);fprintf(s,'R'); + fprintf(s,'B'); fprintf(s,Xbest2);pause((abs(17986))/XPulseRate);gotoZ(s,Zval(1),currzpos);currzpos=Zval(1);fprintf(s,'E');currxpos = xbest2; + fprintf(s,'N'); fprintf(s,'0078719');pause(abs(78719)/YPulseRate);currypos = 78719; + [currzpos, temp_var] = focusstack_2(s,vid,currzpos);fprintf(s,'Y'); + fprintf(s,'Z'); + xA = xbest2; xB = xbest1; + yC = 78719; yB =0; + numofX = (xB - xA) / xFoV; + numofY = (yC - yB) / yFoV; + while fscanf(s) ~= 'S' + end; + % 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*2); + snapshot=getsnapshot(vid); + %enh_snapshot=autocontrast(snapshot); + filename = strcat(setslidename,'_',num2str(rowcount),'-',num2str(filecount), '.bmp'); + %imwrite(enh_snapshot,filename); + imwrite(snapshot,strcat('images/',filename)); + filecount = filecount + 1; + end + % wait for shift by one FoV in Y direction + filecount = 0; + %pause(YImDelay*2); + rowcount = rowcount + 1; + end +elseif(target1 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 ; + fprintf(s,'G');pause(0.1) + %gotoZ(s,crude_curr_loc,currzpos); + currzpos=crude_curr_loc; + end + disp(currzpos) + disp(crude_loc_max_var) + crude_pulse_count = round(abs(crude_loc_max_var - currzpos)/200); + for i=1: crude_pulse_count + if(currzpos>crude_loc_max_var) + fprintf(s,'T');pause(0.1) + currzpos = currzpos - 200; + elseif(currzpos 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(s,crude_curr_loc,currzpos); currzpos=crude_curr_loc; + end + gotoZ(s,crude_loc_max_var,currzpos);currzpos=crude_loc_max_var; + + % fINECRUDE AF. + Z_travel_for_crude = 500; crude_step_count = 50; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + crude_pulse_count = Z_travel_for_crude / crude_step_count; + crude_max_var=0; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % go to starting Z loc (we've hit limit switch via arduino already so its z=0) + crude_start_loc = currzpos- (Z_travel_for_crude/2); + gotoZ(s,crude_start_loc,currzpos);currzpos=crude_start_loc; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % 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(s,crude_curr_loc,currzpos);currzpos=crude_curr_loc; + end + gotoZ(s,crude_loc_max_var,currzpos);currzpos=crude_loc_max_var; + + % The fine AF begins here + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + Ztotaltravel = 100; final_go_backpulses = 0 ; + currZ = crude_loc_max_var; + + % first go above + start_above_loc = currZ- (Ztotaltravel/2); + gotoZ(s,start_above_loc,currzpos);currzpos=start_above_loc; + + % 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(s,localAF_curr_loc,currzpos); + currzpos=localAF_curr_loc; + end + + % Finally go to the max var location + index_of_max_var = localAF_var_array == max(localAF_var_array); + loc_of_max_var = localAF_loc_array(index_of_max_var) - final_go_backpulses ; + gotoZ(s,loc_of_max_var,currzpos); currzpos=loc_of_max_var; + Zval = loc_of_max_var; + Variance = max(localAF_var_array); + t2 = toc; + disp(t2-t1) +end \ No newline at end of file diff --git a/Flowcytometer_matlab _code/focusstack_3.m b/Flowcytometer_matlab _code/focusstack_3.m new file mode 100644 index 0000000..88cc9e5 --- /dev/null +++ b/Flowcytometer_matlab _code/focusstack_3.m @@ -0,0 +1,60 @@ +function [Zval, Variance] = focusstack_3(s,vid,z) + t1 = toc;currzpos=z; + % fINECRUDE AF. + Z_travel_for_crude = 1000; crude_step_count = 50; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + crude_pulse_count = Z_travel_for_crude / crude_step_count; + crude_max_var=0; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % go to starting Z loc (we've hit limit switch via arduino already so its z=0) + crude_start_loc = z - (Z_travel_for_crude/2); + gotoZ(s,crude_start_loc,currzpos); currzpos=crude_start_loc; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % 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(s,crude_curr_loc,currzpos); currzpos=crude_curr_loc; + end + gotoZ(s,crude_loc_max_var,currzpos);currzpos=crude_loc_max_var; + + % The fine AF begins here + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + Ztotaltravel = 100; final_go_backpulses = 0 ; + currZ = crude_loc_max_var; + + % first go above + start_above_loc = currZ - (Ztotaltravel/2); + gotoZ(s,start_above_loc,currzpos);currzpos=start_above_loc; + % 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(s,localAF_curr_loc,currzpos); + currzpos=localAF_curr_loc; + end + + % Finally go to the max var location + index_of_max_var = localAF_var_array == max(localAF_var_array); + loc_of_max_var = localAF_loc_array(index_of_max_var) - final_go_backpulses ; + gotoZ(s,loc_of_max_var,currzpos);currzpos=loc_of_max_var; + Zval = loc_of_max_var; + Variance = max(localAF_var_array); + t2 = toc; + disp(t2-t1) +end \ No newline at end of file diff --git a/Flowcytometer_matlab _code/gotoZ.m b/Flowcytometer_matlab _code/gotoZ.m new file mode 100644 index 0000000..104851f --- /dev/null +++ b/Flowcytometer_matlab _code/gotoZ.m @@ -0,0 +1,14 @@ +function gotoZ(s,check,currzpos) + ZPulseRate = 38400; + if(check<200000 && check>-200000) + str = sprintf('%06d',abs(round(check))); % abs needed so that no +&- in neg numbers + if((check) < 0) + newstr = strcat('-',str); + else + newstr = strcat('+',str); + end + disp(strcat('moving to position:',newstr)) + % end formatting ---------- -------------------------------------------- + fprintf(s,'M'); fprintf(s, newstr ); pause(0.035); + end +end \ No newline at end of file diff --git a/Flowcytometer_matlab _code/parameter.m b/Flowcytometer_matlab _code/parameter.m new file mode 100644 index 0000000..775c194 --- /dev/null +++ b/Flowcytometer_matlab _code/parameter.m @@ -0,0 +1,21 @@ +function param = parameter(image) + t1 = toc; + bw = rgb2gray(image); + [Gx, Gy] = imgradientxy(bw); + %[Gmag, Gdir] = imgradient(Gx, Gy); + gradImage = rgb2gray(imfuse(Gx,Gy)); + %temp = histeq(gradImage); + threshImage = im2bw(gradImage,graythresh(gradImage)); + [~, radii] = imfindcircles(threshImage,[10 60]); + cell_area = round(sum(3.14*(radii.^2))); + bg = rgb2gray(imread('bg.jpg')); + a = histeq(bw)-bg; + level2 = graythresh(a); + bgImg = bwareaopen(im2bw(a,level2),800); + bg_pixels = sum(sum(bgImg == 1)); + fg_pixels = (1280 * 1024) - bg_pixels; + fg_pixels = fg_pixels - round(0.1*fg_pixels); + param = cell_area/fg_pixels; + t2 = toc; + disp(t2-t1) +end \ No newline at end of file