1 ## Copyright (C) 2004 Josep Mones i Teixidor
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{A} = } col2im (@var{B}, [@var{m},@var{n}], [@var{mm},@var{nn}], @var{block_type})
18 ## @deftypefnx {Function File} {@var{A} = } col2im (@var{B}, [@var{m},@var{n}], [@var{mm},@var{nn}])
19 ## Rearranges matrix columns into blocks.
21 ## @code{A=col2im(B,[m,n],[mm,nn],block_type)} rearranges columns of
22 ## matrix @var{B} intro blocks in a way controlled by @var{block_type}
23 ## param, which can take the following values:
27 ## It uses @var{m}-by-@var{n} distinct blocks (which are not
28 ## overlapped), and are rearranged to form a @var{mm}-by-@var{nn} matrix
29 ## @var{A}. @var{B}'s height must be @var{m}*@var{n} and @code{col2im}
30 ## rearranges each column to a @var{m}-by-@var{n} block and uses them to
31 ## fill the whole matrix in left-to-right and then up-to-down order.
33 ## Is uses @var{m}-by-@var{n} sliding blocks. It rearranges row vector
34 ## @var{B} to a (@var{mm}-@var{m}+1)-by-(@var{nn}-@var{n}+1) matrix
35 ## @var{A}. @var{B} must be a
36 ## 1-by-(@var{mm}-@var{m}+1)*(@var{nn}-@var{n}+1).
39 ## @code{A=col2im(B,[m,n],[mm,nn])} takes @code{distinct} as a default
40 ## value for @var{block_type}.
45 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
47 function A = col2im(B, sblock, sb, block_type)
48 if(nargin<3 || nargin>4)
49 usage("A=col2im(B, [m,n], [mm,nn] [, block_type])");
58 error("col2im: B should be a matrix (or vector).");
60 if(!isvector(sblock) || length(sblock)!=2)
61 error("col2im: expected [m,n] as second parameter.");
63 if(!isvector(sb) || length(sb)!=2)
64 error("col2im: expected [mm,nn] as third parameter.");
75 error("col2im: B height must be m*n for 'distinct' block_type.");
78 error("col2im: mm should be multiple of m");
81 error("col2im: nn should be multiple of n");
86 error("col2im: B's width is too small (should be mm*nn/(m*m)).");
90 ## TODO: check if we can horzcat([],uint8([10;11])) in a
91 ## future Octave version > 2.1.58 in order to deuglify this!
92 r=reshape(B(:,c),m,n);
95 r=horzcat(r, reshape(B(:,c),m,n));
98 if(i==1) ## this workarrounds a bug in ver<=2.1.57 cat implementation
106 if(!all(size(B)==[1,(mm-m+1)*(nn-n+1)]))
107 error("col2im: wrong B size. Should be 1-by-(mm-m+1)*(nn-n+1).");
109 A=reshape(B, mm-m+1, nn-n+1);
112 error("col2im: invalid block_type.");
118 %! A=[1:10;11:20;21:30;31:40]
119 %! B=im2col(A,[2,5],'distinct')
120 %! C=col2im(B,[2,5],[4,10],'distinct')
121 %! # Divide A using distinct blocks and reverse operation
127 %! B=[v, v+10, v+20, v+30, v+40, v+50];
128 %! Ad=[r, r+10; r+20, r+30; r+40, r+50];
131 %!error(col2im(B,[3,5],[6,10],'distinct'));
134 %!error(col2im(B,[2,3],[6,10],'distinct'));
137 %!error(col2im(B,[2,5],[7,10],'distinct'));
140 %!error(col2im(B,[2,5],[6,11],'distinct'));
143 %!error(col2im(B,[2,5],[6,10],'wrong_block_type'));
145 %!# this should be ok
146 %!assert(col2im(B,[2,5],[6,10],'distinct'), Ad);
149 %!assert(col2im(ones(1,(10-2+1)*(7-3+1)),[2,3],[10,7]), ones((10-2+1),(7-3+1)));
150 %!assert(col2im(ones(1,(10-2+1)*(7-3+1)),[2,3],[10,7],'sliding'), ones((10-2+1),(7-3+1)));
153 %!# disctint on uint8
154 %!assert(col2im(uint8(B),[2,5],[6,10],'distinct'), uint8(Ad));
156 %!# now sliding on uint8
157 %!assert(col2im(ones(1,(10-2+1)*(7-3+1),"uint8"),[2,3],[10,7]), ones((10-2+1),(7-3+1),"uint8"));
162 % Revision 1.4 2007/03/23 16:14:36 adb014
163 % Update the FSF address
165 % Revision 1.3 2007/01/04 23:46:17 hauberg
166 % Minor changes in help text
168 % Revision 1.2 2007/01/04 23:37:54 hauberg
169 % Minor changes in help text
171 % Revision 1.1 2006/08/20 12:59:32 hauberg
172 % Changed the structure to match the package system
174 % Revision 1.2 2004/09/03 17:57:42 jmones
175 % Added support for int* and uint* types
177 % Revision 1.1 2004/08/18 14:39:07 jmones
178 % im2col and col2im added