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