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

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:35:24 +0000
parents 41a5a3c26961
children
rev   line source
idamnjanovic@10 1 function y = sampgrid(x,blocksize,varargin)
idamnjanovic@10 2 %SAMPGRID Sample a multi-dimensional matrix on a regular grid.
idamnjanovic@10 3 % Y = SAMPGRID(X,BLOCKSIZE,I1,I2,...,Ip) extracts block samples of size
idamnjanovic@10 4 % BLOCKSIZE from the p-dimensional matrix X, arranging the samples as the
idamnjanovic@10 5 % column vectors of the matrix Y. The locations of the (1,1,..,1)-th
idamnjanovic@10 6 % elements of each block are given in the index vectors I1,I2,..Ip. The
idamnjanovic@10 7 % total number of samples taken is length(I1)xlength(I2)x...xlength(Ip).
idamnjanovic@10 8 % BLOCKSIZE should either be a p-element vector of the form [N1,N2,...Np],
idamnjanovic@10 9 % or a scalar N which is shorthand for the square block size [N N ... N].
idamnjanovic@10 10 %
idamnjanovic@10 11 % Example: Sample a set of blocks uniformly from a 2D image.
idamnjanovic@10 12 %
idamnjanovic@10 13 % n = 512; blocknum = 20000; blocksize = [8 8];
idamnjanovic@10 14 % im = rand(n,n);
idamnjanovic@10 15 % [i1,i2] = reggrid(size(im)-blocksize+1, blocknum);
idamnjanovic@10 16 % blocks = sampgrid(im, blocksize, i1, i2);
idamnjanovic@10 17 %
idamnjanovic@10 18 % See also REGGRID.
idamnjanovic@10 19
idamnjanovic@10 20 % Ron Rubinstein
idamnjanovic@10 21 % Computer Science Department
idamnjanovic@10 22 % Technion, Haifa 32000 Israel
idamnjanovic@10 23 % ronrubin@cs
idamnjanovic@10 24 %
idamnjanovic@10 25 % November 2007
idamnjanovic@10 26
idamnjanovic@10 27
idamnjanovic@10 28 p = ndims(x);
idamnjanovic@16 29 if (p==2 && any(size(x)==1) && length(blocksize)==1)
idamnjanovic@16 30 p = 1;
idamnjanovic@16 31 end
idamnjanovic@10 32
idamnjanovic@10 33 if (numel(blocksize)==1)
idamnjanovic@10 34 blocksize = ones(1,p)*blocksize;
idamnjanovic@10 35 end
idamnjanovic@10 36
idamnjanovic@10 37 n = zeros(1,p);
idamnjanovic@10 38 for i = 1:p
idamnjanovic@10 39 n(i) = length(varargin{i});
idamnjanovic@10 40 end
idamnjanovic@10 41
idamnjanovic@10 42 nsamps = prod(n);
idamnjanovic@10 43
idamnjanovic@10 44 % create y of the same class as x
idamnjanovic@10 45 y = zeros(prod(blocksize),nsamps,class(x));
idamnjanovic@10 46
idamnjanovic@10 47 % ids() contains the index of the current block in I1..Ip
idamnjanovic@10 48 ids = ones(p,1);
idamnjanovic@10 49
idamnjanovic@10 50 % block_ids contains the indices of the current block in X
idamnjanovic@10 51 block_ids = cell(p,1);
idamnjanovic@10 52 for j = 1:p
idamnjanovic@10 53 block_ids{j} = varargin{j}(1) : varargin{j}(1)+blocksize(j)-1;
idamnjanovic@10 54 end
idamnjanovic@10 55
idamnjanovic@10 56 for k = 1:nsamps
idamnjanovic@10 57 block = x(block_ids{:});
idamnjanovic@10 58 y(:,k) = block(:);
idamnjanovic@10 59
idamnjanovic@10 60 % increment ids() and block_ids{}
idamnjanovic@10 61 if (k<nsamps)
idamnjanovic@10 62 j = 1;
idamnjanovic@10 63 while (ids(j) == n(j))
idamnjanovic@10 64 ids(j) = 1;
idamnjanovic@10 65 block_ids{j} = varargin{j}(1) : varargin{j}(1)+blocksize(j)-1;
idamnjanovic@10 66 j = j+1;
idamnjanovic@10 67 end
idamnjanovic@10 68 ids(j) = ids(j)+1;
idamnjanovic@10 69 block_ids{j} = varargin{j}(ids(j)) : varargin{j}(ids(j))+blocksize(j)-1;
idamnjanovic@10 70 end
idamnjanovic@10 71 end
idamnjanovic@10 72