annotate util/ksvd utils/reggrid.m @ 198:83af80baf959 luisf_dev

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