-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdemo.m
169 lines (147 loc) · 5.15 KB
/
demo.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
close all;clc;
% disp('Fast Directional Chamfer Matching Demo');
templateImg = rgb2gray(imread('DemoImg/template.png'));
matchingTargetImg = imread('DemoImg/matching_target.jpg');
templateImgCannyEdge = edge(templateImg,'Canny',0.4);
matchingTargetImgCannyEdge = edge(rgb2gray(matchingTargetImg),'Canny');
% imshow(templateImgCannyEdge);
% imshow(matchingTargetImgCannyEdge);
%//==================================================================
%// Basic Configuration
%//==================================================================
%templateEdgeMap = imread('Template/handdrawn_bottle.pgm');
templateEdgeMap = templateImgCannyEdge;
% figure(1);
% subplot(2,2,1);
% imshow(imread('Query/ray.jpg'));
% title('Image 1');
% subplot(2,2,2);
% imshow(imread('Query/ceazanne.jpg'));
% title('Image 2');
% subplot(2,2,3);
% imshow(imread('Query/four.jpg'));
% title('Image 3');
% subplot(2,2,4);
% imshow(imread('Query/pale.jpg'));
% title('Image 4');
% x = input('Choose the image by number (Default 1) :');
% x = 1;
%
% switch x
% case 1
% query = imread('Query/ray_edges.pgm');
% queryColor = imread('Query/ray.jpg');
% case 2
% query = imread('Query/ceazanne_edges.pgm');
% queryColor = imread('Query/ceazanne.jpg');
% case 3
% query = imread('Query/four_edges.pgm');
% queryColor = imread('Query/four.jpg');
% case 4
% query = imread('Query/pale_edges.pgm');
% queryColor = imread('Query/pale.jpg');
% otherwise
% query = imread('Query/ray_edges.pgm');
% queryColor = imread('Query/ray.jpg');
% end
query = matchingTargetImgCannyEdge;
queryColor = matchingTargetImg;
threshold = 0.12;
lineMatchingPara = struct(...
'NUMBER_DIRECTION',60,...
'DIRECTIONAL_COST',0.5,...
'MAXIMUM_EDGE_COST',30,...
'MATCHING_SCALE',1.0,...
'TEMPLATE_SCALE',0.6761,...
'BASE_SEARCH_SCALE',1.20,...
'MIN_SEARCH_SCALE',-7,...
'MAX_SEARCH_SCALE',0,...
'BASE_SEARCH_ASPECT',1.1,...
'MIN_SEARCH_ASPECT',-1,...
'MAX_SEARCH_ASPECT',1,...
'SEARCH_STEP_SIZE',2,...
'SEARCH_BOUNDARY_SIZE',2,...
'MIN_COST_RATIO',1.0...
);
%//==================================================================
%// Convert edge map into line representation
%//==================================================================
% figure(1)
% subplot(1,2,1);
% imshow(255-templateEdgeMap,[]);
% title('The tempalte edge map.');
% disp('This is the template shape.');
% disp('press Enter to continue...');
% pause;
% Set the parameter for line fitting function
lineFittingPara = struct(...
'SIGMA_FIT_A_LINE',0.5,...
'SIGMA_FIND_SUPPORT',0.5,...
'MAX_GAP',2.0,...
'N_LINES_TO_FIT_IN_STAGE_1',300,...
'N_TRIALS_PER_LINE_IN_STAGE_1',100,...
'N_LINES_TO_FIT_IN_STAGE_2',100000,...
'N_TRIALS_PER_LINE_IN_STAGE_2',1);
% convert the template edge map into a line representation
[lineRep lineMap] = mex_fitline(double(templateEdgeMap),lineFittingPara);
% figure(1)
% subplot(1,2,2);
% imshow(255-lineMap,[]);
% display the top few line segments to illustrate the representation
nLine = size(lineRep,1);
% fprintf('\n/*Illustration of the linear representation*/\n');
% fprintf('Line ID ( Pt1.x , Pt1.y ) -> ( Pt2.x , Pt2.y )\n');
for i=1:min(nLine,3)
% fprintf('Line %02d ( %d , %d ) -> ( %d , %d )\n',i,...
% lineRep(i,1),lineRep(i,2),lineRep(i,3),lineRep(i,4));
end
% fprintf('...\n');
% title('The linear representation map.');
% disp('This is the linear representation of the template shape.');
% disp('press Enter to continue...');
% pause;
%//==================================================================
%// FDCM detection
%//==================================================================
figure(2);
hold on;
imshow(queryColor,[]);
title('query image');
disp('This is the query image in which the template shape is used to detect similar object instances.');
disp('press Enter to continue...');
pause;
% Set the parameter for line fitting function
lineFittingPara2 = struct(...
'SIGMA_FIT_A_LINE',0.5,...
'SIGMA_FIND_SUPPORT',0.5,...
'MAX_GAP',2.0,...
'N_LINES_TO_FIT_IN_STAGE_1',0,...
'N_TRIALS_PER_LINE_IN_STAGE_1',0,...
'N_LINES_TO_FIT_IN_STAGE_2',100000,...
'N_TRIALS_PER_LINE_IN_STAGE_2',1);
template = cell(1);
tempate{1} = lineRep;
[detWinds] = mex_fdcm_detect(double(query),tempate,threshold,...
lineFittingPara2,lineMatchingPara);
nDetection = size(detWinds,1);
wins = double(zeros(min(nDetection,2), 6));
% fprintf('Wind ID ( x0 , y0 , width , height, cost, count )\n');
for i=1:min(nDetection,2)
wins(i, :) = [detWinds(i,1),detWinds(i,2),detWinds(i,3),detWinds(i,4),detWinds(i,5),detWinds(i,6)];
end
disp(wins);
color = [0 1 0];
lineWidth = 3;
for i=1:size(detWinds)
sx = detWinds(i,1);
ex = sx + detWinds(i,3);
sy = detWinds(i,2);
ey = sy + detWinds(i,4);
line([sx ex],[sy sy],'Color',color,'LineWidth',lineWidth);
line([sx ex],[ey ey],'Color',color,'LineWidth',lineWidth);
line([sx sx],[sy ey],'Color',color,'LineWidth',lineWidth);
line([ex ex],[sy ey],'Color',color,'LineWidth',lineWidth);
end
title('Detection result');
disp('This is the detected object isntance.');
disp('The demo is finished.');