Daniel@0: function y = convolve2(x, m, shape, tol) Daniel@0: %CONVOLVE2 Two dimensional convolution. Daniel@0: % Y = CONVOLVE2(X, M) performs the 2-D convolution of matrices X and Daniel@0: % M. If [mx,nx] = size(X) and [mm,nm] = size(M), then size(Y) = Daniel@0: % [mx+mm-1,nx+nm-1]. Values near the boundaries of the output array are Daniel@0: % calculated as if X was surrounded by a border of zero values. Daniel@0: % Daniel@0: % Y = CONVOLVE2(X, M, SHAPE) where SHAPE is a string returns a Daniel@0: % subsection of the 2-D convolution with size specified by SHAPE: Daniel@0: % Daniel@0: % 'full' - (default) returns the full 2-D convolution, Daniel@0: % 'same' - returns the central part of the convolution Daniel@0: % that is the same size as A (using zero padding), Daniel@0: % 'valid' - returns only those parts of the convolution Daniel@0: % that are computed without the zero-padded Daniel@0: % edges, size(Y) = [mx-mm+1,nx-nm+1] when Daniel@0: % size(X) > size(M), Daniel@0: % 'wrap' - as for 'same' except that instead of using Daniel@0: % zero-padding the input A is taken to wrap round as Daniel@0: % on a toroid. Daniel@0: % 'reflect' - as for 'same' except that instead of using Daniel@0: % zero-padding the input A is taken to be reflected Daniel@0: % at its boundaries. Daniel@0: % Daniel@0: % CONVOLVE2 is fastest when mx > mm and nx > nm - i.e. the first Daniel@0: % argument is the input and the second is the mask. Daniel@0: % Daniel@0: % If the rank of the mask M is low, CONVOLVE2 will decompose it into a Daniel@0: % sum of outer product masks, each of which is applied efficiently as Daniel@0: % convolution with a row vector and a column vector, by calling CONV2. Daniel@0: % The function will often be faster than CONV2 or FILTER2 (in some Daniel@0: % cases much faster) and will produce the same results as CONV2 to Daniel@0: % within a small tolerance. Daniel@0: % Daniel@0: % Y = CONVOLVE2(... , TOL) where TOL is a number in the range 0.0 to Daniel@0: % 1.0 computes the convolution using a reduced-rank approximation to Daniel@0: % M, provided this will speed up the computation. TOL limits the Daniel@0: % relative sum-squared error in the effective mask; that is, if the Daniel@0: % effective mask is E, the error is controlled such that Daniel@0: % Daniel@0: % sum(sum( (M-E) .* (M-E) )) Daniel@0: % -------------------------- <= TOL Daniel@0: % sum(sum( M .* M )) Daniel@0: % Daniel@0: % See also CONV2, FILTER2. Daniel@0: Daniel@0: % David Young, Department of Informatics, University of Sussex, February 2002, Daniel@0: % revised January 2005. Daniel@0: Daniel@0: % Deal with optional arguments Daniel@0: error(nargchk(2,4,nargin)); Daniel@0: if nargin < 3 Daniel@0: shape = 'full'; % shape default as for CONV2 Daniel@0: tol = 0; Daniel@0: elseif nargin < 4 Daniel@0: if isnumeric(shape) Daniel@0: tol = shape; Daniel@0: shape = 'full'; Daniel@0: else Daniel@0: tol = 0; Daniel@0: end Daniel@0: end; Daniel@0: Daniel@0: % Set up to do the wrap & reflect operations, not handled by conv2 Daniel@0: if strcmp(shape, 'wrap') Daniel@0: x = wraparound(x, m); Daniel@0: shape = 'valid'; Daniel@0: elseif strcmp(shape, 'reflect') Daniel@0: x = reflectborders(x, m); Daniel@0: shape = 'valid'; Daniel@0: end Daniel@0: Daniel@0: % do the convolution itself Daniel@0: y = doconv(x, m, shape, tol); Daniel@0: Daniel@0: %----------------------------------------------------------------------- Daniel@0: Daniel@0: function y = doconv(x, m, shape, tol); Daniel@0: % Carry out convolution Daniel@0: [mx, nx] = size(x); Daniel@0: [mm, nm] = size(m); Daniel@0: Daniel@0: % If the mask is bigger than the input, or it is 1-D already, Daniel@0: % just let CONV2 handle it. Daniel@0: if mm > mx | nm > nx | mm == 1 | nm == 1 Daniel@0: y = conv2(x, m, shape); Daniel@0: else Daniel@0: % Get svd of mask Daniel@0: if mm < nm; m = m'; end % svd(..,0) wants m > n Daniel@0: [u,s,v] = svd(m, 0); Daniel@0: s = diag(s); Daniel@0: rank = trank(m, s, tol); Daniel@0: if rank*(mm+nm) < mm*nm % take advantage of low rank Daniel@0: if mm < nm; t = u; u = v; v = t; end % reverse earlier transpose Daniel@0: vp = v'; Daniel@0: % For some reason, CONV2(H,C,X) is very slow, so use the normal call Daniel@0: y = conv2(conv2(x, u(:,1)*s(1), shape), vp(1,:), shape); Daniel@0: for r = 2:rank Daniel@0: y = y + conv2(conv2(x, u(:,r)*s(r), shape), vp(r,:), shape); Daniel@0: end Daniel@0: else Daniel@0: if mm < nm; m = m'; end % reverse earlier transpose Daniel@0: y = conv2(x, m, shape); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %----------------------------------------------------------------------- Daniel@0: Daniel@0: function r = trank(m, s, tol) Daniel@0: % Approximate rank function - returns rank of matrix that fits given Daniel@0: % matrix to within given relative rms error. Expects original matrix Daniel@0: % and vector of singular values. Daniel@0: if tol < 0 | tol > 1 Daniel@0: error('Tolerance must be in range 0 to 1'); Daniel@0: end Daniel@0: if tol == 0 % return estimate of actual rank Daniel@0: tol = length(m) * max(s) * eps; Daniel@0: r = sum(s > tol); Daniel@0: else Daniel@0: ss = s .* s; Daniel@0: t = (1 - tol) * sum(ss); Daniel@0: r = 0; Daniel@0: sm = 0; Daniel@0: while sm < t Daniel@0: r = r + 1; Daniel@0: sm = sm + ss(r); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %----------------------------------------------------------------------- Daniel@0: Daniel@0: function y = wraparound(x, m) Daniel@0: % Extend x so as to wrap around on both axes, sufficient to allow a Daniel@0: % "valid" convolution with m to return the cyclical convolution. Daniel@0: % We assume mask origin near centre of mask for compatibility with Daniel@0: % "same" option. Daniel@0: [mx, nx] = size(x); Daniel@0: [mm, nm] = size(m); Daniel@0: if mm > mx | nm > nx Daniel@0: error('Mask does not fit inside array') Daniel@0: end Daniel@0: Daniel@0: mo = floor((1+mm)/2); no = floor((1+nm)/2); % reflected mask origin Daniel@0: ml = mo-1; nl = no-1; % mask left/above origin Daniel@0: mr = mm-mo; nr = nm-no; % mask right/below origin Daniel@0: me = mx-ml+1; ne = nx-nl+1; % reflected margin in input Daniel@0: mt = mx+ml; nt = nx+nl; % top of image in output Daniel@0: my = mx+mm-1; ny = nx+nm-1; % output size Daniel@0: Daniel@0: y = zeros(my, ny); Daniel@0: y(mo:mt, no:nt) = x; % central region Daniel@0: if ml > 0 Daniel@0: y(1:ml, no:nt) = x(me:mx, :); % top side Daniel@0: if nl > 0 Daniel@0: y(1:ml, 1:nl) = x(me:mx, ne:nx); % top left corner Daniel@0: end Daniel@0: if nr > 0 Daniel@0: y(1:ml, nt+1:ny) = x(me:mx, 1:nr); % top right corner Daniel@0: end Daniel@0: end Daniel@0: if mr > 0 Daniel@0: y(mt+1:my, no:nt) = x(1:mr, :); % bottom side Daniel@0: if nl > 0 Daniel@0: y(mt+1:my, 1:nl) = x(1:mr, ne:nx); % bottom left corner Daniel@0: end Daniel@0: if nr > 0 Daniel@0: y(mt+1:my, nt+1:ny) = x(1:mr, 1:nr); % bottom right corner Daniel@0: end Daniel@0: end Daniel@0: if nl > 0 Daniel@0: y(mo:mt, 1:nl) = x(:, ne:nx); % left side Daniel@0: end Daniel@0: if nr > 0 Daniel@0: y(mo:mt, nt+1:ny) = x(:, 1:nr); % right side Daniel@0: end Daniel@0: Daniel@0: %----------------------------------------------------------------------- Daniel@0: Daniel@0: function y = reflectborders(x, m) Daniel@0: % Extend x so as to reflect at each boundary, sufficient to allow a Daniel@0: % "valid" convolution with m to return a matrix the same size as Daniel@0: % the orginal. Daniel@0: % We assume mask origin near centre of mask for compatibility with Daniel@0: % "same" option. Daniel@0: [mx, nx] = size(x); Daniel@0: [mm, nm] = size(m); Daniel@0: if mm > mx | nm > nx Daniel@0: error('Mask does not fit inside array') Daniel@0: end Daniel@0: Daniel@0: mo = floor((1+mm)/2); no = floor((1+nm)/2); % reflected mask origin Daniel@0: ml = mo-1; nl = no-1; % mask left/above origin Daniel@0: mr = mm-mo; nr = nm-no; % mask right/below origin Daniel@0: me = mx-mr+1; ne = nx-nr+1; % translated margin in input Daniel@0: mt = mx+ml; nt = nx+nl; % top/right of image in output Daniel@0: my = mx+mm-1; ny = nx+nm-1; % output size Daniel@0: Daniel@0: y = zeros(my, ny); Daniel@0: y(mo:mt, no:nt) = x; % central region Daniel@0: if ml > 0 Daniel@0: y(1:ml, no:nt) = x(ml:-1:1, :); % top side Daniel@0: if nl > 0 Daniel@0: y(1:ml, 1:nl) = x(ml:-1:1, nl:-1:1); % top left corner Daniel@0: end Daniel@0: if nr > 0 Daniel@0: y(1:ml, nt+1:ny) = x(ml:-1:1, nx:-1:ne); % top right corner Daniel@0: end Daniel@0: end Daniel@0: if mr > 0 Daniel@0: y(mt+1:my, no:nt) = x(mx:-1:me, :); % bottom side Daniel@0: if nl > 0 Daniel@0: y(mt+1:my, 1:nl) = x(mx:-1:me, nl:-1:1); % bottom left corner Daniel@0: end Daniel@0: if nr > 0 Daniel@0: y(mt+1:my, nt+1:ny) = x(mx:-1:me, nx:-1:ne); % bottom right corner Daniel@0: end Daniel@0: end Daniel@0: if nl > 0 Daniel@0: y(mo:mt, 1:nl) = x(:, nl:-1:1); % left side Daniel@0: end Daniel@0: if nr > 0 Daniel@0: y(mo:mt, nt+1:ny) = x(:, nx:-1:ne); % right side Daniel@0: end