source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/image3dstiching/Inference/PairReInference.m @ 37

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

Added original make3d

File size: 10.8 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 [defaultPara ImgInfo GlabalInfo] = PairReInference(defaultPara, Pair, ImgInfo, FlagFirstPair)
40
41% This function is the meta function of 2 decompose inference
42% Input:
43%       defaultPara - camera intrinsic parameters
44%       Pair - Pair image info- matches camera extrinsic
45%       ImgInfo - Sup, Ray, MultiScaleSup, SupNeighbor, Depth, Constrain (previous)
46% Return:
47%       ImgInfo - add new Constrain
48% initialize parameter
49NumMatches = length(Pair.Xim);
50Img1 = ImgInfo(1).ExifInfo.IDName;
51Img2 = ImgInfo(2).ExifInfo.IDName;
52I1=imreadbw([defaultPara.Fdir '/pgm/' Img1 '.pgm']); % function from sift
53I2=imreadbw([defaultPara.Fdir '/pgm/' Img2 '.pgm']); % function from sift
54
55% ===================== Rescale Depth and T prorperly ======
56if FlagFirstPair
57        UniScale = defaultPara.Scale;
58else
59        UniScale = LoadModelStatus(defaultPara.Fdir, defaultPara.Wrlname, Img1, 'Scale');
60end
61ImgInfo(1).Model.Depth.FitDepth = ImgInfo(1).Model.Depth.FitDepth*UniScale;
62ImgInfo(1).Model.Depth.RawDepth = ImgInfo(1).Model.Depth.RawDepth*UniScale;
63ImgInfo(2).Model.Depth.FitDepth = ImgInfo(2).Model.Depth.FitDepth*Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale;
64ImgInfo(2).Model.Depth.RawDepth = ImgInfo(2).Model.Depth.RawDepth*Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale;
65Pair.T = Pair.T/Pair.DepthScale(1)*UniScale;
66Scale = Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale;
67
68% ==========================================================
69% ConStrain generation
70% 1) Triangulated Depth
71x_calib = [ inv(defaultPara.InrinsicK1)*[ Pair.Xim(1:2,:); ones(1, NumMatches)];...
72              inv(defaultPara.InrinsicK2)*[ Pair.Xim(3:4,:); ones(1, NumMatches)]];
73[ TriDepth1 TriDepth2] = triangulation( defaultPara, Pair.R, Pair.T, x_calib);
74%TriDepth1 = Pair.lamda(1);
75%TriDepth2 = Pair.lamda(2);
76RayNormfactor1 = sqrt( sum(x_calib(1:3,:).^2,1));
77RayNormfactor2 = sqrt( sum(x_calib(4:6,:).^2,1));
78if FlagFirstPair
79        % 2) Ground orientaion and level
80        [GroundLevel] = CalGroundLevel(defaultPara, ImgInfo, Pair);
81else
82    GroundLevel = LoadModelStatus(defaultPara.Fdir, defaultPara.Wrlname, Img1, 'GroundLevel');
83end   
84
85% ImgInfo(1) ReInference
86%[ImgInfo(1)] = ReInference(defaultPara, Pair.R', -R'*Pair.T, Pair.Xim(1:4, :), ImgInfo([1 2]), [ TriDepth1 TriDepth2], GroundLevel, eye(3), 1);
87% ImgInfo(2) ReInference
88%[ImgInfo(2)] = ReInference(defaultPara, Pair.R, Pair.T, Pair.Xim([3 4 1 2], :), ImgInfo([2 1]), [ TriDepth2 TriDepth1], GroundLevel, eye(3), FlagRenderingAll);
89
90% ================================old method
91[D1 IND1] = PorjPosi2Depth(size(I1), size(ImgInfo(1).Model.Depth.FitDepth), Pair.Xim(1:2,:), ImgInfo(1).Model.Depth.FitDepth);
92[D2 IND2] = PorjPosi2Depth(size(I2), size(ImgInfo(2).Model.Depth.FitDepth), Pair.Xim(3:4,:), ImgInfo(1).Model.Depth.FitDepth);
93clear I1 I2;
94    Default.OutPutFolder = defaultPara.OutPutFolder;
95    Default.ScratchFolder = defaultPara.ScratchFolder;
96    Default.Wrlname{1} = defaultPara.Wrlname;
97    Default.Flag.AfterInferenceStorage = 0;
98Default.RenderFlag =  defaultPara.RenderFlag;
99[Default.VertYNuDepth Default.HoriXNuDepth] = size(ImgInfo(1).Model.Depth.FitDepth);
100Default.fy = 2400.2091651084;
101Default.fx = 2407.3312729885838;
102Default.Ox = 1110.7122391785729;%2272/2; %
103Default.Oy = 833.72104535435108;%1704/2; %
104Default.a_default = 2272/Default.fx; %0.70783777; %0.129; % horizontal physical size of image plane normalized to focal length (in meter)
105Default.b_default = 1704/Default.fy; %0.946584169;%0.085; % vertical physical size of image plane normalized to focal length (in meter)
106Default.Ox_default = 1-Default.Ox/2272;%0.489272914; % camera origin offset from the image center in horizontal direction
107Default.Oy_default = 1-Default.Oy/1704;%0.488886982; % camera origin offset from the image center in vertical direction
108Default.MinTriEffectPercent = 5;
109Default.FarestTriDist = 0.1;
110Default.TriCountSupThre = 10;
111
112    AappendOpt = ~FlagFirstPair;
113    ASupMatched = ImgInfo(1).Model.Sup(IND1)';
114    mask = ASupMatched == 0;
115    ASupMatched(mask)=[];
116    ARayMatched = (x_calib(1:3,:)./(repmat( RayNormfactor1,3,1)))';
117    ARayMatched(mask,:) = [];
118ADepth_modified = TriDepth1.*RayNormfactor1;   
119    ADepth_modified(:,mask) = [];
120if FlagFirstPair
121        ARotation = defaultPara.R;
122        ATranslation = defaultPara.T;
123else
124        ARotation = LoadModelStatus( defaultPara.Fdir, defaultPara.Wrlname, Img1, 'R');
125        ATranslation = LoadModelStatus( defaultPara.Fdir, defaultPara.Wrlname, Img1, 'T');
126end
127Aconstrain.RayMatched = ImgInfo(1).Model.Constrain.RayMatche;
128Aconstrain.Depth_modified = ImgInfo(1).Model.Constrain.Depth_modified;
129Aconstrain.SupMatched = ImgInfo(1).Model.Constrain.SupMatched;
130ASup = ImgInfo(1).Model.Sup;
131ASupOri = ASup;
132AdepthMap = ImgInfo(1).Model.Depth.RawDepth; % or RawDepth
133ARayOri = ImgInfo(1).Model.Ray;
134ARayAll = ARayOri;
135ASupNeighborTable = ImgInfo(1).Model.SupNeighborTable;
136AmaskSky = ImgInfo(1).Model.maskSky;
137AmaskG = ImgInfo(1).Model.maskG;
138AMultiScaleSupTable = ImgInfo(1).Model.MultiScaleSupTable;
139
140    Default.filename{1} = strrep(ImgInfo(1).ExifInfo.name, '.jpg','');
141if true
142     [ ImgInfo(1).Model.PlaneParaInfo] = PlaneParaMRFTriangulateOneShot( Default, ARotation, ATranslation, AappendOpt, ...
143                           [ ARayMatched; Aconstrain.RayMatched],...
144                           [ ADepth_modified Aconstrain.Depth_modified]',...
145                           [ ASupMatched; Aconstrain.SupMatched],...
146                           [ ], [ ], [ ], [],...
147                           ASup, ASupOri, [], AdepthMap, zeros(size(AdepthMap)), ARayOri, ARayAll, ...
148                           ASupNeighborTable, [], AmaskSky, AmaskG,...
149                           'cvx_allL1Norm',1,...
150                           [], [], AMultiScaleSupTable, [], [], [], false, 0, ARotation, GroundLevel);
151end
152ImgInfo(1).Model.Constrain.RayMatche = [ ARayMatched; Aconstrain.RayMatched];
153ImgInfo(1).Model.Constrain.Depth_modified = [ ADepth_modified Aconstrain.Depth_modified];
154ImgInfo(1).Model.Constrain.SupMatched = [ ASupMatched; Aconstrain.SupMatched];
155model = ImgInfo(1).Model;
156% model.PlaneParaInfo = ImgInfo(1).PlaneParaInfo;
157ImgName = strrep(ImgInfo(1).ExifInfo.name,'.jpg','');
158save( [defaultPara.ScratchFolder ImgName '/' ImgName '_NonMono.mat'], 'model');
159GlabalInfo(2).GroundLevel = GroundLevel;
160GlabalInfo(2).ARotation = ARotation;
161GlabalInfo(2).ATranslation = ATranslation;
162GlabalInfo(2).Scale = UniScale;
163
164% ===================================
165Default.RenderFlag =  defaultPara.LastImgFlag;
166    AappendOpt = 1;
167    ASupMatched = ImgInfo(2).Model.Sup(IND2)';
168    mask = ASupMatched == 0;
169    ASupMatched(mask)=[];
170    ARayMatched = (x_calib(4:6,:)./(repmat( RayNormfactor2,3,1)))';
171    ARayMatched(mask,:) = [];
172ADepth_modified = TriDepth2.*RayNormfactor2;   
173    ADepth_modified(:,mask) = [];
174ARotation1 = ARotation; % Bad method to avoid override
175ATranslation1 = ATranslation; % Bad method to avoid override
176ARotation = ARotation1*Pair.R';
177ATranslation = ATranslation1 + ARotation1*(-Pair.R'*Pair.T);
178Aconstrain.RayMatched = ImgInfo(2).Model.Constrain.RayMatche;
179Aconstrain.Depth_modified = ImgInfo(2).Model.Constrain.Depth_modified;
180Aconstrain.SupMatched = ImgInfo(2).Model.Constrain.SupMatched;
181ASup = ImgInfo(2).Model.Sup;
182ASupOri = ASup;
183AdepthMap = ImgInfo(2).Model.Depth.RawDepth; % or RawDepth
184ARayOri = ImgInfo(2).Model.Ray;
185ARayAll = ARayOri;
186ASupNeighborTable = ImgInfo(2).Model.SupNeighborTable;
187AmaskSky = ImgInfo(2).Model.maskSky;
188AmaskG = ImgInfo(2).Model.maskG;
189AMultiScaleSupTable = ImgInfo(2).Model.MultiScaleSupTable;
190
191    Default.filename{1} = strrep(ImgInfo(2).ExifInfo.name, '.jpg','');
192if Default.RenderFlag
193     [ ImgInfo(2).Model.PlaneParaInfo] = PlaneParaMRFTriangulateOneShot( Default, ARotation, ATranslation, AappendOpt, ...
194                           [ ARayMatched; Aconstrain.RayMatched],...
195                           [ ADepth_modified Aconstrain.Depth_modified]',...
196                           [ ASupMatched; Aconstrain.SupMatched],...
197                           [ ], [ ], [ ], [],...
198                           ASup, ASupOri, [], AdepthMap, zeros(size(AdepthMap)), ARayOri, ARayAll, ...
199                           ASupNeighborTable, [], AmaskSky, AmaskG,...
200                           'cvx_allL1Norm',1,...
201                           [], [], AMultiScaleSupTable, [], [], [], false, 0, ARotation, GroundLevel);
202end
203
204ImgInfo(2).Model.Constrain.RayMatche = [ ARayMatched; Aconstrain.RayMatched];
205ImgInfo(2).Model.Constrain.Depth_modified = [ ADepth_modified Aconstrain.Depth_modified];
206ImgInfo(2).Model.Constrain.SupMatched = [ ASupMatched; Aconstrain.SupMatched];
207model = ImgInfo(2).Model;
208% model.PlaneParaInfo = ImgInfo(2).PlaneParaInfo;
209ImgName = strrep(ImgInfo(2).ExifInfo.name,'.jpg','');
210save( [defaultPara.ScratchFolder ImgName '/' ImgName '_NonMono.mat'], 'model');
211
212% modify defaultPara
213%defaultPara.GroundLevel = GroundLevel;
214%defaultPara.R = ARotation;%defaultPara.R*Pair.R';
215%defaultPara.T = ATranslation;%defaultPara.T + defaultPara.R*(-Pair.R'*Pair.T);
216%defaultPara.Scale = Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale;
217SaveModelSatus( defaultPara.Fdir, defaultPara.Wrlname, Img2, GroundLevel, ARotation, ATranslation, Scale, FlagFirstPair)
218GlabalInfo(2).GroundLevel = GroundLevel;
219GlabalInfo(2).ARotation = ARotation;
220GlabalInfo(2).ATranslation = ATranslation;
221GlabalInfo(2).Scale = Scale;
222
223return;
224
Note: See TracBrowser for help on using the repository browser.