Code covered by the BSD License  

Highlights from
2d and 3d brain plots

image thumbnail
from 2d and 3d brain plots by Jeremy Manning
Quickly and easily create 2d and 3d plots of fMRI data.

plot_brain2d(x,meta,nrow,ncol,dim,c,s)
function[] = plot_brain2d(x,meta,nrow,ncol,dim,c,s)
%PLOT_BRAIN2D  Plot slices of a brain image
%
% Usage: plot_brain2d(x,meta,[nrow],[ncol],[dim],[clim],[in_roi])
%
% INPUTS:
%      x: a 1 by nvoxels vector of voxel activations
%
%   meta: a struct with the following fields:
%           dimx: number of voxels in the x dimension
%           dimy: number of voxels in the y dimension
%           dimz: number of voxels in the z dimension
%        nvoxels: total number of voxels containing brain
%     coordToCol: dimx by dimy by dimz matrix of voxel numbers (zeros
%                 indicate no voxel at the corresponding location)
%     colToCoord: nvoxels by 3 matrix of voxel locations
%
%      X: design matrix (ntrials by ncovariates) containing the activation of
%         each covariate on each trial.
%
%   nrow: an optional argument specifying the number of rows of brain
%         images (default: 4)
% 
%   ncol: an optional argument specifying the number of columns of brain
%         images (default: 4)
%
%    dim: optional argument specifying the plane along which the 3d brain
%         image will be sliced. default: 3 (coronal slices).  options: 1
%         (sagittal), 2 (horizontal), 3 (coronal).
%
%   clim: optional argument specifying upper and lower bounds of the color
%         axis.  minimum value is mapped to blue; upper is mapped to red.
%
% in_roi: optional argument specifying a 1 by nvoxels logical vector
%         indicating which voxels are in the ROI.  these voxels will be
%         outlined in black (if unspecified, the entire brain is outlined).
%
% OUTPUTS: [none]
%
% SEE ALSO: PLOT_BRAIN2D_ALT, PLOT_BRAIN3D, SANEPCOLOR, SLICES,
%           GETTIGHTSUBPLOTHANDLES, IMCONTOUR, SUBPLOT, PCOLOR,
%           IMAGESC, JET
%
%  AUTHOR: Jeremy R. Manning
% CONTACT: manning3@princeton.edu

% CHANGELOG:
% 4-11-12  jrm  wrote it.
% 2-22-13  jrm  minor edits to comments.

warning('off','MATLAB:contour:ConstantData');

assert(length(x) == meta.nvoxels,'meta.nvoxels not equal to length(data)');
if ~exist('nrow','var') || isempty(nrow)
    nrow = 4;
end
if ~exist('ncol','var') || isempty(ncol)
    ncol = 4;
end
if ~exist('dim','var') || isempty(dim)
    dim = 3;
end
if ~exist('s','var') || isempty(s);
    s = true(size(x));
end
clf;

img = meta.coordToCol;
img(img == 0) = nan;
select_img = nan(size(img));
for i = 1:meta.nvoxels
    img(meta.colToCoord(i,1),meta.colToCoord(i,2),meta.colToCoord(i,3)) = x(i);
    if s(i)
        select_img(meta.colToCoord(i,1),meta.colToCoord(i,2),meta.colToCoord(i,3)) = x(i);
    end
end
nslices = min(nrow*ncol,size(img,dim));
brain_slices = slices(img,dim);
select_slices = slices(select_img,dim);

slice_edges = linspace(1,size(img,dim),nslices+1);
which_slices = mean([slice_edges(1:end-1) ; slice_edges(2:end)],1);
which_slices = unique(round(which_slices));

hs = getTightSubplotHandles(0.01,0.01,0.01,nrow,ncol);

if ~exist('c','var') || isempty(c)
    c = [prctile(img(:),10) prctile(img(:),90)];
end
for i = 1:length(hs)
    axes(hs(i)); %#ok<LAXES>
    if i <= length(which_slices)
        plot_helper(flipud(squeeze(brain_slices{which_slices(i)})'),flipud(squeeze(select_slices{which_slices(i)})'),c);%,xdim,ydim);        
    else        
        axis off;
    end
end

function[] = plot_helper(img,select_img,c)
hold on;
sanePColor(img); 
try
    caxis(c); 
end
colormap jet;
se = strel('arbitrary',ones([1 1]));
[~,h] = imcontour(imdilate(~isnan(select_img),se),1);
set(h,'LineColor','k','LineWidth',2);
hold off;
axis tight;
axis off;

Contact us