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