changeset 805:2a34ada9e75b

impad: deprecate impad in favour of padarray. * impad.m: deprecate function and recommend to replace with padarray because padarray already does the same thing (and padarray is the one that exists in Matlab), works for matrices with any number of dimensions, and is now also working faster. * fftconv2.m, imrotate.m, imshear.m, imsmooth.m, imtranslate.m: replace impad with padarray (careful that impad's replicate pattern is padarray's circular pattern and xpad and ypad need to be swapped). * NEWS: add impad to list of deprecated functions.
author Carnë Draug <carandraug@octave.org>
date Fri, 11 Oct 2013 18:29:56 +0100
parents 9e53c082b5eb
children 275202657ccb
files NEWS inst/fftconv2.m inst/impad.m inst/imrotate.m inst/imshear.m inst/imsmooth.m inst/imtranslate.m
diffstat 7 files changed, 79 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS
+++ b/NEWS
@@ -53,6 +53,7 @@
  ** The following functions have been deprecated (see their help text
     for the recommended alternatives):
 
+      impad
       imrotate_Fourier
 
  ** rgb2ycbcr was completely rewritten to accept images of other classes, and
--- a/inst/fftconv2.m
+++ b/inst/fftconv2.m
@@ -74,8 +74,8 @@
   rb = rows(b);
   cb = columns(b);
 
-  A = fft2 (impad (a, [0 cb-1], [0 rb-1]));
-  B = fft2 (impad (b, [0 ca-1], [0 ra-1]));
+  A = fft2 (padarray (a, [rb-1 cb-1], "post"));
+  B = fft2 (padarray (b, [ra-1 ca-1], "post"));
 
   X = ifft2 (A.*B);
 
--- a/inst/impad.m
+++ b/inst/impad.m
@@ -17,6 +17,35 @@
 ## @deftypefn {Function File} {} impad(@var{A}, @var{xpad}, @var{ypad}, [@var{padding}, [@var{const}]])
 ## Pad (augment) a matrix for application of image processing algorithms.
 ##
+## This function has been deprecated in favor of @code{padarray} and will be
+## removed from future versions of image package.  Notes when making
+## the conversion:
+##
+## @itemize @bullet
+## @item
+## padarray will take the order of the padding arguments by order of dimension,
+## i.e., swap @var{xpad} and @var{ypad}.  Use
+## @code{padarray (@var{A}, [@var{ypad} @var{xpad}], @dots{})}
+##
+## @item
+## There is no @qcode{"ones"} as @var{padding} option.  Simply set the value
+## 1 as padding value.
+##
+## @item
+## If @var{xpad} and @var{ipad} are 2 element vectors with different values,
+## they will need to be replaced by two calls to padarray as follow:
+## @group
+## @example
+## B = padarray (A, [ypad(1) xpad(1)], "pre");
+## B = padarray (B, [ypad(2) xpad(2)], "post");
+## @end example
+## @end group
+##
+## @item
+## The @qcode{"reflect"} @var{padding} option of @code{padarray} is different
+## from @code{impad}.  Use @qcode{"circular"} instead.
+## @end itemize
+##
 ## Pads the input image @var{A} with @var{xpad}(1) elements from left, 
 ## @var{xpad}(2), elements from right, @var{ypad}(1) elements from above 
 ## and @var{ypad}(2) elements from below.
@@ -51,6 +80,19 @@
 ## A = 10*[1:5]' * ones(1,5) + ones(5,1)*[1:5]
 
 function retim = impad(im, xpad, ypad, padding = "zeros", const = 1)
+
+  persistent warned = false;
+  if (! warned)
+    warned = true;
+    ## We are deprecating impad because padarray already does the same thing
+    ## (we don't want multiple functions with the same purpose), padarray
+    ## already exists in Matlab so it must stay, padarray works for matrices
+    ## with any number of dimensions, and is now also working faster.
+    warning ("Octave:deprecated-function",
+             "`impad' has been deprecated in favor of `padarray (IM, [YPAD XPAD], \"PADDING\")'. This function will be removed from future versions of the `image' package");
+  endif
+
+
   ## Input checking
   if (!ismatrix(im) || ndims(im) < 2 || ndims(im) > 3)
     error("impad: first input argument must be an image");
--- a/inst/imrotate.m
+++ b/inst/imrotate.m
@@ -153,7 +153,7 @@
       endfor
     endif
     valid = NA;
-    
+
     ## we probably should do this in a safer way... but hardcoding the list of
     ## im2xxxx functions might not be a good idea since it then it requires to
     ## be added here if a new im2xxx function is implemented
@@ -214,7 +214,7 @@
                 [ydim xdim] = size(fs);
                 xpad = ceil( xmax - (xdim+1)/2 );
                 ypad = ceil( ymax - (ydim+1)/2 );
-                fs = impad(fs, [xpad,xpad], [ypad,ypad], "zeros");
+                fs = padarray (fs, [ypad xpad]);
             endif
             xcentre_new = (size(fs,2)+1) / 2;
             ycentre_new = (size(fs,1)+1) / 2;
@@ -248,7 +248,7 @@
             [ydim xdim] = size(f);
             xpad = ceil( xmax - xdim/2 );
             ypad = ceil( ymax - ydim/2 );
-            %f = impad(f, [xpad,xpad], [ypad,ypad], "zeros");
+            %f = padarray (f, [ypad xpad]);
             xcentre_new = (size(f,2)+1) / 2;
             ycentre_new = (size(f,1)+1) / 2;
         endif
--- a/inst/imshear.m
+++ b/inst/imshear.m
@@ -61,7 +61,7 @@
   [ydim_orig xdim_orig] = size(m);
   if ( strcmp(bbox, "wrap") == 0 )
     ypad = ceil( (xdim_orig+1)/2 * abs(alpha) );
-    m = impad(m, [0,0], [ypad,ypad]);
+    m = padarray (m, ypad);
   endif
 
   [ydim_new xdim_new] = size(m);
--- a/inst/imsmooth.m
+++ b/inst/imsmooth.m
@@ -77,7 +77,7 @@
 ## default, this area is 3 by 3, but this can be changed. If the third input
 ## argument is a scalar @var{N} the area will be @var{N} by @var{N}, and if it's
 ## a two-vector [@var{N}, @var{M}] the area will be @var{N} by @var{M}.
-## 
+##
 ## The image is extrapolated symmetrically before the filtering is performed.
 ##
 ## @strong{Gaussian bilateral filtering}
@@ -86,7 +86,7 @@
 ## Tomasi and Manduchi [2]. The filtering result is computed as
 ## @example
 ## @var{J}(x0, y0) = k * SUM SUM @var{I}(x,y) * w(x, y, x0, y0, @var{I}(x0,y0), @var{I}(x,y))
-##                  x   y        
+##                  x   y
 ## @end example
 ## where @code{k} a normalisation variable, and
 ## @example
@@ -102,7 +102,7 @@
 ## small edges are preserved better. By default @var{sigma_d} is 2, and @var{sigma_r}
 ## is @math{10/255} for floating points images (with integer images this is
 ## multiplied with the maximal possible value representable by the integer class).
-## 
+##
 ## The image is extrapolated symmetrically before the filtering is performed.
 ##
 ## @strong{Perona and Malik}
@@ -207,10 +207,10 @@
     error("imsmooth: second input must be a string");
   endif
   len = length(varargin);
-  
+
   ## Save information for later
   C = class(I);
-  
+
   ## Take action depending on 'name'
   switch (lower(name))
     ##############################
@@ -230,7 +230,7 @@
       h = ceil(3*s);
       f = exp( (-(-h:h).^2)./(2*s^2) ); f /= sum(f);
       ## Pad image
-      I = double(impad(I, h, h, "symmetric"));
+      I = double (padarray (I, [h h], "symmetric"));
       ## Perform the filtering
       for i = imchannels:-1:1
         J(:,:,i) = conv2(f, f, I(:,:,i), "valid");
@@ -255,12 +255,14 @@
       f2 = ones(1,s(1))/s(1);
       f1 = ones(1,s(2))/s(2);
       ## Pad image
-      I = impad(double(I), floor([s(2), s(2)-1]/2), floor([s(1), s(1)-1]/2), "symmetric");
+      I = padarray (I, floor ( [s(1) s(2)]   /2), "symmetric", "pre");
+      I = padarray (I, floor (([s(1) s(2)]-1)/2), "symmetric", "post");
+      I = double (I);
       ## Perform the filtering
       for i = imchannels:-1:1
-        J(:,:,i) = conv2(f1, f2, I(:,:,i), "valid");
+        J(:,:,i) = conv2 (f1, f2, I(:,:,i), "valid");
       endfor
-      
+
     ##############################
     ###   Circular averaging   ###
     ##############################
@@ -277,12 +279,12 @@
       ## Compute filter
       f = fspecial("disk", r);
       ## Pad image
-      I = impad(double(I), r, r, "symmetric");
+      i = double (padarray (I, [r r], "symmetric"));
       ## Perform the filtering
       for i = imchannels:-1:1
         J(:,:,i) = conv2(I(:,:,i), f, "valid");
       endfor
-    
+
     ############################
     ###   Median Filtering   ###
     ############################
@@ -304,7 +306,7 @@
       for i = imchannels:-1:1
         J(:,:,i) = medfilt2(I(:,:,i), s, "symmetric");
       endfor
-    
+
     ###############################
     ###   Bilateral Filtering   ###
     ###############################
@@ -331,10 +333,10 @@
       endif
       ## Pad image
       s = max([round(3*sigma_d),1]);
-      I = impad(I, s, s, "symmetric");
+      I = padarray (I, [s s], "symmetric");
       ## Perform the filtering
       J = __bilateral__(I, sigma_d, sigma_r);
-    
+
     ############################
     ###   Perona and Malik   ###
     ############################
@@ -384,7 +386,7 @@
       for i = imchannels:-1:1
         J(:,:,i) = pm(I(:,:,i), iter, lambda, method);
       endfor
-    
+
     #####################################
     ###   Custom Gaussian Smoothing   ###
     #####################################
@@ -411,15 +413,15 @@
           error ("imsmooth: %s input argument must be of class 'double'", arg_names {k});
         endif
       endfor
-      
+
       ## Perform the smoothing
       for i = imchannels:-1:1
         J(:,:,i) = __custom_gaussian_smoothing__ (I(:,:,i), varargin {:});
       endfor
-    
+
     ######################################
     ###   Mean Shift Based Smoothing   ###
-    ######################################  
+    ######################################
     # NOT YET IMPLEMENTED
     #case "mean shift"
     #  J = mean_shift(I, varargin{:});
@@ -430,7 +432,7 @@
     otherwise
       error("imsmooth: unsupported smoothing type '%s'", name);
   endswitch
-  
+
   ## Cast the result to the same class as the input
   J = cast(J, C);
 endfunction
@@ -440,10 +442,10 @@
   ## Initialisation
   [imrows, imcols] = size(I);
   J = I;
-  
+
   for i = 1:iter
     ## Pad image
-    padded = impad(J, 1, 1, "replicate");
+    padded = padarray (J, [1 1], "circular");
 
     ## Spatial derivatives
     dN = padded(1:imrows, 2:imcols+1) - J;
@@ -475,7 +477,7 @@
   m01 = conv2(I.*y, f, "same")./tmp;
   ms_x = round( m10./m00 ); # konverter ms_x og ms_y til linære indices og arbejd med dem!
   ms_y = round( m01./m00 );
-  
+
   ms = sub2ind(sz, ms_y, ms_x);
   %for i = 1:10
   i = 0;
@@ -493,7 +495,7 @@
     %endfor
   endwhile
   %endfor
-  
+
   ## Compute result
   J = I(ms);
 endfunction
--- a/inst/imtranslate.m
+++ b/inst/imtranslate.m
@@ -28,25 +28,13 @@
 function Y = imtranslate (X, a, b, bbox = "wrap")
 
   if ( strcmp(bbox, "crop")==1 )
-
-    xpad = [0,0];
-    if (a>0)
-      xpad = [0,ceil(a)];
-    elseif (a<0)
-      xpad = [-ceil(a),0];
-    endif
-
-    ypad = [0,0];
-    if (b>0)
-      ypad = [ceil(b),0];
-    elseif (b<0)
-      ypad = [0,-ceil(b)];
-    endif
-
-    X = impad(X, xpad, ypad, 'zeros');
+    pre = post = ceil ([a b]);
+    pre(pre   > 0) = 0;
+    post(post < 0) = 0;
+    X = padarray (X, abs (pre), "pre");
+    X = padarray (X, post, "post");
   endif
 
-
   [dimy, dimx] = size(X);
   x = fft2(X);
   px = exp(-2*pi*i*a*(0:dimx-1)/dimx);