Mercurial > hg > smallbox
comparison examples/private/sampgrid.m @ 1:7750624e0c73 version0.5
(none)
author | idamnjanovic |
---|---|
date | Thu, 05 Nov 2009 16:36:01 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:5181bee80bc1 | 1:7750624e0c73 |
---|---|
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 | |
30 if (numel(blocksize)==1) | |
31 blocksize = ones(1,p)*blocksize; | |
32 end | |
33 | |
34 n = zeros(1,p); | |
35 for i = 1:p | |
36 n(i) = length(varargin{i}); | |
37 end | |
38 | |
39 nsamps = prod(n); | |
40 | |
41 % create y of the same class as x | |
42 y = zeros(prod(blocksize),nsamps,class(x)); | |
43 | |
44 % ids() contains the index of the current block in I1..Ip | |
45 ids = ones(p,1); | |
46 | |
47 % block_ids contains the indices of the current block in X | |
48 block_ids = cell(p,1); | |
49 for j = 1:p | |
50 block_ids{j} = varargin{j}(1) : varargin{j}(1)+blocksize(j)-1; | |
51 end | |
52 | |
53 for k = 1:nsamps | |
54 block = x(block_ids{:}); | |
55 y(:,k) = block(:); | |
56 | |
57 % increment ids() and block_ids{} | |
58 if (k<nsamps) | |
59 j = 1; | |
60 while (ids(j) == n(j)) | |
61 ids(j) = 1; | |
62 block_ids{j} = varargin{j}(1) : varargin{j}(1)+blocksize(j)-1; | |
63 j = j+1; | |
64 end | |
65 ids(j) = ids(j)+1; | |
66 block_ids{j} = varargin{j}(ids(j)) : varargin{j}(ids(j))+blocksize(j)-1; | |
67 end | |
68 end | |
69 | |
70 | |
71 % | |
72 % p = ndims(x); | |
73 % | |
74 % n = zeros(1,p); | |
75 % for i = 1:p | |
76 % n(i) = length(varargin{i}); | |
77 % end | |
78 % | |
79 % nsamps = prod(n); | |
80 % | |
81 % % create y of the same class as x | |
82 % y = zeros(prod(blocksize),nsamps,class(x)); | |
83 % | |
84 % id = cell(p,1); | |
85 % for k = 1:nsamps | |
86 % [id{:}] = ind2sub(n,k); | |
87 % for j = 1:p | |
88 % id{j} = varargin{j}(id{j}) : varargin{j}(id{j})+blocksize(j)-1; | |
89 % end | |
90 % block = x(id{:}); | |
91 % y(:,k) = block(:); | |
92 % end | |
93 |