📄 anatseq.m
字号:
function [] = anatseq(varargin)% ANATSEQ - obtain a sequence of images for morphologic reconstruction%% ANATSEQ acquires a sequence of non-ratiometric images for morphologic% reconstruction. A GUI interface is provided to control the acquisition.%% If no PVCAM device is found, the program will operate in a demo mode% where images will be generated randomly.% 3/31/03 SCM% Files required:% ---------------% PVCAM*.DLL and PVCAM*.M% LAMBDACTRL.M and LAMBDAREAD.M% ROIPARSE.M% UINT8/UINT16 DLLs% UTILITY package% Image processing toolbox (for ROIPOLY command)% Files needed for compiling PVCAM DLLs:% --------------------------------------% PVCAM*.C & PVCAM*.H (for source code, compiled under MATLAB 6.5)% PVCAM32.LIB (PVCAM library)% PVCAM32_MANUAL.PDF (reference for parameter names)% validate argumentsif (nargin == 0) control_command = 'initialize';elseif (nargin ~= 2) warning('MATLAB:anatseq', 'type ''help anatseq'' for syntax'); returnelseif (isa(varargin{1}, 'serial') && isvalid(varargin{1}) && ... isfield(varargin{2}, 'Type') && isfield(varargin{2}, 'Data')) % Lambda 10-2 callback lambda_port = varargin{1}; lambda_event = varargin{2}; h_fig = gcf; control_command = 'lambda callback';elseif (~istype(varargin{1}, 'figure')) warning('MATLAB:anatseq', 'H_FIG must be a valid figure window'); returnelseif (~ischar(varargin{2}) || isempty(varargin{2})) warning('MATLAB:anatseq', 'CMD must be a string'); returnelse h_fig = varargin{1}; control_command = lower(varargin{2});end% obtain UserData from HFIG% check for valid contentsif (~strcmp(control_command, 'initialize')) user_data = get(h_fig, 'UserData'); if (iscell(user_data) && (length(user_data) >= 7)) [fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par] = deal(user_data{1 : 7}); else warning('MATLAB:anatseq', 'cannot find valid UserData cell array in H_FIG'); return endend% figure window parameters% used by Lambda 10-2 interfacemotion_fcn = 'anatseq(gcbo, ''pointer value'')';figure_tag = 'anatseq';switch (control_command) case 'initialize' % open figure window % camera parameters %pvcam_getpar = {'PARAM_BIT_DEPTH', 'PARAM_CHIP_NAME', 'PARAM_FRAME_CAPABLE', 'PARAM_MPP_CAPABLE', ... % 'PARAM_PAR_SIZE', 'PARAM_SER_SIZE', 'PARAM_PREMASK', 'PARAM_TEMP'}; %pvcam_getvalue = {12, 'demo mode', 0, 'mpp mode unknown', ... % 512, 512, 0, 0}; pvcam_getpar = {'PARAM_BIT_DEPTH', 'PARAM_CHIP_NAME', ... 'PARAM_PAR_SIZE', 'PARAM_SER_SIZE', 'PARAM_PREMASK', 'PARAM_TEMP'}; pvcam_getvalue = {12, 'demo mode', 512, 512, 0, 0}; pvcam_setpar = {'PARAM_CLEAR_MODE', 'PARAM_CLEAR_CYCLES', 'PARAM_GAIN_INDEX', ... 'PARAM_PMODE', 'PARAM_SHTR_OPEN_MODE', 'PARAM_SHTR_CLOSE_DELAY', 'PARAM_SHTR_OPEN_DELAY', ... 'PARAM_SPDTAB_INDEX', 'PARAM_TEMP_SETPOINT'}; %pvcam_setvalue = {'clear pre-sequence', 2, 3, ... % 'normal parallel clocking', 'open shutter pre-sequence', 10, 5, 2, -2500}; pvcam_setvalue = {'clear pre-sequence', 2, 2, ... 'normal parallel clocking', 'open shutter pre-sequence', 10, 5, 0, 2000}; % Lambda 10-2 parameters % LAMBDA PARAMS WILL BE EDITABLE IN FUTURE VERSIONS! lambda_par = cell2struct({1, [1 0], [5 2], 5, 'lambda 10-2', @anatseq, []}, ... {'wheel', 'filter', 'speed', 'wait', 'name', 'fcn', 'port'}, 2); % initialize camera % operate in demo mode & use camera defaults if error h_cam = pvcamopen(0); if (isempty(h_cam)) disp('ANATSEQ: could not open camera, using DEMO mode'); pvcamclose(0); pvcam_get = cell2struct(pvcam_getvalue, pvcam_getpar, 2); pvcam_set = cell2struct(pvcam_setvalue, pvcam_setpar, 2); else % read camera parameters disp('ANATSEQ: camera detected'); for i = 1 : length(pvcam_getpar) pvcam_get.(pvcam_getpar{i}) = pvcamgetvalue(h_cam, pvcam_getpar{i}); end % set camera parameters for i = 1 : length(pvcam_setpar) if (pvcamsetvalue(h_cam, pvcam_setpar{i}, pvcam_setvalue{i})) pvcam_set.(pvcam_setpar{i}) = pvcamgetvalue(h_cam, pvcam_setpar{i}); else warning('MATLAB:anatseq', 'could not set %s, using current value', pvcam_setpar{i}); pvcam_set.(pvcam_setpar{i}) = pvcamgetvalue(h_cam, pvcam_setpar{i}); end end end % create image parameters structure % create default ROI from camera parameters w/o binning image_size = [pvcam_get.PARAM_SER_SIZE pvcam_get.PARAM_PAR_SIZE]; roi_coord = [1 1 image_size]; field_name = {'h_cam', 'h_lambda', 'filter_name', 'roi_full', 'roi_coord', ... 'image_size', 'bit_depth', 'expose_time', 'bin_full', 'color_map'}; field_value = {h_cam, [], 'none', create_roi_struct(roi_coord, [1 1]), roi_coord, ... image_size, pvcam_get.PARAM_BIT_DEPTH, 10, [1 1], @gray}; image_par = cell2struct(field_value, field_name, 2); % create file parameters structure field_name = {'file_path', 'file_name', 'file_prefix', 'image_count', 'start_time', 'acq_time'}; field_value = {'c:\scm\cells', '', 'scm', 0, clock, clock}; file_par = cell2struct(field_value, field_name, 2); % open image window delete(findobj('Type', 'figure', 'Tag', figure_tag)); h_fig = make_image_win(figure_tag); % obtain figure handles from HFIG % initialize colormap % disable all buttons to start % these will be enabled during Lambda 10-2 callback fig_handle = get(h_fig, 'UserData'); disp_colorbar(h_fig, fig_handle.h_axes(2), image_par.color_map); % obtain focus parameters from PVCAMFOCUS % save structures in HFIG UserData %focus_par = pvcamfocus(fig_handle.h_axes(1), 'initialize', h_cam, image_par.expose_time, image_par.roi_full); focus_par = []; set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); anatseq(h_fig, 'lambda detect'); case 'pointer value' % obtain value under mouse pointer h_image = get(fig_handle.h_axes(1), 'UserData'); if (istype(h_image, 'image')) image_data = get(h_image, 'UserData'); [ptr_pos, ptr_flag] = ptrpos(h_fig, fig_handle.h_axes(1), 'image'); if ((ptr_pos(1) >= 1) && (ptr_pos(1) <= size(image_data, 2)) && ... (ptr_pos(2) >= 1) && (ptr_pos(2) <= size(image_data, 1)) && ptr_flag) ptr_val = double(image_data(ptr_pos(2), ptr_pos(1))); elapsed_time = datestr(datenum(file_par.acq_time) - datenum(file_par.start_time), 13); ptr_text = sprintf('Image: %d Elapsed Time: %s Intensity at (%d, %d) = %g', file_par.image_count, ... elapsed_time, ptr_pos(1) + image_par.roi_coord(1) - 2, ptr_pos(2) + image_par.roi_coord(2) - 2, ptr_val); set(fig_handle.h_text(1), 'String', ptr_text); end end case 'lambda detect' % initialize filter wheel % set timer and Lambda 10-2 callbacks to determine if filter wheel present % presence of Lambda 10-2 is independent of DEMO mode % buttons will be enabled during either callback if (~isa(image_par.h_lambda, 'timer')) image_par.h_lambda = timer(... 'ExecutionMode', 'fixedrate', ... 'BusyMode', 'drop'); end set(image_par.h_lambda, ... 'TasksToExecute', 1, ... 'StartDelay', lambda_par.wait, ... 'Period', lambda_par.wait, ... 'StartFcn', 'anatseq(gcf, ''lambda setup'')', ... 'TimerFcn', 'anatseq(gcf, ''no lambda 10-2'')', ... 'StopFcn', '', ... 'ErrorFcn', 'anatseq(gcf, ''no lambda 10-2'')'); set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); set(h_fig, 'Tag', control_command); lambda_par.port = lambdactrl('initialize'); lambdactrl(lambda_par.port, 'callback', lambda_par.fcn); lambdactrl(lambda_par.port, 'wheel', lambda_par.wheel); set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); start(image_par.h_lambda); case 'lambda setup' % setup filter and speed lambdactrl(lambda_par.port, 'batch', [0 0], lambda_par.filter, lambda_par.speed); case 'lambda callback' % check echo from serial port % check echo from serial port unless initializing % echo should not be completed until command is finished % proceed with acquisition if bytes read back err_msg = lambdaread(lambda_port, lambda_event); if (strcmp(get(h_fig, 'Tag'), 'lambda detect')) if (isa(image_par.h_lambda, 'timer')) stop(image_par.h_lambda); end image_par.filter_name = lambda_par.name; set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); set(h_fig, 'Tag', figure_tag); enable_buttons(fig_handle.h_button, file_par, image_par); set(h_fig, 'WindowButtonMotionFcn', motion_fcn); disp('ANATSEQ: Lambda 10-2 detected'); elseif (~isempty(err_msg)) warning('MATLAB:anatseq', 'Lambda 10-2 error: %s', err_msg); set(h_fig, 'Tag', figure_tag); enable_buttons(fig_handle.h_button, file_par, image_par); set(h_fig, 'WindowButtonMotionFcn', motion_fcn); elseif (~strcmp(get(h_fig, 'Tag'), figure_tag)) anatseq(h_fig, 'acquire image'); end case 'no lambda 10-2' % configure the program to run w/o Lambda 10-2 stop(image_par.h_lambda); set(h_fig, 'Tag', figure_tag); disp(sprintf('ANATSEQ: Lambda 10-2 not found after %d sec', round(lambda_par.wait))); yes_no = questdlg('Reset the Lambda 10-2 and try again?', 'Lambda 10-2 not found', 'yes', 'no', 'no'); if (strcmp(yes_no, 'yes')) anatseq(h_fig, 'lambda detect'); else enable_buttons(fig_handle.h_button, file_par, image_par); set(h_fig, 'WindowButtonMotionFcn', motion_fcn); end case {'snap image', 'start focus'} % setup snap or focus % disable buttons and open shutter % this will execute lambda 10-2 command callback upon completion % remainder of image acquisition will be completed here set(fig_handle.h_button, 'Enable', 'off'); set(fig_handle.h_button(3), 'Enable', 'on'); set(h_fig, 'WindowButtonMotionFcn', ''); set(h_fig, 'Tag', control_command); % initiate acquisition directly or via Lambda 10-2 callback if (strcmp(image_par.filter_name, lambda_par.name)) lambdactrl(lambda_par.port, 'open'); else anatseq(h_fig, 'acquire image'); end case 'acquire image' % callback following lambda 10-2 command echo % perform single or repetitive acquisition if specified image_cmd = get(h_fig, 'Tag'); switch (image_cmd) case 'snap image' % acquire single image [image_data, file_par] = get_single_image(h_fig, fig_handle, image_par, file_par, lambda_par, 'normal'); set(h_fig, 'Tag', figure_tag); set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); set(h_fig, 'WindowButtonMotionFcn', motion_fcn); enable_buttons(fig_handle.h_button, file_par, image_par); case 'start focus' % continuously acquire until stop is detected % call PVCAMFOCUS to obtain images % reset image parameters following focus %focus_par = pvcamfocus(h_fig, 'start focus', focus_par); %if (istype(focus_par{1}.h_image, 'image')) % set(fig_handle.h_axes(1), 'UserData', focus_par{1}.h_image); %end % old focus code if (~isempty(image_par.h_cam)) [param_value, param_type, param_access, param_range] = pvcamgetvalue(image_par.h_cam, 'PARAM_SPDTAB_INDEX'); pvcamsetvalue(image_par.h_cam, 'PARAM_SPDTAB_INDEX', max(param_range)); end while (~strcmp(get(h_fig, 'Tag'), 'stop')) [image_data, file_par] = get_single_image(h_fig, fig_handle, image_par, file_par, lambda_par, 'focus'); if (isempty(image_data) || ~isnumeric(image_data)) break end end % end old focus code % close LAMBDA 10-2 shutter and reset other parameters set(h_fig, 'Tag', figure_tag); if (strcmp(image_par.filter_name, lambda_par.name)) lambdactrl(lambda_par.port, 'close'); end set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); set(h_fig, 'WindowButtonMotionFcn', motion_fcn); enable_buttons(fig_handle.h_button, file_par, image_par); end case 'parameters' % define sequence parameters % setup editing of acquisition parameters for GUISTRUCT field_name = {'file_path', 'file_prefix', 'expose_time', 'bin_full'}; field_lower = [-Inf -Inf 0 1]; field_upper = Inf * ones(size(field_lower)); field_title = {'File path', 'File prefix', 'Exposure (ms)', 'Pixel bins'}; field_format = {'%s', '%s', '%d', '%d'}; field_multi = {'string', 'string', 'scalar', 'vector'}; % obtain parameters from FILE_PAR and IMAGE_PAR to edit edit_struct = []; for i = 1 : length(field_name) if (isfield(file_par, field_name{i})) edit_struct.(field_name{i}) = file_par.(field_name{i}); elseif (isfield(image_par, field_name{i})) edit_struct.(field_name{i}) = image_par.(field_name{i}); end end [edit_struct, edit_flag] = guistruct('Acquisition Parameters', ... edit_struct, field_lower, field_upper, field_title, field_format, field_multi); % modify parameters if selected if (isstruct(edit_struct) && edit_flag) field_name = fieldnames(edit_struct); for i = 1 : length(field_name) if (isfield(file_par, field_name{i})) file_par.(field_name{i}) = edit_struct.(field_name{i}); elseif (isfield(image_par, field_name{i})) image_par.(field_name{i}) = edit_struct.(field_name{i}); end end % update ROI structure in case binning changed % recalculate focus parameters image_par.roi_full = create_roi_struct(image_par.roi_coord, image_par.bin_full); %focus_par = pvcamfocus(fig_handle.h_axes(1), 'initialize', ... % image_par.h_cam, image_par.expose_time, image_par.roi_full); set(h_fig, 'UserData', {fig_handle, image_par, file_par, pvcam_get, pvcam_set, lambda_par, focus_par}); end case 'camera setup' % edit PVCAM parameters pvcam_setpar = fieldnames(pvcam_set); [new_value, edit_flag] = pvcameditor(image_par.h_cam, pvcam_setpar);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -