comparison util/ksvd utils/sampgrid.m @ 70:c3eca463202d

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