sgwt_irregular_meshmat : Adjacency matrix from irregular domain mask function A = sgwt_irregular_meshmat(mask) Computes the adjaceny matrix of graph for given 2-d irregular domain. Vertices of graph correspond to nonzero elements of mask. Edges in graph connect to (up to) 4 nearest neighbors. Inputs : mask - binary map of desired 2-d irregular domain Outputs: A - adjacency matrix of graph for irregular domain
0001 % sgwt_irregular_meshmat : Adjacency matrix from irregular domain mask 0002 % 0003 % function A = sgwt_irregular_meshmat(mask) 0004 % 0005 % Computes the adjaceny matrix of graph for given 2-d irregular 0006 % domain. Vertices of graph correspond to nonzero elements of 0007 % mask. Edges in graph connect to (up to) 4 nearest neighbors. 0008 % 0009 % Inputs : 0010 % 0011 % mask - binary map of desired 2-d irregular domain 0012 % 0013 % Outputs: 0014 % A - adjacency matrix of graph for irregular domain 0015 0016 % This file is part of the SGWT toolbox (Spectral Graph Wavelet Transform toolbox) 0017 % Copyright (C) 2010, David K. Hammond. 0018 % 0019 % The SGWT toolbox is free software: you can redistribute it and/or modify 0020 % it under the terms of the GNU General Public License as published by 0021 % the Free Software Foundation, either version 3 of the License, or 0022 % (at your option) any later version. 0023 % 0024 % The SGWT toolbox is distributed in the hope that it will be useful, 0025 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0026 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0027 % GNU General Public License for more details. 0028 % 0029 % You should have received a copy of the GNU General Public License 0030 % along with the SGWT toolbox. If not, see <http://www.gnu.org/licenses/>. 0031 0032 function A =sgwt_irregular_meshmat(mask) 0033 ind=nan(size(mask)); 0034 ind(logical(mask))=1:nnz(mask); 0035 N=nnz(mask); 0036 0037 % there will be, at most, 2*N edges 0038 % so 4*N nonzero elements in A 0039 0040 % generate list of edges 0041 % i j 1 0042 % whenever vertex i connects to vertex j 0043 i=zeros(4*N,1); 0044 j=zeros(4*N,1); 0045 0046 % Create array of indices 0047 % ni{k} are pixels that have neighbor type k 0048 % nj{k} are the inidices of pixels of the corresponding neighbor 0049 % 0050 % k=1 'top' k=2 'right' k=3 'bottom' k=4 'left' 0051 offset_list={[1 0],[0 1],[-1 0],[0 -1]}; 0052 for k=1:numel(offset_list) 0053 offset=offset_list{k}; 0054 nmask=shift(mask,offset); 0055 nind=shift(ind,offset); 0056 hnm =mask & shift(mask,offset); 0057 % hnm "has neighbor mask" is one for pixels that have the neighbor 0058 % defined by offset, zero for pixels that do not have such a neighbor 0059 ni{k}=ind(hnm); 0060 nj{k}=nind(hnm); 0061 end 0062 i=[ni{1};ni{2};ni{3};ni{4}]; 0063 j=[nj{1};nj{2};nj{3};nj{4}]; 0064 0065 A=sparse(i,j,ones(size(i)));