comparison toolboxes/FullBNT-1.0.7/graph/mk_2D_lattice.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function G = mk_2D_lattice(nrows, ncols, con)
2 % MK_2D_LATTICE Return adjacency matrix for nearest neighbor connected 2D lattice
3 % G = mk_2D_lattice(nrows, ncols, con)
4 % G(k1, k2) = 1 iff k1=(i1,j1) is a neighbor of k2=(i2,j2)
5 % (Two pixels are neighbors if their Euclidean distance is less than r.)
6 % Default connectivity = 4.
7 %
8 % WE ASSUME NO WRAP AROUND.
9 %
10 % This is the neighborhood as a function of con:
11 %
12 % con=4,r=1 con=8,r=sqrt(2) con=12,r=2 con=24,r=sqrt(8)
13 % nn 2nd order 4th order
14 % x x x x x x
15 % x x x x x x x x x x x x
16 % x o x x o x x x o x x x x o x x
17 % x x x x x x x x x x x x
18 % x x x x x x
19 %
20 % Examples:
21 % Consider a 3x4 grid
22 % 1 4 7 10
23 % 2 5 8 11
24 % 3 6 9 12
25 %
26 % 4-connected:
27 % G=mk_2D_lattice(3,4,4);
28 % find(G(1,:)) = [2 4]
29 % find(G(5,:)) = [2 4 6 8]
30 %
31 % 8-connected:
32 % G=mk_2D_lattice(3,4,8);
33 % find(G(1,:)) = [2 4 5]
34 % find(G(5,:)) = [1 2 3 4 6 7 8 9]
35
36 % meshgrid trick due to Temu Gautama (temu@neuro.kuleuven.ac.be)
37
38 if nargin < 3, con = 4; end
39
40 switch con,
41 case 4, r = 1;
42 case 8, r = sqrt(2);
43 case 12, r = 2;
44 case 24, r = sqrt(8);
45 otherwise, error(['unrecognized connectivity ' num2str(con)])
46 end
47
48
49 npixels = nrows*ncols;
50
51 [x y]=meshgrid(1:ncols, 1:nrows);
52 M = [x(:) y(:)];
53 M1 = repmat(reshape(M',[1 2 npixels]),[npixels 1 1]);
54 M2 = repmat(M,[1 1 npixels]);
55 %D = squeeze(sum(abs(M1-M2),2)); % Manhattan distance
56 M3 = M1-M2;
57 D = sqrt(squeeze(M3(:,1,:)) .^2 + squeeze(M3(:,2,:)) .^2); % Euclidean distance
58 G = reshape(D <= r,npixels,npixels);
59 G = setdiag(G, 0);