0001 function mars_rois2img(roi_list, img_name, roi_space, flags)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
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';
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
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
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
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
0082 img_type = 'float';
0083
0084
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
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);