Hi Sarah,

rgb is an image format, and COR is a volume format. There are freesurfer matlab scripts that can write a volume out in cor (or mgh) format. I've attached them.

cheers,
Bruce

On Fri, 1 Jul 2005, Sarah Rugheimer wrote:

Hello,

I manipulated some of the slices in matlab and would like to convert them back into COR format so I can bring them back into Freesurfer to complete the reconstruction. I can save these images into any matlab supported format or RGB. Since freesurfer can convert COR to RGB I thought there might be a way to convert from RGB to COR, but I have been unable to find it.
Any help would be appreciated! Thanks.
Sarah
_______________________________________________
Freesurfer mailing list
Freesurfer@nmr.mgh.harvard.edu
https://mail.nmr.mgh.harvard.edu/mailman/listinfo/freesurfer
function err = save_cor(corvol,stemdir,dir)
%
% err = save_cor(corvol,<stemdir>,<dir>)
%
% Saves in stemdir/dir. If stemdir and dir are not 
% specified, saves in the current directory.
%
% See also load_cor.
%
% $Id: save_cor.m,v 1.2 2003/08/21 20:00:51 greve Exp $

err = 1;

if(nargin < 1 | nargin > 3)
  msg = 'USAGE: err = save_cor(corvol,<stemdir>,<dir>)';
  qoe(msg);error(msg);
end

if(exist('stemdir') ~= 1 & exist('dir') ~= 1)
  cordir = '.';
else
  cordir = sprintf('%s/%s',stemdir,dir);
  status = mkdir(stemdir,dir);
end

fname = sprintf('%s/COR-.info',cordir);
fid = fopen(fname,'w');
if(fid == -1)
  fprintf('ERROR: opening %s for writing\n',fname);
  return;
end

fprintf(fid,'imnr0 %d\n',1);
fprintf(fid,'imnr1 %d\n',size(corvol,3));
fprintf(fid,'ptype %d\n',2);
fprintf(fid,'x %d\n',size(corvol,2));
fprintf(fid,'y %d\n',size(corvol,1));
fprintf(fid,'fov %f\n',0.256);
fprintf(fid,'thick %f\n',0.001);
fprintf(fid,'psiz %f\n',0.001);
fprintf(fid,'locatn %f\n',0);
fprintf(fid,'strtx %f\n',-0.128);
fprintf(fid,'endx %f\n',0.128);
fprintf(fid,'strty %f\n',-0.128);
fprintf(fid,'endy %f\n',0.128);
fprintf(fid,'strtz %f\n',-0.128);
fprintf(fid,'endz %f\n',0.128);
fprintf(fid,'tr %f\n',0.0);
fprintf(fid,'te %f\n',0.0);
fprintf(fid,'ti %f\n',0.0);
fclose(fid);

[nc,nr,ns] = size(corvol);

for s=1:ns
  corslice = squeeze(corvol(:,:,s))'; %' Convert to column major 
  corslice = uint8(max(0,min(255,corslice)));
  corslicefile = sprintf('%s/COR-%03d',cordir,s);
  fid=fopen(corslicefile,'wb');
  if(fid == -1)
    msg = sprintf('Could not open %s for writing.',corslicefile); 
    qoe(msg); error(msg);
  end
  precision = 'uint8';
  Nv = prod(size(corslice));
  count = fwrite(fid,corslice(:),precision);
  fclose(fid); 
  if(count ~= Nv)
    fprintf(2,'ERROR: wrote %d/%d elements to %s\n',count,Nv,corslicefile);
    err = 1;
    return;
  else 
    err = 0;
  end
end

err = 0;

return;
function r = save_mgh(vol, fname, M, mr_parms);
%
% save_mgh(vol,fname, M, <mr_parms>);
%
% M is the 4x4 vox2ras transform such that
% y(i1,i2,i3), xyz = M*[i1 i2 i3 1] where the
% indicies are 0-based
%
% mr_parms = [tr flipangle te ti]
%
% See also: load_mgh, vox2ras_0to1
%
% $Id: save_mgh.m,v 1.6 2004/07/26 14:08:50 fischl Exp $
%

r = 1;

if(nargin < 2 | nargin > 4)
  msg = 'USAGE: save_mgh2(vol,fname,M)';
  return;
end

if(exist('mr_parms')~=1) mr_parms = []; end
if(isempty(mr_parms))   mr_parms = [0 0 0 0]; end
if(length(mr_parms) < 4)
  fprintf('ERROR: mr_parms length = %d, must be 4 or 5\n', ...
          length(mr_parms));
  return;
end

% These dont appear to be used %
MRI_UCHAR =  0 ;
MRI_INT =    1 ;
MRI_LONG =   2 ;
MRI_FLOAT =  3 ;
MRI_SHORT =  4 ;
MRI_BITMAP = 5 ;
MRI_TENSOR = 6 ;

fid = fopen(fname, 'wb', 'b') ;
if(fid == -1)
  fprintf('ERROR: could not open %s for writing\n',fname);
  return;
end


[ndim1,ndim2,ndim3,frames] = size(vol) ;
fwrite(fid, 1, 'int') ;         % magic #
fwrite(fid, ndim1, 'int') ; 
fwrite(fid, ndim2, 'int') ; 
fwrite(fid, ndim3, 'int') ; 
fwrite(fid, frames, 'int') ;    % # of frames
if(ndims(vol) == 5)
  is_tensor = 1 ;
  fwrite(fid, MRI_TENSOR, 'int') ; % type = MRI_TENSOR
else
  is_tensor = 0 ;
  fwrite(fid, MRI_FLOAT, 'int') ;  % type = MRI_FLOAT
end

%%?????????????%%%
fwrite(fid, 1, 'int') ;          % dof (not used)
dof = fread(fid, 1, 'int') ; 

UNUSED_SPACE_SIZE= 256;
USED_SPACE_SIZE = (3*4+4*3*4);  % space for ras transform

MdcD = M(1:3,1:3);
delta = sqrt(sum(MdcD.^2));

Mdc = MdcD./repmat(delta,[3 1]);
Pcrs_c = [ndim1/2 ndim2/2 ndim3/2 1]'; %'
Pxyz_c = M*Pcrs_c;
Pxyz_c = Pxyz_c(1:3);

fwrite(fid, 1,      'short') ;       % ras_good_flag = 1
fwrite(fid, delta,  'float32') ; 
fwrite(fid, Mdc,    'float32') ; 
fwrite(fid, Pxyz_c, 'float32') ; 

unused_space_size = UNUSED_SPACE_SIZE-2 ;
unused_space_size = unused_space_size - USED_SPACE_SIZE ;
fwrite(fid, zeros(unused_space_size,1), 'char') ;

fwrite(fid,vol,'float32');

fwrite(fid, mr_parms, 'float32') ; 
fclose(fid) ;

r = 0;

if (strcmpi(fname((length(fname)-3):length(fname)), '.MGZ') | ...
                strcmpi(fname((length(fname)-3):length(fname)), '.GZ'))

        gzipped =  round(rand(1)*10000000);
        ind = findstr(fname, '.');
        new_fname = sprintf('/tmp/tmp%d.mgh', gzipped);
        unix(sprintf('mv %s %s ; gzip %s ; mv %s.gz %s', fname, new_fname, 
new_fname, new_fname, fname)) ;
        fname = new_fname ;
end     
return;

_______________________________________________
Freesurfer mailing list
Freesurfer@nmr.mgh.harvard.edu
https://mail.nmr.mgh.harvard.edu/mailman/listinfo/freesurfer

Reply via email to