source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/image3dstiching/PosrEst/TestWholePostMatch.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 10.0 KB
Line 
1% *  This code was used in the following articles:
2% *  [1] Learning 3-D Scene Structure from a Single Still Image,
3% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
4% *      In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
5% *      (best paper)
6% *  [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
7% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
8% *      In ICCV workshop on Virtual Representations and Modeling
9% *      of Large-scale environments (VRML), 2007.
10% *  [3] 3-D Depth Reconstruction from a Single Still Image,
11% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
12% *      International Journal of Computer Vision (IJCV), Aug 2007.
13% *  [6] Learning Depth from Single Monocular Images,
14% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
15% *      In Neural Information Processing Systems (NIPS) 18, 2005.
16% *
17% *  These articles are available at:
18% *  http://make3d.stanford.edu/publications
19% *
20% *  We request that you cite the papers [1], [3] and [6] in any of
21% *  your reports that uses this code.
22% *  Further, if you use the code in image3dstiching/ (multiple image version),
23% *  then please cite [2].
24% * 
25% *  If you use the code in third_party/, then PLEASE CITE and follow the
26% *  LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
27% *
28% *  Finally, this code is for non-commercial use only.  For further
29% *  information and to obtain a copy of the license, see
30% *
31% *  http://make3d.stanford.edu/publications/code
32% *
33% *  Also, the software distributed under the License is distributed on an
34% * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
35% *  express or implied.   See the License for the specific language governing
36% *  permissions and limitations under the License.
37% *
38% */
39function [Pair fail]=TestWholePostMatch(defaultPara, ImgInfo1, ImgInfo2, PriorPose)
40
41% This function load the Initial SurfMatches and the OccluSurfMatches
42% then, do RANSAC algorithm to prune the bad matches out
43% Calucluate New Camera Pose (R and T)
44
45% Default Parameters -----------------------------
46fail = 0;
47% ------------------------------------------------
48
49% Initialize Variables ---------------------------
50Img1 = ImgInfo1.ExifInfo.IDName;
51Img2 = ImgInfo2.ExifInfo.IDName;
52I1=imreadbw([defaultPara.Fdir '/pgm/' Img1 '.pgm']); % function from sift
53I2=imreadbw([defaultPara.Fdir '/pgm/' Img2 '.pgm']); % function from sift
54% ------------------------------------------------
55
56% Combine Both Initial SurfMatches and the OccluSurfMatches -------------------------
57% Also Assign Confidence of each Matches
58% (Used as RANSAC Sampling Distribution)
59% 0) load cleaned Fisrt set of matches
60%defaultPara.MaxUniqueRatio
61load([defaultPara.Fdir '/data/' Img1 '_' Img2 '_PoseMatch.mat']);
62defaultPara.MaxUniqueRatio = 100;
63% 1) load Initial SurfMatches
64[f1, f2, matches] = readSurfMatches(Img1, Img2, defaultPara.Fdir, ...
65                    [ defaultPara.Type 'Dense_' num2str(defaultPara.AbsThre) '_' num2str(defaultPara.RatioThre)], 1, 1);
66if isempty(f1)
67        [f1, f2, matches] = readSurfMatches(Img1, Img2, defaultPara.Fdir, ...
68                        ['Dense_' num2str(defaultPara.AbsThre) '_' num2str(defaultPara.RatioThre)], 1, 1);
69end
70
71% 2) load OccluSurfMatches
72[f1, f2, OccluedMatches] = readSurfMatches(Img1, Img2, defaultPara.Fdir, [ defaultPara.Type 'OccluDense'], 1, 1, 3);% need UniqueRatio//Min jobs
73UniqueRatio = OccluedMatches(3,:);
74%UniqueRatio = min(UniqueRatio,defaultPara.MaxUniqueRatio);
75Ptr = UniqueRatio > defaultPara.MaxUniqueRatio;
76UniqueRatio(Ptr) = Inf;%defaultPara.MaxUniqueRatio;
77
78OccluedMatches = OccluedMatches(1:2,:);
79if false
80        disp('use CleanMatches');
81        matches = Matches;
82        % removing initial matches that inconsistent with OccluedMatches
83        [c i] = intersect( matches(1,:), OccluedMatches(1,:));
84        matches(:,i) = [];
85        [c i] = intersect( matches(2,:), OccluedMatches(2,:));
86        matches(:,i) = [];
87        matches = [matches OccluedMatches];
88
89else
90        disp('Number of Occlumatches used')
91        size( OccluedMatches,2)
92
93        % removing initial matches that inconsistent with OccluedMatches
94        [c i] = intersect( matches(1,:), OccluedMatches(1,:));
95        matches(:,i) = [];
96        [c i] = intersect( matches(2,:), OccluedMatches(2,:));
97        matches(:,i) = [];
98
99        NumInitialMatches = size(matches, 2);
100        matches = [matches OccluedMatches];
101        if isempty(matches)
102           disp('Zeros Surf matches');
103           fail = 1;
104           return;
105        end   
106
107        % 3) Construct Prior Dist for All matches
108        NMatches = size(matches, 2);
109        PriorDist = ones(1, NMatches);
110        PriorDist( (NumInitialMatches+1):end) = UniqueRatio;
111        % 4) RANSAC
112        disp('Number of matches used')
113       
114        if defaultPara.Flag.StorageDataBeforeRansac
115                disp('Storage Data Before Ransac');
116                save([defaultPara.Fdir '/data/DataBeforeRansac.mat']);
117%               return;
118        end
119
120        % Ensemble method to determine confidence of inliers
121        fittingfn = @fundmatrix;
122        distfnEnsmble = @fundistEnsmble;
123        degenfn   = @isdegenerate;
124        x = [[f1(:, matches(1,:)); ones(1, NMatches)]; [f2(:, matches(2,:)); ones(1, NMatches)]];
125        [ SampsonDist ] = EnsembleRansac(defaultPara, x, fittingfn, distfnEnsmble, degenfn, 8, PriorDist', min(NMatches*10, defaultPara.MAXEnsembleSamples), 0);
126        kurtosisValue =kurtosis(SampsonDist');
127
128        [F0, inliers, NewDist, fail, ind]=GeneralRansac(defaultPara, f1, f2, matches, [], [], kurtosisValue', 8);
129        figure(100); plotmatches(I1,I2,f1, f2,matches(:,inliers), 'Stacking', 'h', 'Interactive', 3);
130        matches = matches(:,inliers); % accept the pruning result
131        if isempty(matches)
132           disp('Zeros After Ransac matches');
133           fail = 2;
134           return;
135        end
136end
137% ------------------------------------------------------------------------------------
138
139
140% Apply Bundle Adjustment Refinment Algorithm to Prune the Matches further -----------
141% And Estimated the Pose
142% 1) initialize the 3D position of the matches given Prior Pose and Depths
143x_calib = [ inv(defaultPara.InrinsicK1)*[ f1(:,matches(1,:)); ones(1,size(matches,2))];...
144              inv(defaultPara.InrinsicK2)*[ f2(:,matches(2,:)); ones(1,size(matches,2))]];
145
146% Estimate F using NonLine LS on every inlier
147MatchDensityWeights1 = CalMatchDensityWeights(f1(:,matches(1,:)), max(size(I1))/defaultPara.radius2imageSizeRatio);
148MatchDensityWeights2 = CalMatchDensityWeights(f2(:,matches(2,:)), max(size(I2))/defaultPara.radius2imageSizeRatio);
149MatchDensityWeights =mean([MatchDensityWeights1; MatchDensityWeights2], 1);
150F = getFnpt( F0, f1(:, matches(1,:))',  f2(:, matches(2,:))', MatchDensityWeights);
151E = defaultPara.InrinsicK2'*F*defaultPara.InrinsicK1; % Camera essential Matrix
152if ~isempty(PriorPose)
153        [ R0, T0, lamda1, lamda2, inlier, Error] = EstPose( defaultPara, E, x_calib, [], PriorPose.R(1:3,:));
154else
155        [ R0, T0, lamda1, lamda2, inlier, Error] = EstPose( defaultPara, E, x_calib, [], []);
156end
157T0 = [T0; - R0'*T0];
158R0 = [R0; R0'];
159matches = matches(:,inlier); % delet matches give negative depths
160x_calib = x_calib(:,inlier);
161lamda1 = lamda1(inlier);
162lamda2 = lamda2(inlier);
163
164X_obj_1 = x_calib(1:3,:).*repmat(lamda1, 3, 1);
165X_obj_2 = R0(4:6,:)*(x_calib(4:6,:).*repmat(lamda2, 3, 1)) + repmat(T0(4:6), 1, size(matches,2));
166X_obj = (X_obj_1+X_obj_2)/2;
167
168% 2)
169[R T X_obj_BA X_im_BA dist1_BA dist2_BA]=SparseBAWraper(defaultPara, R0(1:3,:), T0(1:3), [f1(:,matches(1,:)); f2(:,matches(2,:))], X_obj, [ ImgInfo1 ImgInfo2], 1);
170if false % Min Modified for not pruning using BundleAdjustment
171        if all(isnan( dist1_BA)) || isempty(R) || any(isnan(R(:)))
172                disp('BA failed');
173                fail = 3;
174                return;
175        end
176        while length(X_im_BA) >= defaultPara.MinimumNumMatches
177                disp('BundleAdjClean')
178            outlier_thre1 = prctile(dist1_BA,90);
179            outlier_thre2 = prctile(dist2_BA,90);
180            if outlier_thre1 >= defaultPara.ReProjErrorThre || outlier_thre2 >= defaultPara.ReProjErrorThre
181                Outlier = dist1_BA > outlier_thre1 | dist2_BA > outlier_thre2;
182                matches(:,Outlier) = [];
183                if isempty(matches)
184                    disp('Zeros After BA matches');
185                    fail = 4;
186                    return;
187                end
188                lamda1(Outlier) = [];
189                lamda2(Outlier) = [];
190                X_obj_BA(:,Outlier) = [];
191                x_calib(:,Outlier) = [];
192                [R T X_obj_BA X_im_BA dist1_BA dist2_BA]=SparseBAWraper(defaultPara, R, T, [f1(:,matches(1,:)); f2(:,matches(2,:))], X_obj_BA, [ ImgInfo1 ImgInfo2], 1);
193                if all(isnan( dist1_BA)) || isempty(R) || any(isnan(R(:)))
194                        disp('BA failed');
195                    fail = 5;
196                        return;
197                end
198            else
199                break;
200            end
201        end   
202end
203% ------------------------------------------------------------------------------------
204
205
206% Triangulation ----------------------------------------------------------------------
207% modified the x_calib
208tempf1 = X_im_BA(1:2,:);
209tempf2 = X_im_BA(3:4,:);
210x_calib = [ inv(defaultPara.InrinsicK1)*[ tempf1; ones(1,length(tempf1))];...
211             inv(defaultPara.InrinsicK2)*[ tempf2; ones(1,length(tempf2))]];
212[ lamda1 lamda2 Error] = triangulation( defaultPara, R, T, x_calib);
213% Clean outlier triangulated depth
214%LamdaOutlier = lamda1 > 1000 | lamda1 <1;
215%matches(:,LamdaOutlier) = [];
216%if isempty(matches)
217%    disp('Zeros After Tri matches');
218%    fail = 6;
219%    return;
220%end
221
222%lamda1(LamdaOutlier) = [];
223%lamda2(LamdaOutlier) = [];
224%x_calib(:,LamdaOutlier) = [];
225
226% Pair Image Depth Scale
227[D1 IND1] = PorjPosi2Depth(size(I1), size(ImgInfo1.Model.Depth.FitDepth), f1(:,matches(1,:)), ImgInfo1.Model.Depth.FitDepth);
228[D2 IND2] = PorjPosi2Depth(size(I2), size(ImgInfo2.Model.Depth.FitDepth), f2(:,matches(2,:)), ImgInfo2.Model.Depth.FitDepth);
229Depth1ProjDepthRatio = sqrt(sum(x_calib(1:3,:).^2, 1));
230Depth2ProjDepthRatio = sqrt(sum(x_calib(4:6,:).^2, 1));
231DProj1 = D1./Depth1ProjDepthRatio;
232DProj2 = D2./Depth2ProjDepthRatio;
233[DepthScale1] = UniformDepthScale( defaultPara, DProj1, lamda1, ones(1,length(lamda1)));
234[DepthScale2] = UniformDepthScale( defaultPara, DProj2, lamda2, ones(1,length(lamda2)) );
235%if DepthScale1 > 20 | DepthScale1 <0.05 | DepthScale2 > 20 | DepthScale2 <0.05 %//Min used to use 10 and 0.2
236%   disp('Unrealistic in Rescaleing the depth, Check matchings');
237%   fail = 6;
238%   return;
239%end
240
241Pair.lamda = [lamda1; lamda2];%//Min add for debug
242Pair.DepthScale = [DepthScale1; DepthScale2];
243Pair.R = R;
244Pair.T = T;
245Pair.Xim = [f1(:,matches(1,:)); f2(:,matches(2,:))];
246% -----------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.