File: applylut.m

package info (click to toggle)
octave-image 2.12.0-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,524 kB
  • sloc: cpp: 4,191; objc: 1,014; makefile: 30; xml: 30
file content (58 lines) | stat: -rw-r--r-- 2,179 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## Copyright (C) 2004 Josep Mones i Teixidor <jmones@puntbarra.com>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{A} =} applylut (@var{BW}, @var{LUT})
## Uses lookup tables to perform a neighbour operation on binary images.
##
## A = applylut(BW,LUT) returns the result of a neighbour operation
## using the lookup table @var{LUT} which can be created by makelut.
##
## It first computes a matrix with the index of each element in the
## lookup table. To do this, it convolves the original matrix with a
## matrix which assigns each of the neighbours a bit in the resulting
## index. Then @var{LUT} is accessed to compute the result.
##
## @seealso{makelut}
## @end deftypefn

function A = applylut (BW, LUT)
  if (nargin != 2)
    print_usage;
  endif

  nq=log2(length(LUT));
  n=sqrt(nq);
  if (floor(n)!=n)
    error ("applylut: LUT length is not as expected. Use makelut to create it.");
  endif
  w=reshape(2.^[nq-1:-1:0],n,n);
  A=LUT(filter2(w,BW)+1);
endfunction

%!demo
%! lut = makelut (inline ('sum (x (:)) >= 3', 'x'), 3);
%! S = applylut (eye (5), lut);
%! disp (S)
%! ## Everything should be 0 despite a diagonal which doesn't reach borders.

## 2-by-2 test
%!assert (prod (applylut (eye (3), makelut (@(x) x(1) == 1, 2)) == eye (3)), [1 1 1]);

## 3-by-3 test
%!assert (prod (applylut (eye (3), makelut (@(x) x(2,2) == 1, 3)) == eye (3)), [1 1 1]);
%!assert (prod (applylut (eye (3), makelut (@(x) x(3,3) == 1, 3)) ==
%!              applylut (eye (3), makelut (@(x) x(2,2) == 1, 2))),
%!        [1 1 1]);