Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slimginterp
Home > sltoolbox > imgproc > slimginterp.m

slimginterp

PURPOSE ^

SLIMGINTERP Performs image based interpolation

SYNOPSIS ^

function V = slimginterp(A, I, J, interpker)

DESCRIPTION ^

SLIMGINTERP Performs image based interpolation 

 $ Syntax $
   - V = slimginterp(A, I, J)
   - V = slimginterp(A, I, J, interpker)

 $ Arguments $
   - A:            The reference image array
   - I, J:         The coordinates at which the values are interpolated
                   The sizes of I and J should be exactly the same
   - interpker:    The interpolation kernel: default = 'linear'.
                   Please refer to slgetinterpkernel for details.
 
 $ Description $
   - V = slimginterp(A, I, J) performs interpolation on the given
     positions specified by I and J using the default interpolator.
     Suppose A is an array of h x w x n1 x n2 x ... nm, and X and Y
     have size s1 x s2 x ... x sd. Then the output array V would be
     of size s1 x s2 x ... x sd x n1 x n2 x ... nm. 

   - V = slimginterp(A, I, J, interpker) performs interpolation on
     the given positions using specified interpolator.

 $ History $
   - Created by Dahua Lin, on Sep 2nd, 2006
   - Modified by Dahua Lin, on Sep 10, 2006
       - use sladdvec to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdvec SLADDVEC adds a vector to columns or rows of a matrix
  • slgetinterpkernel SLGETINTERPKERNEL Gets the interpolation kernel function
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:
  • slresizeimg SLRESIZEIMG Resizes the images by interpolation

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function V = slimginterp(A, I, J, interpker)
0002 %SLIMGINTERP Performs image based interpolation
0003 %
0004 % $ Syntax $
0005 %   - V = slimginterp(A, I, J)
0006 %   - V = slimginterp(A, I, J, interpker)
0007 %
0008 % $ Arguments $
0009 %   - A:            The reference image array
0010 %   - I, J:         The coordinates at which the values are interpolated
0011 %                   The sizes of I and J should be exactly the same
0012 %   - interpker:    The interpolation kernel: default = 'linear'.
0013 %                   Please refer to slgetinterpkernel for details.
0014 %
0015 % $ Description $
0016 %   - V = slimginterp(A, I, J) performs interpolation on the given
0017 %     positions specified by I and J using the default interpolator.
0018 %     Suppose A is an array of h x w x n1 x n2 x ... nm, and X and Y
0019 %     have size s1 x s2 x ... x sd. Then the output array V would be
0020 %     of size s1 x s2 x ... x sd x n1 x n2 x ... nm.
0021 %
0022 %   - V = slimginterp(A, I, J, interpker) performs interpolation on
0023 %     the given positions using specified interpolator.
0024 %
0025 % $ History $
0026 %   - Created by Dahua Lin, on Sep 2nd, 2006
0027 %   - Modified by Dahua Lin, on Sep 10, 2006
0028 %       - use sladdvec to increase efficiency
0029 %
0030 
0031 %% parse and verify input arguments
0032 
0033 if nargin < 3
0034     raise_lackinput('slimginterp', 3);
0035 end
0036 
0037 if ~isnumeric(A)
0038     error('sltoolbox:invalidarg', ...
0039         'The image array should be an numeric array');
0040 end
0041 dA = ndims(A);
0042 sA = size(A);
0043 h = sA(1); w = sA(2);
0044 
0045 s = size(I);
0046 if ~isequal(size(J), s)
0047     error('sltoolbox:invalidarg', ...
0048         'The sizes of I and J are inconsistent');
0049 end
0050 
0051 if dA == 2
0052     nc = 1;
0053 else
0054     nc = prod(sA(3:end));
0055 end
0056 
0057 if nargin < 4 || isempty(interpker)
0058     interpker = 'linear';
0059 end
0060 [interpfunc, rad] = slgetinterpkernel(interpker);
0061 
0062 %% Main skeleton
0063 
0064 % do interpolation
0065 
0066 if ischar(interpker) && strcmpi(interpker, 'nearest')
0067     V = interp_nn(A, h, w, nc, I, J, s);
0068 else
0069     V = interp_kernel(A, h, w, nc, I, J, s, interpfunc, rad);
0070 end
0071 
0072 % reshape for multi-channel
0073 
0074 if dA >= 4
0075     vsiz = [s, sA(3:end)];
0076     V = reshape(V, vsiz);
0077 end
0078 
0079 %% Core functions
0080 
0081 function V = interp_nn(A, h, w, nc, I, J, s)
0082 
0083 Ir = round(I);
0084 Jr = round(J);
0085 Ir = confine_value(Ir, 1, h);
0086 Jr = confine_value(Jr, 1, w);
0087 inds = ij2ind(h, Ir, Jr);
0088 clear Ir Jr;
0089 
0090 if nc == 1
0091     V = A(inds);
0092 else
0093     inds = inds(:);
0094     A = reshape(A, h*w, nc);
0095     V = A(inds, :);
0096     V = reshape(V, [s, nc]);
0097 end
0098 
0099 
0100 function V = interp_kernel(A, h, w, nc, I, J, s, interpfunc, rad)
0101 
0102 n = numel(I);
0103 If = reshape(I, [1, n]);
0104 Jf = reshape(J, [1, n]);
0105 
0106 % generate indices of used points
0107 
0108 dxs = get_offsets(rad)';
0109 nnb = 2 * rad;
0110 Iu = floor(If);
0111 Ju = floor(Jf);
0112 Iu = Iu(ones(nnb, 1), :);
0113 Ju = Ju(ones(nnb, 1), :);
0114 Iu = sladdvec(Iu, dxs, 1);
0115 Ju = sladdvec(Ju, dxs, 1);
0116 
0117 % compute displacements and weights
0118 
0119 Di = sladdvec(Iu, -If, 2);
0120 Dj = sladdvec(Ju, -Jf, 2);
0121 clear If Jf;
0122 Wi = interpfunc(Di);
0123 clear Di;
0124 Wj = interpfunc(Dj);
0125 clear Dj;
0126 
0127 % confine used indices
0128 
0129 Iu = confine_value(Iu, 1, h);
0130 Ju = confine_value(Ju, 1, w);
0131 
0132 % from 1D to 2D
0133 inds_i = expand_inds(1, nnb);
0134 inds_j = expand_inds(2, nnb);
0135 Wi = Wi(inds_i, :);
0136 Wj = Wj(inds_j, :);
0137 Iu = Iu(inds_i, :);
0138 Ju = Ju(inds_j, :);
0139 
0140 W = Wi .* Wj;
0141 clear Wi Wj;
0142 Inds = ij2ind(h, Iu, Ju);
0143 clear Iu Ju;
0144 
0145 % interpolation by weighted sum
0146 
0147 if nc == 1
0148     M = A(Inds);
0149     clear Inds;
0150     V = sum(M .* W, 1);
0151     V = reshape(V, s);
0152 else
0153     
0154 % Batch implementation: the memory consumption is too large
0155 %     Inds = Inds(:);
0156 %     A = reshape(A, h*w, nc);
0157 %     M = A(Inds, :);
0158 %     clear Inds;
0159 %     M = reshape(M, [nnb * nnb, n * nc]);
0160 %     W = repmat(W, [1, nc]);
0161 %     V = sum(M .* W, 1);
0162 %     V = reshape(V, [s, nc]);
0163 
0164 % Sequential implementation
0165     V = zeros(1, prod(s), nc);
0166     for i = 1 : nc
0167         curA = A(:,:,i);
0168         M = curA(Inds);
0169         curV = sum(M .* W, 1);
0170         V(:,:,i) = curV;
0171     end
0172     V = reshape(V, [s, nc]);
0173 
0174 end
0175 
0176 
0177 
0178 %% Auxiliary function
0179 
0180 function x = confine_value(x, lb, ub)
0181 
0182 x(x < lb) = lb;
0183 x(x > ub) = ub;
0184 
0185 function inds = ij2ind(h, I, J)
0186 
0187 inds = I + h * (J - 1);
0188 
0189 function dxs = get_offsets(r)
0190 
0191 if r == floor(r)
0192     dxs = -(r-1) : r;
0193 else
0194     error('sltoolbox:rterror', 'The effective radius should be integer');
0195 end
0196 
0197 function inds = expand_inds(d, n)
0198 
0199 if d == 1
0200     inds = (1:n)';
0201     inds = inds(:, ones(1,n));
0202     inds = inds(:);
0203 elseif d == 2
0204     inds = 1:n;
0205     inds = inds(ones(n,1), :);
0206     inds = inds(:);
0207 end
0208 
0209 
0210 
0211 
0212 
0213 
0214 
0215 
0216 
0217

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com