1 | %GENDATI Create dataset from randomly selected windows in a given image
|
---|
2 | %
|
---|
3 | % A = GENDATI(IMAGE,WSIZE,N,LABEL)
|
---|
4 | %
|
---|
5 | % INPUT
|
---|
6 | % IMAGE - Image of any dimensionality
|
---|
7 | % WSIZE - Vector with size of the window
|
---|
8 | % N - Number windows to be generated
|
---|
9 | % LABEL - Optional string or number with label for all objects
|
---|
10 | %
|
---|
11 | % OUTPUT
|
---|
12 | % A - Dataset of N objects and prod(WSIZE) features
|
---|
13 | %
|
---|
14 | % DESCRIPTION
|
---|
15 | % Windows of the specified size are arbitrarily positioned in the
|
---|
16 | % image, converted to a row vector and stored in the dataset A.
|
---|
17 | %
|
---|
18 | % If specified, all objects have label LABEL. Otherwise they are
|
---|
19 | % unlabeled.
|
---|
20 | %
|
---|
21 | % SEE ALSO
|
---|
22 | % DATASETS
|
---|
23 |
|
---|
24 | % Copyright: R.P.W. Duin, r.p.w.duin@prtools.org
|
---|
25 | % Faculty EWI, Delft University of Technology
|
---|
26 | % P.O. Box 5031, 2600 GA Delft, The Netherlands
|
---|
27 |
|
---|
28 | function a = gendati(im,ws,m,label)
|
---|
29 |
|
---|
30 | if nargin < 4, label = []; end
|
---|
31 | if nargin < 3, m = 100; end
|
---|
32 |
|
---|
33 | n = length(ws);
|
---|
34 | imsize = size(im);
|
---|
35 | if length(imsize) ~= n
|
---|
36 | error('Window size should have same number of components as image size')
|
---|
37 | end
|
---|
38 |
|
---|
39 | if any(ws > imsize)
|
---|
40 | error('Window size should not be larger than image size')
|
---|
41 | end
|
---|
42 |
|
---|
43 | we = prod(ws);
|
---|
44 | n = length(ws);
|
---|
45 | window_offset = [0:ws(1)-1]';
|
---|
46 | for j=2:n
|
---|
47 | wo = window_offset;
|
---|
48 | for i=1:ws(j)-1
|
---|
49 | wo = wo + prod(imsize(1:j-1));
|
---|
50 | window_offset = cat(j,window_offset,wo);
|
---|
51 | end
|
---|
52 | end
|
---|
53 | window_offset = window_offset(:)';
|
---|
54 |
|
---|
55 | N = cell(1,n);
|
---|
56 | for j=1:n
|
---|
57 | R = imsize(j)-ws(j)+1;
|
---|
58 | N(j) = {ceil(rand(m,1)*R)};
|
---|
59 | end
|
---|
60 | N = sub2ind(imsize,N{:});
|
---|
61 |
|
---|
62 | a = zeros(m,prod(ws));
|
---|
63 | for i=1:m
|
---|
64 | a(i,:) = im(N(i)+window_offset);
|
---|
65 | end
|
---|
66 |
|
---|
67 | a = dataset(a);
|
---|
68 | a = setfeatsize(a,ws);
|
---|
69 | if ~isempty(label)
|
---|
70 | a = setlabels(a,label);
|
---|
71 | end |
---|