]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/radon.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / radon.m
1 ## Copyright (C) 2007 Alexander Barth <barth.alexander@gmail.com>
2 ##
3 ##
4 ## This program is free software; you can redistribute it and/or modify it
5 ## under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 3 of the License, or (at
7 ## your option) any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but
10 ## WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ## General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; see the file COPYING.  If not, see
16 ## <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {[@var{RT},@var{xp}] =} radon(@var{I}, @var{theta})
20 ## @deftypefnx {Function File} {[@var{RT},@var{xp}] =} radon(@var{I})
21 ##
22 ## Calculates the 2D-Radon transform of the matrix @var{I} at angles given in
23 ## @var{theta}. To each element of @var{theta} corresponds a column in @var{RT}. 
24 ## The variable @var{xp} represents the x-axis of the rotated coordinate.
25 ## If @var{theta} is not defined, then 0:179 is assumed.
26 ## @end deftypefn
27
28
29 function [RT,xp] = radon (I,theta)
30
31   ## Input checking
32   if (nargin == 0 || nargin > 2)
33     print_usage ();
34   elseif (nargin == 1)
35     theta = 0:179;
36   endif
37   
38   if (!ismatrix(I) || ndims(I) != 2)
39     error("radon: first input must be a MxN matrix");
40   endif
41   
42   if (!isvector(theta))
43     error("radon: second input must be a vector");
44   endif
45
46   [m, n] = size (I);
47
48   # center of image
49   xc = floor ((m+1)/2);
50   yc = floor ((n+1)/2);
51
52   # divide each pixel into 2x2 subpixels
53
54   d = reshape (I,[1 m 1 n]);
55   d = d([1 1],:,[1 1],:);
56   d = reshape (d,[2*m 2*n])/4;
57
58   b = ceil (sqrt (sum (size (I).^2))/2 + 1);
59   xp = [-b:b]';
60   sz = size(xp);
61
62   [X,Y] = ndgrid (0.75 - xc + [0:2*m-1]/2,0.75 - yc + [0:2*n-1]/2);
63
64   X = X(:)';
65   Y = Y(:)';
66   d = d(:)';
67
68   th = theta*pi/180;
69
70   for l=1:length (theta)
71     # project each pixel to vector (-sin(th),cos(th))
72     Xp = -sin (th(l)) * X + cos (th(l)) * Y;
73     
74     ip = Xp + b + 1;
75
76     k = floor (ip);
77     frac = ip-k;
78
79     RT(:,l) = accumarray (k',d .* (1-frac),sz) + accumarray (k'+1,d .* frac,sz);
80   endfor
81
82 endfunction
83
84 %!test
85 %! A = radon(ones(2,2),30);
86 %! assert (A,[0 0 0.608253175473055 2.103325780167649 1.236538105676658 0.051882938682637 0]',1e-10)
87
88