annotate examples/private/reggrid.m @ 5:f44689e95ea4

(none)
author idamnjanovic
date Mon, 22 Mar 2010 10:43:01 +0000
parents 7750624e0c73
children
rev   line source
idamnjanovic@1 1 function [varargout] = reggrid(sz,num,mode)
idamnjanovic@1 2 %REGGRID Regular sampling grid.
idamnjanovic@1 3 % [I1,I2,...,Ip] = REGGRID([N1 N2 ... Np], NUM) returns the indices
idamnjanovic@1 4 % of a regular uniform sampling grid over a p-dimensional matrix with
idamnjanovic@1 5 % dimensions N1xN2x...xNp. NUM is the minimal number of required samples,
idamnjanovic@1 6 % and it is ensured that the actual number of samples, given by
idamnjanovic@1 7 % length(I1)xlength(I2)x...xlength(Ip), is at least as large as NUM.
idamnjanovic@1 8 %
idamnjanovic@1 9 % [I1,I2,...,Ip] = REGGRID([N1 N2 ... Np], NUM,'MODE') specifies the
idamnjanovic@1 10 % method for distributing the samples along each dimension. Valid modes
idamnjanovic@1 11 % include 'eqdist' (the default mode) and 'eqnum'. 'eqdist' indicates an
idamnjanovic@1 12 % equal distance between the samples in each dimension, while 'eqnum'
idamnjanovic@1 13 % indicates an equal number of samples in each dimension.
idamnjanovic@1 14 %
idamnjanovic@1 15 % Notes about MODE:
idamnjanovic@1 16 %
idamnjanovic@1 17 % 1. The 'eqnum' mode will generally fail when the p-th root of NUM
idamnjanovic@1 18 % (i.e. NUM^(1/p)) is larger than min([N1 N2 ... Np]). Thus 'eqdist' is
idamnjanovic@1 19 % the more useful choice for sampling an arbitrary number of samples
idamnjanovic@1 20 % from the matrix (up to the total number of matrix entries).
idamnjanovic@1 21 %
idamnjanovic@1 22 % 2. In both modes, the equality (of the distance between samples, or
idamnjanovic@1 23 % the number of samples in each dimension) is only approximate. This is
idamnjanovic@1 24 % because REGGRID attempts to maintain the appropriate equality while at
idamnjanovic@1 25 % the same time find a sampling pattern where the total number of
idamnjanovic@1 26 % samples is as close as possible to NUM. In general, the larger {Ni}
idamnjanovic@1 27 % and NUM are, the tighter the equality.
idamnjanovic@1 28 %
idamnjanovic@1 29 % Example: Sample a set of blocks uniformly from a 2D image.
idamnjanovic@1 30 %
idamnjanovic@1 31 % n = 512; blocknum = 20000; blocksize = [8 8];
idamnjanovic@1 32 % im = rand(n,n);
idamnjanovic@1 33 % [i1,i2] = reggrid(size(im)-blocksize+1, blocknum);
idamnjanovic@1 34 % blocks = sampgrid(im, blocksize, i1, i2);
idamnjanovic@1 35 %
idamnjanovic@1 36 % See also SAMPGRID.
idamnjanovic@1 37
idamnjanovic@1 38 % Ron Rubinstein
idamnjanovic@1 39 % Computer Science Department
idamnjanovic@1 40 % Technion, Haifa 32000 Israel
idamnjanovic@1 41 % ronrubin@cs
idamnjanovic@1 42 %
idamnjanovic@1 43 % November 2007
idamnjanovic@1 44
idamnjanovic@1 45 dim = length(sz);
idamnjanovic@1 46
idamnjanovic@1 47 if (nargin<3)
idamnjanovic@1 48 mode = 'eqdist';
idamnjanovic@1 49 end
idamnjanovic@1 50
idamnjanovic@1 51 if (any(sz<1))
idamnjanovic@1 52 error(['Invalid matrix size : [' num2str(sz) ']']);
idamnjanovic@1 53 end
idamnjanovic@1 54
idamnjanovic@1 55 if (num > prod(sz))
idamnjanovic@1 56 warning(['Invalid number of samples, returning maximum number of samples.']);
idamnjanovic@1 57 elseif (num <= 0)
idamnjanovic@1 58 if (num < 0)
idamnjanovic@1 59 warning('Invalid number of samples, assuming 0 samples.');
idamnjanovic@1 60 end
idamnjanovic@1 61 for i = 1:length(sz)
idamnjanovic@1 62 varargout{i} = [];
idamnjanovic@1 63 end
idamnjanovic@1 64 return;
idamnjanovic@1 65 end
idamnjanovic@1 66
idamnjanovic@1 67
idamnjanovic@1 68 if (strcmp(mode,'eqdist'))
idamnjanovic@1 69
idamnjanovic@1 70 % approximate distance between samples: total volume divided by number of
idamnjanovic@1 71 % samples gives the average volume per sample. then, taking the p-th root
idamnjanovic@1 72 % gives the average distance between samples
idamnjanovic@1 73 d = (prod(sz)/num)^(1/dim);
idamnjanovic@1 74
idamnjanovic@1 75 % compute the initial guess for number of samples in each dimension.
idamnjanovic@1 76 % then, while total number of samples is too large, decrese the number of
idamnjanovic@1 77 % samples by one in the dimension where the samples are the most crowded.
idamnjanovic@1 78 % finally, do the opposite process until just passing num, so the final
idamnjanovic@1 79 % number of samples is the closest to num from above.
idamnjanovic@1 80
idamnjanovic@1 81 n = min(max(round(sz/d),1),sz); % set n so that it saturates at 1 and sz
idamnjanovic@1 82
idamnjanovic@1 83 active_dims = find(n>1); % dimensions where the sample num can be reduced
idamnjanovic@1 84 while(prod(n)>num && ~isempty(active_dims))
idamnjanovic@1 85 [y,id] = min((sz(active_dims)-1)./n(active_dims));
idamnjanovic@1 86 n(active_dims(id)) = n(active_dims(id))-1;
idamnjanovic@1 87 if (n(active_dims(id)) < 2)
idamnjanovic@1 88 active_dims = find(n>1);
idamnjanovic@1 89 end
idamnjanovic@1 90 end
idamnjanovic@1 91
idamnjanovic@1 92 active_dims = find(n<sz); % dimensions where the sample num can be increased
idamnjanovic@1 93 while(prod(n)<num && ~isempty(active_dims))
idamnjanovic@1 94 [y,id] = max((sz(active_dims)-1)./n(active_dims));
idamnjanovic@1 95 n(active_dims(id)) = n(active_dims(id))+1;
idamnjanovic@1 96 if (n(active_dims(id)) >= sz(active_dims(id)))
idamnjanovic@1 97 active_dims = find(n<sz);
idamnjanovic@1 98 end
idamnjanovic@1 99 end
idamnjanovic@1 100
idamnjanovic@1 101 for i = 1:dim
idamnjanovic@1 102 varargout{i} = round((1:n(i))/n(i)*sz(i));
idamnjanovic@1 103 varargout{i} = varargout{i} - floor((varargout{i}(1)-1)/2);
idamnjanovic@1 104 end
idamnjanovic@1 105
idamnjanovic@1 106 elseif (strcmp(mode,'eqnum'))
idamnjanovic@1 107
idamnjanovic@1 108 % same idea as above
idamnjanovic@1 109 n = min(max( ones(size(sz)) * round(num^(1/dim)) ,1),sz);
idamnjanovic@1 110
idamnjanovic@1 111 active_dims = find(n>1);
idamnjanovic@1 112 while(prod(n)>num && ~isempty(active_dims))
idamnjanovic@1 113 [y,id] = min((sz(active_dims)-1)./n(active_dims));
idamnjanovic@1 114 n(active_dims(id)) = n(active_dims(id))-1;
idamnjanovic@1 115 if (n(active_dims(id)) < 2)
idamnjanovic@1 116 active_dims = find(n>1);
idamnjanovic@1 117 end
idamnjanovic@1 118 end
idamnjanovic@1 119
idamnjanovic@1 120 active_dims = find(n<sz);
idamnjanovic@1 121 while(prod(n)<num && ~isempty(active_dims))
idamnjanovic@1 122 [y,id] = max((sz(active_dims)-1)./n(active_dims));
idamnjanovic@1 123 n(active_dims(id)) = n(active_dims(id))+1;
idamnjanovic@1 124 if (n(active_dims(id)) >= sz(active_dims(id)))
idamnjanovic@1 125 active_dims = find(n<sz);
idamnjanovic@1 126 end
idamnjanovic@1 127 end
idamnjanovic@1 128
idamnjanovic@1 129 for i = 1:dim
idamnjanovic@1 130 varargout{i} = round((1:n(i))/n(i)*sz(i));
idamnjanovic@1 131 varargout{i} = varargout{i} - floor((varargout{i}(1)-1)/2);
idamnjanovic@1 132 end
idamnjanovic@1 133 else
idamnjanovic@1 134 error('Invalid sampling mode');
idamnjanovic@1 135 end
idamnjanovic@1 136