[07d29c]: impad.m Maximize Restore History

Download this file

impad.m    143 lines (130 with data), 5.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
## Copyright (C) 2000 Teemu Ikonen
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
## -*- texinfo -*-
## @deftypefn {Function File} {} impad(@var{A}, @var{xpad}, @var{ypad}, [@var{padding}, [@var{const}]])
## Pad (augment) a matrix for application of image processing algorithms.
##
## 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.
## Values of padding elements are determined from the optional arguments
## @var{padding} and @var{const}. @var{padding} is one of
##
## @table @samp
## @item "zeros"
## pad with zeros (default)
##
## @item "ones"
## pad with ones
##
## @item "constant"
## pad with a value obtained from the optional fifth argument const
##
## @item "symmetric"
## pad with values obtained from @var{A} so that the padded image mirrors
## @var{A} starting from edges of @var{A}
##
## @item "reflect"
## same as symmetric, but the edge rows and columns are not used in the padding
##
## @item "replicate"
## pad with values obtained from A so that the padded image
## repeates itself in two dimensions
##
## @end table
## @end deftypefn
## Author: Teemu Ikonen <tpikonen@pcu.helsinki.fi>
## Created: 5.5.2000
## Keywords: padding image processing
## A nice test matrix for padding:
## A = 10*[1:5]' * ones(1,5) + ones(5,1)*[1:5]
function retval = impad(A, xpad, ypad, padding, const)
try empty_list_elements_ok_save = empty_list_elements_ok;
catch empty_list_elements_ok_save = 0;
end
try warn_empty_list_elements_save = warn_empty_list_elements;
catch warn_empty_list_elements_save = 0;
end
unwind_protect
if nargin < 4, padding = "zeros"; endif
if nargin < 5, const = 1; endif
if isscalar(xpad), xpad(2) = xpad(1); endif
if isscalar(ypad), ypad(2) = ypad(1); endif
origx = size(A,2);
origy = size(A,1);
retx = origx + xpad(1) + xpad(2);
rety = origy + ypad(1) + ypad(2);
empty_list_elements_ok = 1;
warn_empty_list_elements = 0;
if(strcmp(padding, "zeros"))
retval = zeros(rety,retx);
retval(ypad(1)+1 : ypad(1)+origy, xpad(1)+1 : xpad(1)+origx) = A;
elseif(strcmp(padding,"ones"))
retval = ones(rety,retx);
retval(ypad(1)+1 : ypad(1)+origy, xpad(1)+1 : xpad(1)+origx) = A;
elseif(strcmp(padding,"constant"))
retval = const.*ones(rety,retx);
retval(ypad(1)+1 : ypad(1)+origy, xpad(1)+1 : xpad(1)+origx) = A;
elseif(strcmp(padding,"replicate"))
y1 = origy-ypad(1)+1;
x1 = origx-xpad(1)+1;
if(y1 < 1 || x1 < 1 || ypad(2) > origy || xpad(2) > origx)
error("Too large padding for this padding type");
else
yrange1 = y1 : origy;
yrange2 = 1 : ypad(2);
xrange1 = x1 : origx;
xrange2 = 1 : xpad(2);
retval = [ A(yrange1, xrange1), A(yrange1, :), A(yrange1, xrange2);
A(:, xrange1), A, A(:, xrange2);
A(yrange2, xrange1), A(yrange2, :), A(yrange2, xrange2) ];
endif
elseif(strcmp(padding,"symmetric"))
y2 = origy-ypad(2)+1;
x2 = origx-xpad(2)+1;
if(ypad(1) > origy || xpad(1) > origx || y2 < 1 || x2 < 1)
error("Too large padding for this padding type");
else
yrange1 = 1 : ypad(1);
yrange2 = y2 : origy;
xrange1 = 1 : xpad(1);
xrange2 = x2 : origx;
retval = [ fliplr(flipud(A(yrange1, xrange1))), flipud(A(yrange1, :)), fliplr(flipud(A(yrange1, xrange2)));
fliplr(A(:, xrange1)), A, fliplr(A(:, xrange2));
fliplr(flipud(A(yrange2, xrange1))), flipud(A(yrange2, :)), fliplr(flipud(A(yrange2, xrange2))) ];
endif
elseif(strcmp(padding,"reflect"))
y2 = origy-ypad(2);
x2 = origx-xpad(2);
if(ypad(1)+1 > origy || xpad(1)+1 > origx || y2 < 1 || x2 < 1)
error("Too large padding for this padding type");
else
yrange1 = 2 : ypad(1)+1;
yrange2 = y2 : origy-1;
xrange1 = 2 : xpad(1)+1;
xrange2 = x2 : origx-1;
retval = [ fliplr(flipud(A(yrange1, xrange1))), flipud(A(yrange1, :)), fliplr(flipud(A(yrange1, xrange2)));
fliplr(A(:, xrange1)), A, fliplr(A(:, xrange2));
fliplr(flipud(A(yrange2, xrange1))), flipud(A(yrange2, :)), fliplr(flipud(A(yrange2, xrange2))) ];
endif
else
error("Unknown padding type");
endif
unwind_protect_cleanup
empty_list_elements_ok = empty_list_elements_ok_save;
warn_empty_list_elements = warn_empty_list_elements_save;
end_unwind_protect
endfunction