% ss_edges( image_filename ) % -------------------------- % % Component of Scale Space Edges, version 1.2c. % % Locates and characterizes scale-space edges in the luminosity % of a 2-D image. Optionally oversegments and clusters sub-regions % of the image. % % The sparse scale space search and edge following algorithm is % designed to flexibly limit computational resouce needs and % preserve edge connectivity for image segmentation purposes. % The program generates a list of edge segments (ordered sets of % edge points), nodes (intersections of edge segments) and edge % point metrics. No threshold or smoothing parameters need to be % preselected. % % The appropriate scale at each point is adaptively selected % using this edge definition: edge points have a first derivative % magnitude (2-D space) that is locally maximal (in scale-space) % in the first derivative direction. % % The scale-space image model is gaussian blurred. The working % scale representation is limited to a small number % ( < log2(image dimension) ) of blurred copies of the input image. % A bank of oriented Difference of Gaussian (DoG) filters, matched % to the sample scale planes, is generated and used for first % through second (or third) order derivative estimation. % % This m-file is a control program for a set of functions that can % be run independently. Most parameters can be altered in the body % of this function (see below). The calls made may include (in this % order): % % -> ss_edges() % -> ss_make_DoG_filters() % -> ss_DoGauss() % -> ss_get_image_from_file() % -> ss_k_planes() % -> ss_edge_worm() % -> ss_display_edges() % -> ss_write_edge_images() % % Input: % % filename - % Image file name of an image (>= 8 bit) in a format % that Matlab understands. For example, % % >> ss_edges( 'Clown.tif' ) % % will generate edge information for the luminosity % of that color TIFF. Matlab also can read % ( see >>help imread ) most JPEG, BMP, PNG images % with standard extensions. The file must be in the % current directory or in a directory on the MATLAB % path. % % Output: % % bank of oriented filters - % A matlab file with a name like: % % ss_DoG_filters.mat % % containing derivative approximation filters matched % to the scale space sample planes. See % ss_make_DoG_filters.m for details. % % If the file already exists in the current directory % or in a directory in the MATLAB path, the file will % not be regenerated. % % scale space grayscale sample image planes - % A series of matlab files with names like: % % Clown_plane_k0.mat % Clown_plane_k1.mat % . . % . . % Clown_plane_k[n].mat % % containing resampled, grayscale, and blurred images. % See ss_k_planes.m for details. % % If these files already exists in the current directory % or in a directory on the MATLAB path, the files will % not be regenerated. % % Edge point, segment and node arrays - % A matlab file with a name like: % % edges_Clown.mat % % containing all derived edge information and some of the % parameters used. See ss_edge_worm.m for details. % % This file will be overwritten if ss_edges.m is run again % on the same file, or one having the same stem. % % See ss_display_edges.m for an example of how to display % some of this edge information. % % Display of edge segment paths - % Window showing simple representation of edge segments % overlaid on grayscale image, and/or other edge info maps. % Set parameters for edge information display in body of this % function (see below). See ss_display_edges.m for details of % parameters. % % NOTE: Memory requirements: % % The program ss_edge_worms.m requires a considerable amount of memory % to accomodate the scale space sample planes files (Clown_plane_k[#].mat), % filter bank (ss_DoG_filters.mat), and edge information. % % The memory required will primarily depend on: % % - The image dimensions. % - The resampling factor. % - The range of scales used in the search. % - The scale resolution and angular resolution used to create the % filter bank. % % % % Mark Dow, last modified April 2004 % University of Oregon % Brain Development Lab / Lewis Center for Neuroimaging % http://lcni.uoregon.edu/~mark % dow@uoregon.edu % function ss_edges( image_filename ); %tic filestem = image_filename( 1:find(image_filename == '.') - 1 ); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Set parameters for scale space sample plane generation, ss_k_planes.m. % % Note: If any of required planes don't already % exist ( [filestem]_plane_k[0:k_max].mat ), % all planes are regenerated, overwriting % any existing planes. resample_factor = 1 % Up-sampling ratio. k_min = 0; % Minimum scale index (scale = 2^k). % Limit of edge following at small scales. k_max = 3; % Maximum scale index (scale = 2^k). Gaussian % smoothed raw images are generated for % [ 0, k_max ], if the size of the image allows. plane_postfix = '_plane_k'; b_display_planes = 0; % [0,1] -> Show/don't show matlab figure with resulting images. b_delete_planes = 1; % [0,1] -> delete planes after using. % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Set parameters for scale space edge worm search. % % b_delete_edge_info = 1; % edges_prefix = 'edges_'; % File containing edge info will be named % % [ edges_prefix image_filestem ].mat, like edges_Clown.mat. % May 2003: now uses the hardcoded postfix [ filestem '_edges_info' ].mat, like Clown_edges_info.mat. k_mx = k_max; % Maximum scale index (scale = 2^k). ks = [ .4 ]; % Initial search scale power (scale = 2^ks) list. 0 < ks <= k_max % ks = [ 2.4 1.4 .4]; % ks = [ .4 1.4 2.4 ]; spacing = [ 2 ]; % Search grid spacing list, at each initial search scale. % spacing = [ 16 8 4]; % spacing = [ 4 8 16 ]; rc_Crop = [-1 0 0 0]; % Cropping rectangle, relative to sample planes. % [ y_min y_max x_min x_max ] where [ -1 0 0 0] % indicates complete image. For % example rc_Crop = [450 650 300 500]; for the Clown.tif % light bulb. G1_init_thresh = 0.0; % Normalized gradient threshold for starting an edge segment. % In principle, the edge tracker finds all edges, regardless % of gradient magnitude, so this should be set to 0.0. In % practice, edges that have no points with a gradient above % ~.005 have no physical significance (they are due to noise). % Raising the threshold to this value will reduce the number of % low salience short segments significantly, and speed up the % the search a by a small factor (~10%). % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Set parameters for filter bank algorithm, ss_make_DoG_filters.m. % % Note: remove or rename old filter bank if these parameters are changed, as an existing % filter bank will not be overwritten. >> !rm ss_DoG_filters.mat filter_bank_filestem = 'ss_DoG_filters'; n_k = 10; % Number of filters between a factor of two in scale. % Fixes scale resolution. n_k = [5 20] is reasonable. n_theta = 90; % Number of filter angles between a 0 and 180 % degrees. Fixes angular resolution. % n_theta = [30 90] is reasonable. k_inc = 1/n_k; % Scale resolution (increment). % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Set parameters for edge information post-processing, image writing and display. % % b_write_results_files = 1; % [0 1] Write all results data and images. b_DoSegmentation = 1; b_WriteRegionHierarchyVolume = 1; % Save a Space software compatible 16 bit indexed tree segmentation volume b_display_edge_info = 1; % [0 1] Show derived edge overlay, with the parameters % set to default values (see below). % Default display values: n_window_option = 2; % [1 8] Index for window display options. % 1 - Display all windows. % 2 - Show only edge overlay. % 3 - Show original image and edge overlay. % See ss_display_images.m header for other options. figure_size = 0; % [0 >0 ] Scaling factor for figure size % (figure size/resampled image size). Default, % or set to 0, fits the windows to the % screen with some elbow room. % g_m = .01; % Minimum average normalized gradient threshold. % g_m = .004; % Minimum average normalized gradient threshold. g_m = .00004; % Minimum average normalized gradient threshold. % g2_m = .04; % Normalized gradient intercept threshold. % g2_m = .03; % Normalized gradient intercept threshold. g2_m = .0003; % Normalized gradient intercept threshold. % l_m = 5; % Minimum average length threshold. % l_m = 4; % Minimum average length threshold. l_m = 1; % Minimum average length threshold. % l2_m = 80; % Length intercept threshold. % l2_m = 50; % Length intercept threshold. l2_m = 1; % Length intercept threshold. % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Construct filter bank only if it doesn't already exist. % % ss_make_DoG_filters.m % fid = fopen( [ filter_bank_filestem '.mat' ] ); % If file with desired filter bank name does not yet exist: if fid == -1 fprintf('\n\n Constructing filter bank ....\n'); ss_make_DoG_filters( filter_bank_filestem, n_k, n_theta ); fprintf('\n Done constructing filter bank.\n\n'); else fclose(fid); msg = ['\n\n Using an existing filter bank: ' filter_bank_filestem '.mat\n\n']; fprintf( msg ); end % % %%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Load image and construct scale space sample planes, % only if they don't already exist. % flim_gray = ss_get_image_from_file( image_filename ); b_regenerate = 0; for k = 0 : k_max plane_filestem = [ filestem '_plane_k' num2str(k) ]; plane_filename = [ plane_filestem '.mat' ]; fid = fopen( plane_filename ); % If any file with this plane name does not yet exist: if fid == -1 % Initiate regeneration of all planes. b_regenerate = 1; k = k_max; else fclose(fid); msg = ['\n\n Using existing sample planes: ' plane_filestem '\n\n' ]; fprintf( msg ); end end if b_regenerate == 1 fprintf('\n\n Constructing scale space sample planes ....\n'); plane_fileprefix = [ filestem plane_postfix ]; ss_k_planes( flim_gray, plane_fileprefix, k_max, resample_factor, b_display_planes ); fprintf('\n Done constructing scale space sample planes.\n\n'); else plane_filestems = [ filestem plane_postfix '#.mat' ]; end % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Run edge worm, a scale space gradient ridge location % and following algorithm. % plane_fileroot = [ filestem plane_postfix ]; ss_edge_worm( filestem, plane_fileroot, k_min, k_mx, k_inc, ks, spacing, filter_bank_filestem, rc_Crop, G1_init_thresh ) % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Delete scale space sample planes after use. % if b_delete_planes == 1 for k = 0 : k_max plane_filestem = [ filestem '_plane_k' num2str(k) ]; plane_filename = [ plane_filestem '.mat' ]; delete( plane_filename ); end end % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Post-processing. % ss_segment_summary( filestem ); % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Write and display edge maps, overlay. % if b_write_results_files == 1 ss_write_edge_images( filestem ) end if b_display_edge_info == 1 ss_display_edges( filestem, n_window_option, figure_size, g_m, g2_m, l_m, l2_m, k_mx/2 ) end % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Segmentation. % if b_DoSegmentation == 1 fprintf( '\n\n Starting region averaging and segmentation ... this could take a while.' ); ss_region_average( filestem, b_write_results_files ); ss_region_hierarchy( filestem, b_WriteRegionHierarchyVolume ); fprintf( '\n Segmentation hierarchy Space volume has been generated:' ); fprintf( '\n See http://lcni.uoregon.edu/~mark for an example of how to use this data.' ); end % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Delete edge info file. % if b_delete_edge_info == 1 delete( [ filestem '_edge_info.mat' ] ); end % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %toc