annotate toolboxes/FullBNT-1.0.7/graph/mk_2D_lattice.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function G = mk_2D_lattice(nrows, ncols, con)
Daniel@0 2 % MK_2D_LATTICE Return adjacency matrix for nearest neighbor connected 2D lattice
Daniel@0 3 % G = mk_2D_lattice(nrows, ncols, con)
Daniel@0 4 % G(k1, k2) = 1 iff k1=(i1,j1) is a neighbor of k2=(i2,j2)
Daniel@0 5 % (Two pixels are neighbors if their Euclidean distance is less than r.)
Daniel@0 6 % Default connectivity = 4.
Daniel@0 7 %
Daniel@0 8 % WE ASSUME NO WRAP AROUND.
Daniel@0 9 %
Daniel@0 10 % This is the neighborhood as a function of con:
Daniel@0 11 %
Daniel@0 12 % con=4,r=1 con=8,r=sqrt(2) con=12,r=2 con=24,r=sqrt(8)
Daniel@0 13 % nn 2nd order 4th order
Daniel@0 14 % x x x x x x
Daniel@0 15 % x x x x x x x x x x x x
Daniel@0 16 % x o x x o x x x o x x x x o x x
Daniel@0 17 % x x x x x x x x x x x x
Daniel@0 18 % x x x x x x
Daniel@0 19 %
Daniel@0 20 % Examples:
Daniel@0 21 % Consider a 3x4 grid
Daniel@0 22 % 1 4 7 10
Daniel@0 23 % 2 5 8 11
Daniel@0 24 % 3 6 9 12
Daniel@0 25 %
Daniel@0 26 % 4-connected:
Daniel@0 27 % G=mk_2D_lattice(3,4,4);
Daniel@0 28 % find(G(1,:)) = [2 4]
Daniel@0 29 % find(G(5,:)) = [2 4 6 8]
Daniel@0 30 %
Daniel@0 31 % 8-connected:
Daniel@0 32 % G=mk_2D_lattice(3,4,8);
Daniel@0 33 % find(G(1,:)) = [2 4 5]
Daniel@0 34 % find(G(5,:)) = [1 2 3 4 6 7 8 9]
Daniel@0 35
Daniel@0 36 % meshgrid trick due to Temu Gautama (temu@neuro.kuleuven.ac.be)
Daniel@0 37
Daniel@0 38 if nargin < 3, con = 4; end
Daniel@0 39
Daniel@0 40 switch con,
Daniel@0 41 case 4, r = 1;
Daniel@0 42 case 8, r = sqrt(2);
Daniel@0 43 case 12, r = 2;
Daniel@0 44 case 24, r = sqrt(8);
Daniel@0 45 otherwise, error(['unrecognized connectivity ' num2str(con)])
Daniel@0 46 end
Daniel@0 47
Daniel@0 48
Daniel@0 49 npixels = nrows*ncols;
Daniel@0 50
Daniel@0 51 [x y]=meshgrid(1:ncols, 1:nrows);
Daniel@0 52 M = [x(:) y(:)];
Daniel@0 53 M1 = repmat(reshape(M',[1 2 npixels]),[npixels 1 1]);
Daniel@0 54 M2 = repmat(M,[1 1 npixels]);
Daniel@0 55 %D = squeeze(sum(abs(M1-M2),2)); % Manhattan distance
Daniel@0 56 M3 = M1-M2;
Daniel@0 57 D = sqrt(squeeze(M3(:,1,:)) .^2 + squeeze(M3(:,2,:)) .^2); % Euclidean distance
Daniel@0 58 G = reshape(D <= r,npixels,npixels);
Daniel@0 59 G = setdiag(G, 0);