Spectral Graph Wavelet Transform, unsure what is wrong with demo file not working

34 Views Asked by At

I'm trying to explore sgwt, and I decided the first step is try to understand the sgwt toolbox for matlab. I tried running the code for sgwt_demo3, but it gives me an error. For reference, the code for sgwt_demo3 is:

% sgwt_demo3 : Image decomposition with SGWT wavelets based on local adjacency.
%
% This demo builds the SGWT transform on a graph representing 
% adjacency on a pixel mesh with 4-nearest neighbor connectivity.
% This demonstrates inverse on problem with large dimension.
%
% The demo loads an image file and decomposes the image with the SGWT,
% showing the coefficients as images at each scale. The demo does not show
% the individual wavelets (this could be done by replacing the input 
% image by a "delta image" with a single unit nonzero pixel) .
%
% The inverse is then computed, from the original coefficients as well as 
% from a modified set of coefficients where only coefficients at one
% scale are preserved. This shows that the SGWT can generate a
% multiresolution decomposition for images. We don't claim that this
% particular local-adjacency based transform is better for image
% processing than other available wavelet image decompositions, but it
% demonstrates the flexibility of the SGWT.  

function sgwt_demo3
global SGWT_ROOT
close all;
fprintf('Welcome to SGWT demo #3\n');
% load image
imname = [fileparts(mfilename('fullpath')),filesep,'paques_attack.png'];

fprintf('loading image %s\n',imname);
im = double( imread(imname) );
% build mesh adjacency graph
fprintf('Building mesh adjacency graph\n');
A=sgwt_meshmat(size(im));
% transform
fprintf('Calculating graph Laplacian\n');
L=sgwt_laplacian(A);
fprintf('Measuring largest eigenvalue, lmax = ');
lmax=sgwt_rough_lmax(L);
arange=[0,lmax];
fprintf('%g\n',lmax);

Nscales=5;
fprintf('Designing transform in spectral domain\n');
[g,t]=sgwt_filter_design(lmax,Nscales);

m=25; % order of polynomial approximation
fprintf('Computing Chebyshev polynomials of order %g for fast transform \n',m);
for k=1:numel(g)
    c{k}=sgwt_cheby_coeff(g{k},m,m+1,arange);
end

fprintf('Computing forward transform\n');
wpall=sgwt_cheby_op(im(:),L,c,arange);

% invert with all subbands
fprintf('Computing inverse transform with all coefficients\n');
imr1=sgwt_inverse(wpall,L,c,arange);
imr1=reshape(imr1,size(im));

ks=3; % scale at which to keep coefficients, set all others to zero.
fprintf('\nsetting all coefficients to zero except wavelet scale %g\n',ks-1);
% invert with only one scale
for k=1:numel(wpall)
    wpall2{k}=zeros(size(wpall{k}));
end
wpall2{ks}=wpall{ks};
fprintf('Computing inverse transform with coefficients from wavelet scale %g only\n',ks-1);
imr2=sgwt_inverse(wpall2,L,c,arange);
imr2=reshape(imr2,size(im));

%% display results
figure(1)
set(gcf,'position',[ 5   730   350   350]);
sgwt_show_im(im)
title('original image');
set(gcf,'menubar','none')

figure(2)
set(gcf,'position',[365 730 350 350]);
sgwt_show_im(imr1)
title('reconstuction from all coefficients');
set(gcf,'menubar','none')

figure(3)
set(gcf,'position',[725 730 350 350]);
sgwt_show_im(imr2);
title(sprintf('reconstruction only from wavelets at scale %g',ks-1));
set(gcf,'menubar','none')

figure(4)
set(gcf,'position',[0 0 1150 700]);
set(gcf,'menubar','none')
for k=1:Nscales+1
    subplot(2,3,k);
    sgwt_show_im(reshape(wpall{k},size(im)));
    if k==1
        title('Scaling function coefficients');
    else
        title(sprintf('Wavelet coefficients at scale %g',k-1));
    end
end

The error it gives is:

Error using boundary
Not enough input arguments.

Error in sgwt_meshmat (line 48)
  switch boundary

Error in sgwt_demo3 (line 47)
A=sgwt_meshmat(size(im));

I tried checking what is inside the sgwt_meshmat function, and this is what's inside:

% sgwt_meshmat : Adjacency matrix for regular 2d mesh 
%
% function A=meshmat_p(dim,varargin)
%
% Inputs:
% dim - size of 2d mesh 
% Selectable control parameters:
% boundary - 'rectangle' or 'torus'
%
% Outputs:
% A - adjacency matrix

function A=sgwt_meshmat(dim,varargin)
  control_params={'boundary','rectangle'};
  argselectAssign(control_params);
  argselectCheck(control_params,varargin);
  argselectAssign(varargin);
  if (numel(dim)==1)
    dim=[1 1]*dim;
  end
  % build adjacency matrix : find i,j coordinates of center points
  % and right and bottom neighbors, then build connectivity matrix.
  % For each valid center,neighbor pair, will add A(center,neighbor)=1
  % and A(neighbor,center)=1, so A will be symmetric
  N=prod(dim);
  [alli,allj]=find(ones(dim));
  % (ci(k),cj(k)) has neighbor (ni(k),nj(k))
  ci=[alli;alli];
  cj=[allj;allj];
  ni=[alli  ; alli+1];
  nj=[allj+1; allj];
  switch boundary
   case 'rectangle' 
    % prune edges at boundary
    valid=(ni>=1 & ni<=dim(1) & nj>=1 & nj<=dim(2));
    ni=ni(valid);
    nj=nj(valid);
    ci=ci(valid);
    cj=cj(valid);
    cind=dim(1)*(cj-1)+ci;
    nind=dim(1)*(nj-1)+ni;
   case 'torus'
    % wrap indices to make torus
    ni=mod(ni,dim(1))+1;
    nj=mod(nj,dim(2))+1;    
    cind=dim(1)*(cj-1)+ci;
    nind=dim(1)*(nj-1)+ni;
   otherwise
    error('unknown boundary option');
  end
  % assemble connection matrix
  A=sparse([cind,nind],[nind,cind],ones(1,2*numel(ni)),N,N);

From the error message, the error seems to arise from the boundary function.

I tried changing this line A=sgwt_meshmat(size(im)); to this

boundary = 'rectangle';  % or 'torus' depending on your needs
A = sgwt_meshmat(size(im), 'boundary', boundary);

but it gives the same error message. To be honest, I'm not that knowledgeable in matlab coding, so I'm really hoping someone could help me or at least point me in the right direction.

For reference, the sgwt toolbox is also be viewed here: https://github.com/ChunyuanLI/spectral_descriptors/tree/master/sgwt_toolbox

Edit: I use Matlab 2022b. As per the comment below, I tried downgrading my Matlab to 2015a, and when it gave me the same error. I downgraded to Matlab 2010 and downloaded SGWTtoolbox1.00 which was the original release. After trying to further explore the code, I found that the problem is really due to the sgwt_meshmat function, but I can't figure out what's wrong.

Thank you so much. Sorry for the lengthy post.

0

There are 0 best solutions below