annotate Problems/private/reggrid.m @ 40:6416fc12f2b8

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