# 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;```