[07d29c]: blkproc.m Maximize Restore History

Download this file

blkproc.m    191 lines (167 with data), 6.7 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
## Copyright (C) 2004 Josep Mones i Teixidor
##
## 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} {@var{B} = } blkproc (@var{A}, [@var{m},@var{n}], @var{fun})
## @deftypefnx {Function File} {@var{B} = } blkproc (@var{A}, [@var{m},@var{n}], @var{fun}, ...)
## @deftypefnx {Function File} {@var{B} = } blkproc (@var{A}, [@var{m},@var{n}], [@var{mborder},@var{nborder}], @var{fun}, @var{...})
## @deftypefnx {Function File} {@var{B} = } blkproc (@var{A}, 'indexed', ...)
## Processes image in blocks using user-supplied function
##
## @code{B=blkproc(A,[m,n],fun)} divides image @var{A} in
## @var{m}-by-@var{n} blocks, and passes them to user-supplied function
## @var{fun}, which result is concatenated to build returning matrix
## @var{B}. If padding is needed to build @var{m}-by-@var{n}, it is added
## at the bottom and right borders of the image. 0 is used as a padding
## value.
##
## @code{B=blkproc(A,[m,n],fun,...)} behaves as described above but
## passes extra parameters to function @var{fun}.
##
## @code{B=blkproc(A,[m,n],[mborder,nborder],fun,...)} behaves as
## described but uses blocks which overlap with neighbour blocks.
## Overlapping dimensions are @var{mborder} vertically and @var{nborder}
## horizontally. This doesn't change the number of blocks in an image
## (which depends only on size(@var{A}) and [@var{m},@var{n}]). Adding a
## border requires extra padding on all edges of the image. 0 is used as
## a padding value.
##
## @code{B=blkproc(A,'indexed',...)} assumes that @var{A} is an indexed
## image, so it pads the image using proper value: 0 for uint8 and
## uint16 images and 1 for double images. Keep in mind that if 'indexed'
## is not specified padding is always done using 0.
##
## @end deftypefn
## @seealso{colfilt,inline,bestblk}
## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
function B = blkproc(A, varargin)
if(nargin<3)
error("blkproc: invalid number of parameters.");
endif
## check 'indexed' presence
indexed=false;
p=1;
if(ischar(varargin{1}) && strcmp(varargin{1}, "indexed"))
indexed=true;
p+=1;
if(isa(A,"uint8") || isa(A,"uint16"))
padval=0;
else
padval=1;
endif
else
padval=0;
endif
## check [m,n]
if(!isvector(varargin{p}))
error("blkproc: expected [m,n] but param is not a vector.");
endif
if(length(varargin{p})!=2)
error("blkproc: expected [m,n] but param has wrong length.");
endif
sblk=varargin{p}(:);
p+=1;
## check [mborder,nborder]
if(nargin<p)
error("blkproc: required parameters haven't been supplied.");
endif
if(isvector(varargin{p}))
if(length(varargin{p})!=2)
error("blkproc: expected [mborder,nborder] but param has wrong length.");
endif
sborder=varargin{p}(:);
p+=1;
else
sborder=[0;0];
endif
## check fun
## TODO: add proper checks for this one
if(nargin<p)
error("blkproc: required parameters haven't been supplied.");
endif
fun=varargin{p};
if(!isa(fun,"function handle") &&
!isa(fun,"inline function") &&
!ischar(fun))
error("blkproc: invalid fun parameter.");
endif
## remaining params are params to fun
## extra params are p+1:nargin-1
## First of all we calc needed padding which will be applied on bottom
## and right borders
## The "-" makes the function output needed elements to fill another
## block directly
sp=mod(-size(A)',sblk);
if(any(sp))
A=padarray(A,sp,padval,'post');
endif
## we store A size without border padding to iterate later
soa=size(A);
## If we have borders then we need more padding
if(any(sborder))
A=padarray(A,sborder,padval);
endif
## calculate end of block
eblk=sblk+sborder*2-1;
## now we can process by blocks
## we try to preserve fun return type by concatenating everything
for i=1:sblk(1):soa(1)
## This assures r has the same class as returned by fun
r=feval(fun,A(i:i+eblk(1),1:1+eblk(2)),varargin{p+1:nargin-1});
for j=1+sblk(2):sblk(2):soa(2)
r=horzcat(r,feval(fun,A(i:i+eblk(1),j:j+eblk(2)),varargin{p+1:nargin-1}));
endfor
if(i==1) ## this assures B has the same class as A
B=r;
else
B=vertcat(B,r);
endif
endfor
endfunction
%!demo
%! blkproc(eye(6),[2,2],inline("any(x(:))","x"))
%! # Returns a 3-by-3 diagonal
%!assert(blkproc(eye(6),[2,2],"sum"),blkproc(eye(6),[2,2],@sum));
%!assert(blkproc(eye(6),[2,2],"sum"),blkproc(eye(6),[2,2],inline("sum(x)","x")));
%!assert(blkproc(eye(6),[1,2],@sum),kron(eye(3),[1;1]));
%!assert(blkproc(eye(6),[2,2],inline("any(x(:))","x")),eye(3)!=0);
%!assert(blkproc(eye(6),[1,2],[1,1],inline("sum(x(:))","x")),[2,1,0;3,2,0;2,3,1;1,3,2;0,2,3;0,1,2]);
%!assert(blkproc(eye(6),'indexed',[1,2],[1,1],inline("sum(x(:))","x")),[8,5,6;6,2,3;5,3,4;4,3,5;3,2,6;6,5,8]);
%!assert(blkproc(eye(6),[2,3],[4,3],inline("sum(x(:))","x")),ones(3,2)*6);
% Some int* and uint* tests
%!assert(blkproc(eye(6),[2,2],inline("int8(sum(x(:)))","x")),eye(3,"int8")*2);
% Padding is 0 even for indexed
%!assert(blkproc(uint8(eye(6)),[1,2],[1,1],inline("sum(x(:))","x")),[2,1,0;3,2,0;2,3,1;1,3,2;0,2,3;0,1,2]);
%!assert(blkproc(uint8(eye(6)),'indexed',[1,2],[1,1],inline("sum(x(:))","x")),[2,1,0;3,2,0;2,3,1;1,3,2;0,2,3;0,1,2]);
%!assert(blkproc(uint16(eye(6)),[1,2],[1,1],inline("sum(x(:))","x")),[2,1,0;3,2,0;2,3,1;1,3,2;0,2,3;0,1,2]);
%!assert(blkproc(uint16(eye(6)),'indexed',[1,2],[1,1],inline("sum(x(:))","x")),[2,1,0;3,2,0;2,3,1;1,3,2;0,2,3;0,1,2]);
%
% $Log$
% Revision 1.5 2005/09/08 02:00:17 pkienzle
% [for Bill Denney] isstr -> ischar
%
% Revision 1.4 2004/11/15 16:04:20 pkienzle
% Fix tests for functions which return boolean matrices
%
% Revision 1.3 2004/09/03 17:49:37 jmones
% Improved uint8 and uint16 padding expections
%
% Revision 1.2 2004/09/03 13:40:13 jmones
% Check result has same class as function result, and improved fun param checking
%
% Revision 1.1 2004/08/15 19:27:46 jmones
% blkproc: block process an image using user-supplied function
%
%