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 | % */
|
---|
39 | function [] = OcclusionMatchRefineMent(defaultPara, Wrlname, PairList) |
---|
40 | |
---|
41 | % This function do three thing |
---|
42 | % 1) detect the occlusion from the current model |
---|
43 | % 2) Match the Occlued area to find any match |
---|
44 | % 3) reinference including the new matches information |
---|
45 | |
---|
46 | % Input: |
---|
47 | % 1) defaultPara - all parameter share for the whole stitching3d folder |
---|
48 | % 2) Wrlname - the name of the model, different by the PairList |
---|
49 | % 3) PairList - the linearly order of image that been added to the model |
---|
50 | |
---|
51 | % Fixed constant --- need to be put in defaultPara by Min later |
---|
52 | NegI = diag([1 1 -1]); |
---|
53 | PostFixStr = 'NonMono' |
---|
54 | PostFixStrAfter = 'NonMonoOccluMatched' |
---|
55 | % 1) Run occlusion detection and Matcheing for every pair of images |
---|
56 | |
---|
57 | ImgList = unique(PairList); |
---|
58 | NumImg = length(ImgList); |
---|
59 | |
---|
60 | for i = 1:NumImg |
---|
61 | for j = (i+1):NumImg |
---|
62 | |
---|
63 | % detect occlusion for a pair of image |
---|
64 | |
---|
65 | [ImgInfo1 ImgInfo2 Pair GlobalScale] = ... % What to do with Pairs haven't been matched ??????????????????? |
---|
66 | LoadDataForFindOcclu(defaultPara, Wrlname, ImgList{i}, ImgList{j}, ... |
---|
67 | 0, PostFixStr); % load all imformation needed for occlusion detection |
---|
68 | |
---|
69 | % Define variables |
---|
70 | H = 2274; |
---|
71 | V = 1704; |
---|
72 | Img1 = ImgInfo1.ExifInfo.IDName; |
---|
73 | Img2 = ImgInfo2.ExifInfo.IDName; |
---|
74 | I1=imreadbw([defaultPara.Fdir '/pgm/' Img1 '.pgm']); % function from sift |
---|
75 | I2=imreadbw([defaultPara.Fdir '/pgm/' Img2 '.pgm']); % function from sift |
---|
76 | ImgScale1 = size(I1); |
---|
77 | ImgScale2 = size(I2); |
---|
78 | |
---|
79 | % time consuming about 5mins |
---|
80 | if ~defaultPara.Flag.FlagPreloadOccluDetect |
---|
81 | [Region1 PointPix1 POriReprojM1 PoccluM1 OccluDist1 Region2 PointPix2 POriReprojM2 PoccluM2 OccluDist2] = ... |
---|
82 | FindOccluPair(defaultPara, ImgInfo1, ImgInfo2, Pair, GlobalScale, 1); % detect occlsion |
---|
83 | save([ defaultPara.Fdir '/data/' Img1 '_' Img2 '_OccluDetect.mat'],... |
---|
84 | 'Region1','PointPix1','POriReprojM1','PoccluM1','OccluDist1','Region2','PointPix2','POriReprojM2','PoccluM2','OccluDist2'); |
---|
85 | else |
---|
86 | load([defaultPara.Fdir '/data/' Img1 '_' Img2 '_OccluDetect.mat']); |
---|
87 | end |
---|
88 | |
---|
89 | % Define occlusion if OccluDist > CentainThreshold |
---|
90 | Mask1 = OccluDist1 > defaultPara.OccluDistThre; |
---|
91 | Mask2 = OccluDist2 > defaultPara.OccluDistThre; |
---|
92 | [Region1 PointPix1 POriReprojM1 PoccluM1 Region2 PointPix2 POriReprojM2 PoccluM2] = ... |
---|
93 | OccluPruning(Mask1, Mask2, Region1, PointPix1, POriReprojM1, PoccluM1, OccluDist1, ... |
---|
94 | Region2, PointPix2, POriReprojM2, PoccluM2, OccluDist2); |
---|
95 | |
---|
96 | |
---|
97 | % match occlusion part for a pair of image |
---|
98 | % Prepare the Constrain to run the matching |
---|
99 | [ Rc1 ConS1 ConSRough1] = EndPoint2BoxConS(defaultPara, H, V, POriReprojM1, PoccluM1); |
---|
100 | [ Rc2 ConS2 ConSRough2] = EndPoint2BoxConS(defaultPara, H, V, POriReprojM2, PoccluM2); |
---|
101 | Vector2Ipoint([Rc1; ConS1],[defaultPara.Fdir '/surf/'],['RConS_' Img1]); |
---|
102 | Vector2Ipoint([Rc2; ConS2],[defaultPara.Fdir '/surf/'],['RConS_' Img2]); |
---|
103 | Vector2Ipoint([ConSRough1],[defaultPara.Fdir '/surf/'],['RConSRough_' Img1]); |
---|
104 | Vector2Ipoint([ConSRough2],[defaultPara.Fdir '/surf/'],['RConSRough_' Img2]); |
---|
105 | tic; |
---|
106 | cd match |
---|
107 | % system(['./surfOccluMatch.sh ' defaultPara.Fdir ' ' Img1 ' ' Img2 ' OccluDense ' '0.1 0.2']); % Parameter still need to be changed//Min |
---|
108 | cd .. |
---|
109 | toc |
---|
110 | [f1, f2, matches] = readSurfMatches(Img1, Img2, defaultPara.Fdir, [ defaultPara.Type 'OccluDense'], 1, 1); |
---|
111 | figure(200); plotmatches(I1,I2,f1, f2,matches, 'Stacking','v','Interactive', 3); |
---|
112 | saveas(200,[ defaultPara.ScratchFolder Img1 '_' Img2 '_OccluMatches'],'jpg'); |
---|
113 | |
---|
114 | % Process the matches |
---|
115 | % Pruning by epipolarline |
---|
116 | [inlier] = EpipoPrune(defaultPara, Pair, [f1(:,matches(1,:)); f2(:,matches(2,:))], (ImgScale1+ImgScale2)/2); |
---|
117 | matches = matches(:,inlier); |
---|
118 | figure(201); plotmatches(I1,I2,f1, f2,matches, 'Stacking','v','Interactive', 3); |
---|
119 | saveas(201,[ defaultPara.ScratchFolder Img1 '_' Img2 '_OccluMatchesPrune'],'jpg'); |
---|
120 | |
---|
121 | |
---|
122 | % Triangulation |
---|
123 | if ~isempty(matches) |
---|
124 | tempf1 = f1(:,matches(1,:)); |
---|
125 | tempf2 = f2(:,matches(2,:)); |
---|
126 | x_calib = [ inv(defaultPara.InrinsicK1)*[ tempf1; ones(1,size(tempf1,2))];... |
---|
127 | inv(defaultPara.InrinsicK2)*[ tempf2; ones(1,size(tempf2,2))]]; |
---|
128 | [ lamda1 lamda2 Error] = triangulation( defaultPara, Pair.R, Pair.T, x_calib); |
---|
129 | % notice lamda re-scale to local model scale |
---|
130 | lamda1 = lamda1./GlobalScale(1); |
---|
131 | lamda2 = lamda2./GlobalScale(2); |
---|
132 | % Storage the match result for later ReInference |
---|
133 | AddMatch2Model(defaultPara, Wrlname, lamda1, f1(:,matches(1,:)), ImgInfo1, ImgScale1, i, j, PostFixStrAfter); |
---|
134 | AddMatch2Model(defaultPara, Wrlname, lamda2, f2(:,matches(2,:)), ImgInfo2, ImgScale2, i, j, PostFixStrAfter); |
---|
135 | end |
---|
136 | |
---|
137 | % storage the new Triangulated information |
---|
138 | end |
---|
139 | end |
---|
140 | |
---|
141 | % 2) ReInference of each image individiually |
---|
142 | |
---|
143 | for i = 1:NumImg |
---|
144 | |
---|
145 | % Data preparing and ReInfernece |
---|
146 | Default.Wrlname{1} = [Wrlname '_' ImgList{i} '_OccluMatches']; |
---|
147 | load([defaultPara.Fdir '/data/' ImgList{i} '/' Wrlname '_' ImgList{i} '_' PostFixStrAfter '.mat']); |
---|
148 | R = LoadModelStatus( defaultPara.Fdir, Wrlname, ImgList{i}, 'R'); |
---|
149 | T = LoadModelStatus( defaultPara.Fdir, Wrlname, ImgList{i}, 'T'); |
---|
150 | Scale = LoadModelStatus(defaultPara.Fdir, Wrlname, ImgList{i}, 'Scale'); |
---|
151 | SingleImgReInference(defaultPara, model, ImgList{i}, LoadModelStatus(defaultPara.Fdir, Wrlname, ImgList{i}, 'GroundLevel'),Wrlname, ... |
---|
152 | R, T, Scale ,PostFixStrAfter); |
---|
153 | |
---|
154 | % Build Meta Wrl file |
---|
155 | |
---|
156 | Path = [ defaultPara.OutPutFolder Wrlname 'Occlu.wrl']; |
---|
157 | InLinePath = ['./' ImgList{i} '/' Default.Wrlname{1} '.wrl']; |
---|
158 | % Possibly to be wrong ========== |
---|
159 | % Angle = recoverAlphasFromU(reshape(R,1,[])); |
---|
160 | % Q = GetQauternionFrom2Rotation(zeros(3,1), Angle, false);%[1 0 0 0]'; |
---|
161 | Q = Rotation2Q(NegI*R*NegI);% for Wrl (-) z component; |
---|
162 | if any(isnan(Q)) |
---|
163 | Q = zeros(4,1); |
---|
164 | end |
---|
165 | % =============================== |
---|
166 | WRLT = T; |
---|
167 | WRLT(3) = -WRLT(3);% for Wrl (-) z component; |
---|
168 | Scale = LoadModelStatus(defaultPara.Fdir, Wrlname, ImgList{i}, 'Scale'); |
---|
169 | if i == 1 |
---|
170 | BuildVrmlMetaModel(1, defaultPara.OutPutFolder, Path, InLinePath, Q, WRLT, repmat(Scale,3,1)); |
---|
171 | else |
---|
172 | BuildVrmlMetaModel(0, defaultPara.OutPutFolder, Path, InLinePath, Q, WRLT, repmat(Scale,3,1)); |
---|
173 | end |
---|
174 | end |
---|
175 | |
---|
176 | return; |
---|