-
Notifications
You must be signed in to change notification settings - Fork 30
/
stDetect.m
126 lines (111 loc) · 3.16 KB
/
stDetect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function S = stDetect( I, model, stride, rescale_back )
% Detect sketch tokens in image.
%
% USAGE
% S = stDetect( I, model, [stride] )
%
% INPUTS
% I - [h x w x 3] color input image
% model - sketch token model trained with stTrain
% stride - [2] stride at which to compute sketch tokens
% rescale_back - [true] rescale after running stride
%
% OUTPUTS
% S - [h x w x (nTokens+1)] sketch token probability maps
%
% EXAMPLE
%
% See also stTrain, stChns
%
% Sketch Token Toolbox V0.95
% Copyright 2013 Joseph Lim [[email protected]]
% Please email me if you find bugs, or have suggestions or questions!
% Licensed under the Simplified BSD License [see bsd.txt]
if nargin<3
stride=2;
end
if nargin<4
rescale_back = true;
end
% compute features
sizeOrig=size(I);
opts=model.opts;
%opts.inputColorChannel = 'luv';
%opts.inputColorChannel = 'rgb';
I = imPad(I,opts.radius,'symmetric');
chns = stChns( I, opts );
[cids1,cids2] = computeCids(size(chns),opts);
if opts.nCells
chnsSs = convBox(chns,opts.cellRad);
else
chnsSs = [];
end
% run forest on image
S = stDetectMex( chns, chnsSs, model.thrs, model.fids, model.child, ...
model.distr, cids1, cids2, stride, opts.radius, opts.nChnFtrs );
% finalize sketch token probability maps
S = permute(S,[2 3 1]) * (1/opts.nTrees);
if ~rescale_back
%keyboard;
else
S = imResample( S, stride );
cr=size(S); cr=cr(1:2)-sizeOrig(1:2);
if any(cr)
S=S(1:end-cr(1),1:end-cr(2),:);
end
end
end
function [cids1,cids2] = computeCids( siz, opts )
% construct cids lookup for standard features
radius=opts.radius;
s=opts.patchSiz;
nChns=opts.nChns;
ht=siz(1);
wd=siz(2);
assert(siz(3)==nChns);
nChnFtrs=s*s*nChns;
fids=uint32(0:nChnFtrs-1);
rs=mod(fids,s);
fids=(fids-rs)/s;
cs=mod(fids,s);
ch=(fids-cs)/s;
cids = rs + cs*ht + ch*ht*wd;
% construct cids1/cids2 lookup for self-similarity features
n=opts.nCells;
m=opts.cellStep;
nCellTotal=(n*n)*(n*n-1)/2;
assert((n==0) || (mod(n,2)==1));
n1=(n-1)/2;
nSimFtrs=nCellTotal*nChns;
fids=uint32(0:nSimFtrs-1);
ind=mod(fids,nCellTotal);
ch=(fids-ind)/nCellTotal;
ind1 = []; ind2 = [];
k=0;
for i=1:n*n-1,
k1=n*n-i;
ind1(k+1:k+k1)=(0:k1-1);
k=k+k1;
end
k=0;
for i=1:n*n-1,
k1=n*n-i;
ind2(k+1:k+k1)=(0:k1-1)+i;
k=k+k1;
end
ind1=ind1(ind+1);
rs1=mod(ind1,n);
cs1=(ind1-rs1)/n;
ind2=ind2(ind+1);
rs2=mod(ind2,n);
cs2=(ind2-rs2)/n;
rs1=uint32((rs1-n1)*m+radius);
cs1=uint32((cs1-n1)*m+radius);
rs2=uint32((rs2-n1)*m+radius);
cs2=uint32((cs2-n1)*m+radius);
cids1 = rs1 + cs1*ht + ch*ht*wd;
cids2 = rs2 + cs2*ht + ch*ht*wd;
% combine cids for standard and self-similarity features
cids1=[cids cids1];
cids2=[zeros(1,nChnFtrs) cids2];
end