]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/qtgetblk.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / qtgetblk.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{vals}] = } qtgetblk (@var{I},@var{S},@var{dim})
18 ## @deftypefnx {Function File} {[@var{vals},@var{idx}] = } qtgetblk (@var{I},@var{S},@var{dim})
19 ## @deftypefnx {Function File} {[@var{vals},@var{r},@var{c}] = } qtgetblk (@var{I},@var{S},@var{dim})
20 ## Obtain block values from a quadtree decomposition.
21 ##
22 ## [vals]=qtgetblk(I,S,dim) returns a dim-by-dim-by-k array in
23 ## @var{vals} which contains the dim-by-dim blocks in the quadtree
24 ## decomposition (@var{S}, which is returned by qtdecomp) of @var{I}. If
25 ## there are no blocks, an empty matrix is returned.
26 ##
27 ## [vals,idx]=qtgetblk(I,S,dim) returns @var{vals} as described above.
28 ## In addition, it returns @var{idx}, a vector which contains the linear
29 ## indices of the upper left corner of each block returned (the same
30 ## result as find(full(S)==dim)).
31 ##
32 ## [vals,r,c]=qtgetblk(I,S,dim) returns @var{vals} as described, and two
33 ## vectors, @var{r} and @var{c}, which contain the row and column
34 ## coordinates of the blocks returned.
35 ##
36 ## @seealso{qtdecomp, qtsetblk}
37 ## @end deftypefn
38
39 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
40
41 function [varargout] = qtgetblk(I, S, dim)
42   if (nargin!=3)
43     usage("[vals,r,c]=qtgetblk(I,S,dim), [vals,idx]=qtgetblk(I,S,dim)");
44   endif
45   if (nargout>3)
46     usage("[vals,r,c]=qtgetblk(I,S,dim), [vals,idx]=qtgetblk(I,S,dim)");
47   endif
48
49   ## get blocks
50   [i,j,v]=find(S);
51
52   ## filter the ones which match dim
53   idx=find(v==dim);
54   
55   if(length(idx)==0)
56     for i=1:nargout
57       varargout{i}=[];
58     endfor
59   else
60     r=i(idx);
61     c=j(idx);
62     
63     ## copy to a dim-by-dim-by-k array
64     vals=zeros(dim,dim,length(idx));
65     for i=1:length(idx)
66       vals(:,:,i)=I(r(i):r(i)+dim-1,c(i):c(i)+dim-1);
67     endfor
68     
69     varargout{1}=vals;
70   
71     if(nargout==3)
72       varargout{2}=r;
73       varargout{3}=c;
74     elseif(nargout==2)
75       varargout{2}=(c-1)*rows(I)+r;
76     endif
77   endif
78 endfunction
79
80
81 %!demo
82 %! [vals,r,c]=qtgetblk(eye(4),qtdecomp(eye(4)),2)
83 %! % Returns 2 blocks, at [1,3] and [3,1] (2*2 zeros blocks)
84
85 %!shared A,S
86 %! A=[ 1, 4, 2, 5,54,55,61,62;
87 %!     3, 6, 3, 1,58,53,67,65;
88 %!     3, 6, 3, 1,58,53,67,65;
89 %!     3, 6, 3, 1,58,53,67,65;
90 %!    23,42,42,42,99,99,99,99;    
91 %!    27,42,42,42,99,99,99,99;    
92 %!    23,22,26,25,99,99,99,99;    
93 %!    22,22,24,22,99,99,99,99];
94 %! S=qtdecomp(A,10);
95
96 %!test
97 %! [va]=qtgetblk(A,S,8);
98 %! [vb,r,c]=qtgetblk(A,S,8);
99 %! [vc,i]=qtgetblk(A,S,8);
100 %! assert(va, vb);
101 %! assert(va, vc);
102 %! assert(i,[]);
103 %! assert(r,[]);
104 %! assert(c,[]);
105 %! R=[];
106 %! assert(va,R);
107
108
109 %!test
110 %! [va]=qtgetblk(A,S,4);
111 %! [vb,r,c]=qtgetblk(A,S,4);
112 %! [vc,i]=qtgetblk(A,S,4);
113 %! assert(va, vb);
114 %! assert(va, vc);
115 %! assert(i, find(full(S)==4));
116 %! assert(r,[1;5]);
117 %! assert(c,[1;5]);
118 %! R=zeros(4,4,2);
119 %! R(:,:,1)=A(1:4,1:4);
120 %! R(:,:,2)=A(5:8,5:8);
121 %! assert(va,R);
122
123 %!test
124 %! [va]=qtgetblk(A,S,2);
125 %! [vb,r,c]=qtgetblk(A,S,2);
126 %! [vc,i]=qtgetblk(A,S,2);
127 %! assert(va, vb);
128 %! assert(va, vc);
129 %! assert(i, find(full(S)==2));
130 %! assert(r,[7;5;7;1;3;1;3]);
131 %! assert(c,[1;3;3;5;5;7;7]);
132 %! R=zeros(2,2,7);
133 %! R(:,:,1)=A(7:8,1:2);
134 %! R(:,:,2)=A(5:6,3:4);
135 %! R(:,:,3)=A(7:8,3:4);
136 %! R(:,:,4)=A(1:2,5:6);
137 %! R(:,:,5)=A(3:4,5:6);
138 %! R(:,:,6)=A(1:2,7:8);
139 %! R(:,:,7)=A(3:4,7:8);
140 %! assert(va,R);
141
142 %
143 % $Log$
144 % Revision 1.4  2007/03/23 16:14:37  adb014
145 % Update the FSF address
146 %
147 % Revision 1.3  2007/01/04 23:50:47  hauberg
148 % Put seealso before end deftypefn
149 %
150 % Revision 1.2  2007/01/04 23:41:47  hauberg
151 % Minor changes in help text
152 %
153 % Revision 1.1  2006/08/20 12:59:35  hauberg
154 % Changed the structure to match the package system
155 %
156 % Revision 1.3  2006/01/02 20:53:42  pkienzle
157 % Reduce number of shared variables in tests
158 %
159 % Revision 1.2  2004/08/11 19:52:41  jmones
160 % qtsetblk added
161 %
162 %