]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/applylut.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / applylut.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{A} = } applylut (@var{BW},@var{LUT})
18 ## Uses lookup tables to perform a neighbour operation on binary images.
19 ##
20 ## A = applylut(BW,LUT) returns the result of a neighbour operation
21 ## using the lookup table @var{LUT} which can be created by makelut.
22 ##
23 ## It first computes a matrix with the index of each element in the
24 ## lookup table. To do this, it convolves the original matrix with a
25 ## matrix which assigns each of the neighbours a bit in the resulting
26 ## index. Then @var{LUT} is accessed to compute the result.
27 ##
28 ## @seealso{makelut}
29 ## @end deftypefn
30
31 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
32
33 function A = applylut(BW, LUT)
34   if (nargin != 2)
35     usage ("A = applylut(BW, LUT)");
36   endif
37
38   nq=log2(length(LUT));
39   n=sqrt(nq);
40   if (floor(n)!=n)
41     error ("applylut: LUT length is not as expected. Use makelut to create it.");
42   endif
43   w=reshape(2.^[nq-1:-1:0],n,n);
44   A=LUT(filter2(w,BW)+1);
45 endfunction
46
47 %!demo
48 %! lut = makelut (inline ('sum (x (:)) >= 3', 'x'), 3);
49 %! S = applylut (eye (5), lut);
50 %! disp (S)
51 %! ## Everything should be 0 despite a diagonal which
52 %! ## doesn't reach borders.
53
54
55 %!assert(prod(applylut(eye(3),makelut(inline('x(1,1)==1','x'),2))==eye(3))==1); % 2-by-2 test
56 %!assert(prod(applylut(eye(3),makelut(inline('x(2,2)==1','x'),3))==eye(3))==1); % 3-by-3 test
57 %!assert(prod(applylut(eye(3),makelut(inline('x(3,3)==1','x'),3))== \
58 %!            applylut(eye(3),makelut(inline('x(2,2)==1','x'),2)))==1);
59
60
61
62