1 | % By Philip Torr 2002
|
---|
2 | % copyright Microsoft Corp.
|
---|
3 | %MAPSAC is the Bayesian version of MLESAC, and it is easier to pronounce!
|
---|
4 | %
|
---|
5 | % %designed for the good of the world by Philip Torr based on ideas contained in
|
---|
6 | % copyright Philip Torr and Microsoft Corp 2002
|
---|
7 | %
|
---|
8 | % [f,f_sq_errors, n_inliers,inlier_matches] = torr_mapsac_F(x1,y1,x2,y2, no_matches, m3, no_samp, T)
|
---|
9 | % f is fundamentalmatrix in 9 vector
|
---|
10 | % f_sq_errors are non robust errors on each match
|
---|
11 | % n_inliers is the no of inliers
|
---|
12 | % inlier_index is a vector with index no of each inlier
|
---|
13 | %
|
---|
14 | % x1,y1,x2,y2 are column vectors of the data no_matches by 4
|
---|
15 | % m3 is the 3rd homogeneous coordinate (256)
|
---|
16 | % no_samp is the number of samples to be taken (set to 0 if jump out required, at the moment jump out not implemented
|
---|
17 | % T is the threshold on the residuals, derived from MLESAC?MAPSAC paper
|
---|
18 | %
|
---|
19 | % at the moment it is assumed all is normalized so that Gaussian noise has sigma 1
|
---|
20 |
|
---|
21 | % /*
|
---|
22 | %
|
---|
23 | % @inproceedings{Torr93b,
|
---|
24 | % author = "Torr, P. H. S. and Murray, D. W.",
|
---|
25 | % title = "Outlier Detection and Motion Segmentation",
|
---|
26 | % booktitle = "Sensor Fusion VI",
|
---|
27 | % editor = "Schenker, P. S.",
|
---|
28 | % publisher = "SPIE volume 2059",
|
---|
29 | % note = "Boston",
|
---|
30 | % pages = {432-443},
|
---|
31 | % year = 1993 }
|
---|
32 | %
|
---|
33 | %
|
---|
34 | % @phdthesis{Torr:thesis,
|
---|
35 | % author="Torr, P. H. S.",
|
---|
36 | % title="Outlier Detection and Motion Segmentation",
|
---|
37 | % school=" Dept. of Engineering Science, University of Oxford",
|
---|
38 | % year=1995}
|
---|
39 | %
|
---|
40 | %
|
---|
41 | %
|
---|
42 | % @article{Torr97c,
|
---|
43 | % author="Torr, P. H. S. and Murray, D. W. ",
|
---|
44 | % title="The Development and Comparison of Robust Methods for Estimating the Fundamental Matrix",
|
---|
45 | % journal="IJCV",
|
---|
46 | % volume = 24,
|
---|
47 | % number = 3,
|
---|
48 | % pages = {271--300},
|
---|
49 | % year=1997
|
---|
50 | % }
|
---|
51 | %
|
---|
52 | %
|
---|
53 | %
|
---|
54 | %
|
---|
55 | % @article{Torr99c,
|
---|
56 | % author = "Torr, P. H. S. and Zisserman, A",
|
---|
57 | % title ="MLESAC: A New Robust Estimator with Application to Estimating Image Geometry ",
|
---|
58 | % journal = "CVIU",
|
---|
59 | % Volume = {78},
|
---|
60 | % number = 1,
|
---|
61 | % pages = {138-156},
|
---|
62 | % year = 2000}
|
---|
63 | %
|
---|
64 | % %MAPSAC is the Bayesian version of MLESAC, and it is easier to pronounce!
|
---|
65 | % it is described in:
|
---|
66 | %
|
---|
67 | % @article{Torr02d,
|
---|
68 | % author = "Torr, P. H. S.",
|
---|
69 | % title ="Bayesian Model Estimation and Selection for Epipolar Geometry and
|
---|
70 | % Generic Manifold Fitting",
|
---|
71 | % journal = "IJCV",
|
---|
72 | % Volume = {?},
|
---|
73 | % number = ?,
|
---|
74 | % pages = {?},
|
---|
75 | % url = "http://research.microsoft.com/~philtorr/",
|
---|
76 | % year = 2002}
|
---|
77 | %
|
---|
78 |
|
---|
79 | %threshold is the maximum squared value of the residuals
|
---|
80 | %no_matches is the number of matches
|
---|
81 | %no_samp is the number of random samples to be taken
|
---|
82 | %m3 is the estimate of the 3rf projective coordinate (f in pixels)
|
---|
83 |
|
---|
84 | %the F matrix is defined like:
|
---|
85 | % (nx2, ny2, m3) f(1 2 3) nx1
|
---|
86 | % (4 5 6) ny1
|
---|
87 | % (7 8 9) m3
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | %we minimize a robust function min(e^2,T) see mapsac paper.
|
---|
92 |
|
---|
93 |
|
---|
94 | function [f,f_sq_errors, n_inliers,inlier_index] = torr_napsac_F(x1,y1,x2,y2, no_matches, m3, no_samp, T)
|
---|
95 | %disp('This just does calculation of perfect data,for test')
|
---|
96 | %disp('Use estf otherwise')
|
---|
97 | %f = rand(9);
|
---|
98 |
|
---|
99 |
|
---|
100 | %%%%%%%%%%debug
|
---|
101 | %used for debugging:
|
---|
102 | no_trials = 1;
|
---|
103 | max_inliers = 0;
|
---|
104 | %%%%%%%%%%end debug
|
---|
105 |
|
---|
106 | for(i = 1:no_samp)
|
---|
107 |
|
---|
108 | %NAPSAC frenzyoid! first pick one point then take 6 nearest, described in thesis/china paper
|
---|
109 | choice = randperm(no_matches);
|
---|
110 | %set up local design matrix, here we estimate from 7 matches
|
---|
111 | distance_xyxy = (x1 - x1(choice(1))).^2 + (x2 - x2(choice(1))).^2 + (y1 - y1(choice(1))).^2 + (y2 - y2(choice(1))).^2;
|
---|
112 | [sorted_distance_xyxy, index_distance_xyxy] = sort(distance_xyxy);
|
---|
113 |
|
---|
114 |
|
---|
115 | %next randomly permute the best 50 matches
|
---|
116 | choice2 = randperm(60);
|
---|
117 |
|
---|
118 | for (j = 1:7)
|
---|
119 | tx1(j) = x1( index_distance_xyxy(choice2(j)));
|
---|
120 | tx2(j) = x2( index_distance_xyxy(choice2(j)));
|
---|
121 | ty1(j) = y1( index_distance_xyxy(choice2(j)));
|
---|
122 | ty2(j) = y2( index_distance_xyxy(choice2(j)));
|
---|
123 | end % for (j = 1:7)
|
---|
124 |
|
---|
125 | % tx1 = x1( index_distance_xyxy(1:7));
|
---|
126 | % tx2 = x2( index_distance_xyxy(1:7));
|
---|
127 | % ty1 = y1( index_distance_xyxy(1:7));
|
---|
128 | % ty2 = y2( index_distance_xyxy(1:7));
|
---|
129 |
|
---|
130 | %produces 1 or 3 solutions.
|
---|
131 | [no_F big_result]= torr_F_constrained_fit(tx1,ty1,tx2,ty2,m3);
|
---|
132 |
|
---|
133 | for j = 1:no_F
|
---|
134 | ft = big_result(j,:);
|
---|
135 |
|
---|
136 | %get squared errors
|
---|
137 | et = torr_errf2(ft,x1,y1,x2,y2, no_matches, m3);
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | %capped residuals
|
---|
143 | cet = min(et,T);
|
---|
144 | sse = cet' * cet;
|
---|
145 | % use sse 0 to bring it to a reasonable value
|
---|
146 | if ((i ==1) & (j ==1))
|
---|
147 | f = ft;
|
---|
148 | bestsse = sse;
|
---|
149 | elseif bestsse > sse
|
---|
150 | f = ft;
|
---|
151 | bestsse = sse;
|
---|
152 | bestcet = cet; %store best set of residuals
|
---|
153 | end %if
|
---|
154 |
|
---|
155 |
|
---|
156 | %monitor progress %debug
|
---|
157 | inlier_index = find((et < T) == 1);
|
---|
158 | mapsac_inliers(no_trials) = length(inlier_index);
|
---|
159 | if mapsac_inliers(no_trials) > max_inliers
|
---|
160 | max_inliers = mapsac_inliers(no_trials);
|
---|
161 | else
|
---|
162 | mapsac_inliers(no_trials) = max_inliers;
|
---|
163 | end
|
---|
164 | no_trials = no_trials + 1;
|
---|
165 | %%%%%%%%end debug
|
---|
166 |
|
---|
167 |
|
---|
168 |
|
---|
169 | end
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 | end %for(i = 1:no_samp)
|
---|
174 |
|
---|
175 |
|
---|
176 | %calculate squared errors (distance to manifold of F)
|
---|
177 | f_sq_errors = torr_errf2(f,x1,y1,x2,y2, no_matches, m3);
|
---|
178 | %next generate index set of inliers
|
---|
179 | inlier_index = find((f_sq_errors < T) == 1);
|
---|
180 | n_inliers = length(inlier_index);
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | %%%%%%%%%%debug
|
---|
185 | %for NAPSAC paper
|
---|
186 | no_matches
|
---|
187 | n_inliers
|
---|
188 | no_trials
|
---|
189 |
|
---|
190 | mapsac_inliers(1:30)
|
---|
191 | %find out how many it took to get to n_inliers
|
---|
192 | perc = n_inliers;
|
---|
193 | map_index = find((mapsac_inliers < perc) == 1);
|
---|
194 | perc100 = length(map_index)+1
|
---|
195 | %find out how many it took to get to n_inliers
|
---|
196 |
|
---|
197 | perc = n_inliers * 0.9;
|
---|
198 | map_index = find((mapsac_inliers < perc) == 1);
|
---|
199 | perc90 = length(map_index)+1
|
---|
200 |
|
---|
201 | perc = n_inliers * 0.8;
|
---|
202 | map_index = find((mapsac_inliers < perc) == 1);
|
---|
203 | perc80 = length(map_index)+1
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 | perc = n_inliers * 0.7;
|
---|
208 | map_index = find((mapsac_inliers < perc) == 1);
|
---|
209 | perc70 = length(map_index)+1
|
---|
210 |
|
---|
211 |
|
---|
212 |
|
---|
213 | perc = n_inliers * 0.6;
|
---|
214 | map_index = find((mapsac_inliers < perc) == 1);
|
---|
215 | perc60 = length(map_index)+1
|
---|
216 |
|
---|
217 |
|
---|
218 | n_inliers
|
---|
219 |
|
---|
220 | disp('Napsac');
|
---|
221 | %
|
---|
222 | % figure
|
---|
223 | % hold on
|
---|
224 | % for i = 1:no_trials-1
|
---|
225 | % plot(i, mapsac_inliers(i),'rs');
|
---|
226 | % end
|
---|
227 | % hold off
|
---|
228 | % %%%%%%%%%%%%end debug
|
---|
229 |
|
---|
230 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
231 |
|
---|
232 | |
---|