Home > marsbar > mars_rois2img.m

mars_rois2img

PURPOSE ^

creates cluster or number labelled ROI image from ROIs

SYNOPSIS ^

function mars_rois2img(roi_list, img_name, roi_space, flags)

DESCRIPTION ^

 creates cluster or number labelled ROI image from ROIs
 FORMAT mars_rois2img(roi_list, img_name, roi_space, flags)

 roi_list  - strings with ROI filenames
 img_name  - name of image to write out
 roi_space - space for image
 flags    - none or more of: [default = 'i']
             'i' - id image, voxel values identify ROIs
             'c' - cluster image, clusters identified by location

 $Id %

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mars_rois2img(roi_list, img_name, roi_space, flags)
0002 % creates cluster or number labelled ROI image from ROIs
0003 % FORMAT mars_rois2img(roi_list, img_name, roi_space, flags)
0004 %
0005 % roi_list  - strings with ROI filenames
0006 % img_name  - name of image to write out
0007 % roi_space - space for image
0008 % flags    - none or more of: [default = 'i']
0009 %             'i' - id image, voxel values identify ROIs
0010 %             'c' - cluster image, clusters identified by location
0011 %
0012 % $Id %
0013   
0014 if nargin < 1
0015   roi_list = '';
0016 end
0017 if nargin < 2
0018   img_name = '';
0019 end
0020 if nargin < 3
0021   roi_space = [];
0022 end
0023 if nargin < 4
0024   flags = '';
0025 end
0026 
0027 % Process input arguments
0028 if isempty(roi_list)
0029   roi_list = spm_get([0 Inf], ['*' maroi('classdata', 'fileend')], ...
0030              'Select ROIs to write to image');
0031   if isempty(roi_list), return, end
0032 end
0033 if isempty(img_name)
0034   img_name = mars_utils('get_img_name');
0035   if isempty(img_name), return, end
0036 end
0037 if isempty(roi_space)
0038    roi_space = maroi('classdata', 'spacebase');
0039 end
0040 if isempty(flags)
0041   flags = 'i';  % id image is default
0042 end
0043 
0044 roi_list = maroi('load_cell', roi_list);
0045 roi_len = prod(size(roi_list));
0046 
0047 img_data = zeros(roi_space.dim);
0048 roi_ctr = 1;
0049 for i = 1:roi_len
0050   roi = roi_list{i};
0051   % check ROI contains something
0052   if isempty(roi) 
0053     warning(sprintf('ROI %d is missing', i));
0054   elseif is_empty_roi(roi)
0055     warning(sprintf('ROI %d:%s is empty', i, label(roi)));
0056   else    
0057     % convert ROI to matrix
0058     mo = maroi_matrix(roi, roi_space);
0059     dat = matrixdata(mo);
0060     if isempty(dat) | ~any(dat(:))
0061       warning(sprintf('ROI %d: %s  - contains no points in this space',...
0062               i, label(roi)));
0063     else
0064       % add matrix to image
0065       if any(flags == 'i')
0066     img_data(dat ~= 0) = roi_ctr;
0067     roi_info(roi_ctr) = struct('label', label(roi),...
0068                    'number', roi_ctr);
0069       else
0070     img_data = img_data + dat;
0071       end
0072       roi_ctr = roi_ctr + 1;
0073     end
0074   end
0075 end
0076 if roi_ctr == 1
0077   warning('Found no useful ROIs, no image saved');
0078   return
0079 end
0080 
0081 % output image type
0082 img_type = 'float'; % to avoid rounding errors
0083 
0084 % save ROI info
0085 if any(flags == 'i')
0086   [p f e] = fileparts(img_name);
0087   iname = fullfile(p, [f '_labels.mat']);
0088   save(iname, 'roi_info');
0089 end
0090 
0091 % Prepare and write image
0092 V = mars_vol_utils('def_vol');
0093 V.fname = img_name;
0094 V.mat = roi_space.mat;
0095 V.dim(1:3) = roi_space.dim;
0096 V = mars_vol_utils('set_type', V, img_type);
0097 
0098 V = spm_create_vol(V);
0099 V = spm_write_vol(V, img_data);
0100 
0101 fprintf('Wrote image %s\nDone...\n', img_name);

Generated on Thu 17-Mar-2016 01:14:46 by m2html © 2005