]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/bestblk.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / bestblk.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{siz} = } bestblk ([@var{m} @var{n}], @var{k})
18 ## @deftypefnx {Function File} {[@var{mb} @var{nb}] = } bestblk ([@var{m} @var{n}], @var{k})
19 ## Calculates the best size of block for block processing.
20 ##
21 ## @code{siz=bestblk([m,n],k)} calculates the optimal block size for block
22 ## processing for a @var{m}-by-@var{n} image. @var{k} is the maximum
23 ## side dimension of the block. Its default value is 100. @var{siz} is a
24 ## row vector which contains row and column dimensions for the block.
25 ##
26 ## @code{[mb,nb]=bestblk([m,n],k)} behaves as described above but
27 ## returns block dimensions to @var{mb} and @var{nb}.
28 ##
29 ## @strong{Algorithm:}
30 ##
31 ## For each dimension (@var{m} and @var{n}), it follows this algorithm:
32 ##
33 ## 1.- If dimension is less or equal than @var{k}, it returns the
34 ## dimension value.
35 ##
36 ## 2.- If not then returns the value between
37 ## @code{round(min(dimension/10,k/2))} which minimizes padding.
38 ##
39 ##
40 ## @seealso{blkproc}
41 ## @end deftypefn
42
43
44 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
45
46 function [varargout] = bestblk(ims,k)
47   if(nargin<1 || nargin>2)
48     usage("siz=bestblk([m,n],k), [mb,nb]=bestblk([m,n],k)");
49   endif
50   if(nargout>2)
51     usage("siz=bestblk([m,n],k), [mb,nb]=bestblk([m,n],k)");
52   endif
53   if(nargin<2)
54     k=100;
55   endif
56   if(!isvector(ims))
57     error("bestblk: first parameter is not a vector.");
58   endif
59   ims=ims(:);
60   if(length(ims)!=2)
61     error("bestblk: length of first parameter is not 2.");
62   endif
63
64   mb=mi=ims(1);
65   p=mi;
66   if(mi>k)
67     for i=round(min(mi/10,k/2)):k
68       pt=rem(mi,i);
69       if(pt<p)
70         p=pt;
71         mb=i;
72       endif
73     endfor
74   endif
75
76   nb=ni=ims(2);
77   p=ni;
78   if(ni>k)
79     for i=round(min(ni/10,k/2)):k
80       pt=rem(ni,i);
81       if(pt<p)
82         p=pt;
83         nb=i;
84       endif
85     endfor
86   endif
87
88   if(nargout<=1)
89     varargout{1}=[mb;nb];
90   else
91     varargout{1}=mb;
92     varargout{2}=nb;
93   endif
94
95 endfunction
96
97 %!demo
98 %! siz = bestblk ([200; 10], 50);
99 %! disp (siz)
100
101 %!assert(bestblk([300;100],150),[30;100]);
102 %!assert(bestblk([256,128],17),[16;16]);
103
104 % $Log$
105 % Revision 1.3  2007/03/23 16:14:36  adb014
106 % Update the FSF address
107 %
108 % Revision 1.2  2007/01/04 23:44:22  hauberg
109 % Minor changes in help text
110 %
111 % Revision 1.1  2006/08/20 12:59:31  hauberg
112 % Changed the structure to match the package system
113 %
114 % Revision 1.2  2005/07/03 01:10:19  pkienzle
115 % Try to correct for missing newline at the end of the file
116 %
117 % Revision 1.1  2004/08/15 19:01:05  jmones
118 % bestblk added: Calculates best block size for block processing