]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/col2im.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / col2im.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
20 ##
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:
24 ##
25 ## @table @code
26 ## @item distinct
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.
32 ## @item sliding
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).
37 ## @end table
38 ##
39 ## @code{A=col2im(B,[m,n],[mm,nn])} takes @code{distinct} as a default
40 ## value for @var{block_type}.
41 ##
42 ## @seealso{im2col}
43 ## @end deftypefn
44
45 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
46
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])");
50   endif
51   
52   if(nargin!=4)
53     block_type='sliding';
54   endif
55
56   ## common checks
57   if(!ismatrix(B))
58     error("col2im: B should be a matrix (or vector).");
59   endif
60   if(!isvector(sblock) || length(sblock)!=2)
61     error("col2im: expected [m,n] as second parameter.");
62   endif
63   if(!isvector(sb) || length(sb)!=2)
64     error("col2im: expected [mm,nn] as third parameter.");
65   endif
66
67   m=sblock(1);
68   n=sblock(2);
69   mm=sb(1);
70   nn=sb(2);
71
72   switch(block_type)
73     case('distinct')
74       if(rows(B)!=m*n)
75         error("col2im: B height must be m*n for 'distinct' block_type.");
76       endif
77       if(rem(mm,m)!=0)
78         error("col2im: mm should be multiple of m");
79       endif
80       if(rem(nn,n)!=0)
81         error("col2im: nn should be multiple of n");
82       endif
83       mt=mm/m;
84       nt=nn/n;
85       if(columns(B)<mt*nt)
86         error("col2im: B's width is too small (should be mm*nn/(m*m)).");
87       endif
88       c=1;
89       for i=1:mt
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);
93         c+=1;
94         for j=2:nt
95           r=horzcat(r, reshape(B(:,c),m,n));
96           c+=1;
97         endfor
98         if(i==1) ## this workarrounds a bug in ver<=2.1.57 cat implementation
99           A=r;
100         else
101           A=vertcat(A,r);
102         endif
103       endfor
104         
105     case('sliding')
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).");
108       endif
109       A=reshape(B, mm-m+1, nn-n+1);
110       
111     otherwise
112       error("col2im: invalid block_type.");
113   endswitch
114
115 endfunction
116
117 %!demo
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
122
123
124 %!shared B, Ad
125 %! v=[1:10]';
126 %! r=reshape(v,2,5);
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];
129
130 %!# bad m
131 %!error(col2im(B,[3,5],[6,10],'distinct'));
132
133 %!# bad n
134 %!error(col2im(B,[2,3],[6,10],'distinct'));
135
136 %!# bad mm
137 %!error(col2im(B,[2,5],[7,10],'distinct'));
138
139 %!# bad nn
140 %!error(col2im(B,[2,5],[6,11],'distinct'));
141
142 %!# bad block_type
143 %!error(col2im(B,[2,5],[6,10],'wrong_block_type'));
144
145 %!# this should be ok
146 %!assert(col2im(B,[2,5],[6,10],'distinct'), Ad);
147
148 %!# now sliding
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)));
151
152
153 %!# disctint on uint8
154 %!assert(col2im(uint8(B),[2,5],[6,10],'distinct'), uint8(Ad));
155
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"));
158
159
160 %
161 % $Log$
162 % Revision 1.4  2007/03/23 16:14:36  adb014
163 % Update the FSF address
164 %
165 % Revision 1.3  2007/01/04 23:46:17  hauberg
166 % Minor changes in help text
167 %
168 % Revision 1.2  2007/01/04 23:37:54  hauberg
169 % Minor changes in help text
170 %
171 % Revision 1.1  2006/08/20 12:59:32  hauberg
172 % Changed the structure to match the package system
173 %
174 % Revision 1.2  2004/09/03 17:57:42  jmones
175 % Added support for int* and uint* types
176 %
177 % Revision 1.1  2004/08/18 14:39:07  jmones
178 % im2col and col2im added
179 %
180 %