-
Notifications
You must be signed in to change notification settings - Fork 39
/
edgesDetectDL.m
82 lines (72 loc) · 2.64 KB
/
edgesDetectDL.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
function [E,O,inds,segs] = edgesDetectDL( I, model )
% Detect edges in image by deep contour features.
% The code is modified from Structured Edge Detection Toolbox
% get parameters
opts=model.opts; opts.nTreesEval=min(opts.nTreesEval,opts.nTrees);
if(~isfield(opts,'sharpen')), opts.sharpen=0; end
if(~isfield(model,'segs')), model.segs=[]; model.nSegs=[]; end
opts.stride=max(opts.stride,opts.shrink); model.opts=opts;
testFeatFir = [opts.modelDir '/feat/'];
if( opts.multiscale )
% if multiscale run edgesDetect multiple times
ss=2.^(-1:1); k=length(ss); inds=cell(1,k); segs=inds;
siz=size(I); model.opts.multiscale=0; model.opts.nms=0; E=0;
for i=1:k, s=ss(i);
I1=imResample(I,s);
if(s~=1)
model.oriIm = 0;
else
model.oriIm = 1;
end
if(nargout<4),
[E1,inds{i}]=edgesDetectDL(I1,model);
else
[E1,inds{i},segs{i}]=edgesDetectDL(I1,model);
end
E=E+imResample(E1,siz(1:2));
end; E=E/k; model.opts=opts;
else
% pad image, making divisible by 4
siz=size(I); r=opts.imWidth/2; p=[r r r r];
p([2 4])=p([2 4])+mod(4-mod(siz(1:2)+2*r,4),4);
I = imPad(I,p,'symmetric');
% compute features and apply forest to image
if(isfield(model,'oriIm') && model.oriIm ~= 1)
deepFeat = edgesChnsDL2(I, opts);
else
if exist([testFeatFir model.imgId '_deepFeat.mat'], 'file');
load([testFeatFir model.imgId '_deepFeat.mat']);
else
deepFeat = edgesChnsDL2(I, opts);
save([testFeatFir model.imgId '_deepFeat.mat'], 'deepFeat');
end
end
if(isfield(opts, 'selected_dims') && ~isempty(opts.selected_dims))
deepFeat = deepFeat(:, :, opts.selected_dims);
end
% [deepFeat,~] = edgesChns( I, opts );
% deepFeat = deepFeat(:, :, 4);
s=opts.sharpen; if(s), I=convTri(rgbConvert(I,'rgb'),1); end
if(nargout<4), [E,inds] = edgesDetectMexCNNFeats(model,I,deepFeat);
else [E,inds,segs] = edgesDetectMexCNNFeats(model,I,deepFeat); end
% normalize and finalize edge maps
t=opts.stride^2/opts.gtWidth^2/opts.nTreesEval; r=opts.gtWidth/2;
if(s==0), t=t*2; elseif(s==1), t=t*1.8; else t=t*1.66; end
E=E(1+r:siz(1)+r,1+r:siz(2)+r,:)*t; E=convTri(E,1);
end
% compute approximate orientation O from edges E
if( opts.nms==-1 ), O=[]; elseif( nargout>1 || opts.nms )
[Ox,Oy]=gradient2(convTri(E,4));
[Oxx,~]=gradient2(Ox); [Oxy,Oyy]=gradient2(Oy);
O=mod(atan(Oyy.*sign(-Oxy)./(Oxx+1e-5)),pi);
end
% perform nms
if( opts.nms>0 ), E=edgesNmsMex(E,O,1,5,1.01,opts.nThreads); end
Border=5;
for s=1:Border,
E([s end-s+1],:,:)=E([s end-s+1],:,:)*(s-1)/Border;
end
for s=1:Border,
E(:,[s end-s+1],:)=E(:,[s end-s+1],:)*(s-1)/Border;
end
end