SPM99 Gem 4: Histogram of an image

```Subject: Re: t-distribution display (histogram) with spm99b
From: john@fil.ion.ucl.ac.uk (John Ashburner)
Date: Fri, 30 Jul 1999 14:06:20 +0100
To: spm@mailbase.ac.uk, jovicich@humc.edu

The attached program should produce the histograms you are after.  To
call it, type:

V      = spm_vol(spm_get(1,'*.img','Select image...'));
[n, x] = histvol(V, 100);
figure;
bar(x,n);
```

[…]

```Regards,
-John
```

The attached function is here histvol.m and below

```function [n, x]=histvol(V, nbins)
% Create Histogram of an image volume
% FORMAT [n, x]=histvol(V, nbins)
% V     - mapped image volume (see spm_vol)
% nbins - number of bins to use.
% n     - number of counts in each bin
% x     - position of bin centres
%_______________________________________________________________________
% @(#)JohnsGems.html	1.42 05/02/02

if nargin==1, nbins = 256; end;

% determine range...
mx = -Inf;
mn =  Inf;
for p=1:V.dim(3),
img = spm_slice_vol(V,spm_matrix([0 0 p]),V.dim(1:2),1);
msk = find(isfinite(img));
mx  = max([max(img(msk)) mx]);
mn  = min([min(img(msk)) mn]);
end;

% compute histograms...
x = [mn:(mx-mn+1)/nbins:mx];
n = zeros(size(x));
for p=1:V.dim(3),
img = spm_slice_vol(V,spm_matrix([0 0 p]),V.dim(1:2),1);
msk = find(isfinite(img));
n   = n+hist(img(msk),x);
end;
return;```

SPM99 Gem 3: NaNing zero values

```Subject: Re: normalization of contrast images
From: John Ashburner
Date: Wed, 2 Aug 2000 14:37:38 +0100 (BST)
To: spm@mailbase.ac.uk, gpagnon@emory.edu
```

[…]

```| And, while I am at it, does anybody know how to convert a mask that
| has zeros outside the brain to a mask that has NaN outside the brain?
| I tried ImCalc with something like 'i1(find(i1==0))=NaN', but it
| doesn't like it.

By default, ImCalc outputs the data as 16 bit integer with scalefactors.
There is no NaN representation for this, so the data would need to be
written as floating point or double precision floating point.  I think
you can do this by typing something like:
P     = spm_get(1,'*.img');
Q     = 'output.img';
f     = 'i1.*(i1./i1)';
flags = {0,0,spm_type('float'),1};
spm_imcalc_ui(P,Q,f,flags);

Best regards,
-John
```

Note you can NaN-out voxels below a threshold, say, 3:

f = 'i1 + 0./(i1>3)'

Also note that this is a general way for making spm_imcalc write out images with float or double precision; concisely

f = 'sqrt(i1)'; % whatever you like

spm_imcalc_ui('in.img','out.img',f,{[],[],spm_type('float')});

Using  [],[] instead of  0,0 ensures that default values will be used for those two flags.

SPM99 Gem 2: Zeroing NaN values

In addition to setting NaN values to zero, this gem is also useful as a basic skeleton for reading each slice, doing something to it, and then writing it out.

```Subject: Re: NaN values in beta values
From: John Ashburner
Date: Mon, 23 Oct 2000 17:27:24 +0100 (BST)
To: spm@mailbase.ac.uk, steffejr@umdnj.edu

I can't think of any nice way of doing this with ImCalc, so I'm afraid
I'll have to show you a more labour intensive method that should be
modified slightly (as the filenames need changing) before pasting into
Matlab.

VI       = spm_vol('original_beta.img');
VO       = VI;
VO.fname = 'patched_beta.img';
VO       = spm_create_image(VO);
for i=1:VI.dim(3),
img      = spm_slice_vol(VI,spm_matrix([0 0 i]),VI.dim(1:2),0);
tmp      = find(isnan(img));
img(tmp) = 0;
VO       = spm_write_plane(VO,img,i);
end;
```

SPM99 Gem 1: Adding blobs to an image

Say you have a statistic image that you created outside of SPM and you want to overlay it on a structural image. The bare bones solution is as follows.

Create a thresholded version of the image, where all subthreshold voxels are set to NaN (See how to set NaNs below). Then…

```Subject: Re: Your Message Sent on Fri, 19 Jan 2001 23:28:45 -0800
From: John Ashburner
Date: Mon, 22 Jan 2001 11:40:24 +0000
To: SPM@JISCMAIL.AC.UK

I think it may take quite a bit of programming to get most of
the routines working with your data.  One thing to get you
started would be to try an undocumented feature of spm_orthviews.m:

spm_orthviews('image',spm_get(1,'*.img','Select background image'));

Best regards,
-John
```

Here’s a different approach, which I understand will show the blobs in a monotone color (instead of hot metal). Also, there are more bells and whistles (it’s less of a hack).

```Subject: Re: overlaying resulting image
From: John Ashburner
Date: Wed, 4 Oct 2000 10:37:01 +0100 (BST)
To: spm@mailbase.ac.uk, duann@salk.edu

| Would you please show me how to overlay a statistical map onto the brain
| template SPM uses. Let's say I have an analyze-formated statistical
| map obtained from other software. It was already normalized to the same
| coordinates as the template images SPM has. How can I overlay this image
| onto the SPM template just like the results shown in SPM convention.

The following is supposed to work.  It uses a hidden undocumented
feature in SPM99, so it may contain bugs....

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

P1 = spm_get(1,'*.img','Specify background image');
P2 = spm_get(1,'*.img','Specify blobs image');

% Clear graphics window..
spm_clf

% Display background image..
h = spm_orthviews('Image', P1,[0.05 0.05 0.9 0.9]);

% Display blobs in red.  Use [0 1 0] for green, [0 0 1] for blue
% [0.6 0 0.8] for purple etc..