[37] | 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 [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 |
---|
| 49 | NumMatches = length(Pair.Xim); |
---|
| 50 | Img1 = ImgInfo(1).ExifInfo.IDName; |
---|
| 51 | Img2 = ImgInfo(2).ExifInfo.IDName; |
---|
| 52 | I1=imreadbw([defaultPara.Fdir '/pgm/' Img1 '.pgm']); % function from sift |
---|
| 53 | I2=imreadbw([defaultPara.Fdir '/pgm/' Img2 '.pgm']); % function from sift |
---|
| 54 | |
---|
| 55 | % ===================== Rescale Depth and T prorperly ====== |
---|
| 56 | if FlagFirstPair |
---|
| 57 | UniScale = defaultPara.Scale; |
---|
| 58 | else |
---|
| 59 | UniScale = LoadModelStatus(defaultPara.Fdir, defaultPara.Wrlname, Img1, 'Scale'); |
---|
| 60 | end |
---|
| 61 | ImgInfo(1).Model.Depth.FitDepth = ImgInfo(1).Model.Depth.FitDepth*UniScale; |
---|
| 62 | ImgInfo(1).Model.Depth.RawDepth = ImgInfo(1).Model.Depth.RawDepth*UniScale; |
---|
| 63 | ImgInfo(2).Model.Depth.FitDepth = ImgInfo(2).Model.Depth.FitDepth*Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale; |
---|
| 64 | ImgInfo(2).Model.Depth.RawDepth = ImgInfo(2).Model.Depth.RawDepth*Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale; |
---|
| 65 | Pair.T = Pair.T/Pair.DepthScale(1)*UniScale; |
---|
| 66 | Scale = Pair.DepthScale(2)/Pair.DepthScale(1)*UniScale; |
---|
| 67 | |
---|
| 68 | % ========================================================== |
---|
| 69 | % ConStrain generation |
---|
| 70 | % 1) Triangulated Depth |
---|
| 71 | x_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); |
---|
| 76 | RayNormfactor1 = sqrt( sum(x_calib(1:3,:).^2,1)); |
---|
| 77 | RayNormfactor2 = sqrt( sum(x_calib(4:6,:).^2,1)); |
---|
| 78 | if FlagFirstPair |
---|
| 79 | % 2) Ground orientaion and level |
---|
| 80 | [GroundLevel] = CalGroundLevel(defaultPara, ImgInfo, Pair); |
---|
| 81 | else |
---|
| 82 | GroundLevel = LoadModelStatus(defaultPara.Fdir, defaultPara.Wrlname, Img1, 'GroundLevel'); |
---|
| 83 | end |
---|
| 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); |
---|
| 93 | clear I1 I2; |
---|
| 94 | Default.OutPutFolder = defaultPara.OutPutFolder; |
---|
| 95 | Default.ScratchFolder = defaultPara.ScratchFolder; |
---|
| 96 | Default.Wrlname{1} = defaultPara.Wrlname; |
---|
| 97 | Default.Flag.AfterInferenceStorage = 0; |
---|
| 98 | Default.RenderFlag = defaultPara.RenderFlag; |
---|
| 99 | [Default.VertYNuDepth Default.HoriXNuDepth] = size(ImgInfo(1).Model.Depth.FitDepth); |
---|
| 100 | Default.fy = 2400.2091651084; |
---|
| 101 | Default.fx = 2407.3312729885838; |
---|
| 102 | Default.Ox = 1110.7122391785729;%2272/2; % |
---|
| 103 | Default.Oy = 833.72104535435108;%1704/2; % |
---|
| 104 | Default.a_default = 2272/Default.fx; %0.70783777; %0.129; % horizontal physical size of image plane normalized to focal length (in meter) |
---|
| 105 | Default.b_default = 1704/Default.fy; %0.946584169;%0.085; % vertical physical size of image plane normalized to focal length (in meter) |
---|
| 106 | Default.Ox_default = 1-Default.Ox/2272;%0.489272914; % camera origin offset from the image center in horizontal direction |
---|
| 107 | Default.Oy_default = 1-Default.Oy/1704;%0.488886982; % camera origin offset from the image center in vertical direction |
---|
| 108 | Default.MinTriEffectPercent = 5; |
---|
| 109 | Default.FarestTriDist = 0.1; |
---|
| 110 | Default.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,:) = []; |
---|
| 118 | ADepth_modified = TriDepth1.*RayNormfactor1; |
---|
| 119 | ADepth_modified(:,mask) = []; |
---|
| 120 | if FlagFirstPair |
---|
| 121 | ARotation = defaultPara.R; |
---|
| 122 | ATranslation = defaultPara.T; |
---|
| 123 | else |
---|
| 124 | ARotation = LoadModelStatus( defaultPara.Fdir, defaultPara.Wrlname, Img1, 'R'); |
---|
| 125 | ATranslation = LoadModelStatus( defaultPara.Fdir, defaultPara.Wrlname, Img1, 'T'); |
---|
| 126 | end |
---|
| 127 | Aconstrain.RayMatched = ImgInfo(1).Model.Constrain.RayMatche; |
---|
| 128 | Aconstrain.Depth_modified = ImgInfo(1).Model.Constrain.Depth_modified; |
---|
| 129 | Aconstrain.SupMatched = ImgInfo(1).Model.Constrain.SupMatched; |
---|
| 130 | ASup = ImgInfo(1).Model.Sup; |
---|
| 131 | ASupOri = ASup; |
---|
| 132 | AdepthMap = ImgInfo(1).Model.Depth.RawDepth; % or RawDepth |
---|
| 133 | ARayOri = ImgInfo(1).Model.Ray; |
---|
| 134 | ARayAll = ARayOri; |
---|
| 135 | ASupNeighborTable = ImgInfo(1).Model.SupNeighborTable; |
---|
| 136 | AmaskSky = ImgInfo(1).Model.maskSky; |
---|
| 137 | AmaskG = ImgInfo(1).Model.maskG; |
---|
| 138 | AMultiScaleSupTable = ImgInfo(1).Model.MultiScaleSupTable; |
---|
| 139 | |
---|
| 140 | Default.filename{1} = strrep(ImgInfo(1).ExifInfo.name, '.jpg',''); |
---|
| 141 | if 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); |
---|
| 151 | end |
---|
| 152 | ImgInfo(1).Model.Constrain.RayMatche = [ ARayMatched; Aconstrain.RayMatched]; |
---|
| 153 | ImgInfo(1).Model.Constrain.Depth_modified = [ ADepth_modified Aconstrain.Depth_modified]; |
---|
| 154 | ImgInfo(1).Model.Constrain.SupMatched = [ ASupMatched; Aconstrain.SupMatched]; |
---|
| 155 | model = ImgInfo(1).Model; |
---|
| 156 | % model.PlaneParaInfo = ImgInfo(1).PlaneParaInfo; |
---|
| 157 | ImgName = strrep(ImgInfo(1).ExifInfo.name,'.jpg',''); |
---|
| 158 | save( [defaultPara.ScratchFolder ImgName '/' ImgName '_NonMono.mat'], 'model'); |
---|
| 159 | GlabalInfo(2).GroundLevel = GroundLevel; |
---|
| 160 | GlabalInfo(2).ARotation = ARotation; |
---|
| 161 | GlabalInfo(2).ATranslation = ATranslation; |
---|
| 162 | GlabalInfo(2).Scale = UniScale; |
---|
| 163 | |
---|
| 164 | % =================================== |
---|
| 165 | Default.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,:) = []; |
---|
| 172 | ADepth_modified = TriDepth2.*RayNormfactor2; |
---|
| 173 | ADepth_modified(:,mask) = []; |
---|
| 174 | ARotation1 = ARotation; % Bad method to avoid override |
---|
| 175 | ATranslation1 = ATranslation; % Bad method to avoid override |
---|
| 176 | ARotation = ARotation1*Pair.R'; |
---|
| 177 | ATranslation = ATranslation1 + ARotation1*(-Pair.R'*Pair.T); |
---|
| 178 | Aconstrain.RayMatched = ImgInfo(2).Model.Constrain.RayMatche; |
---|
| 179 | Aconstrain.Depth_modified = ImgInfo(2).Model.Constrain.Depth_modified; |
---|
| 180 | Aconstrain.SupMatched = ImgInfo(2).Model.Constrain.SupMatched; |
---|
| 181 | ASup = ImgInfo(2).Model.Sup; |
---|
| 182 | ASupOri = ASup; |
---|
| 183 | AdepthMap = ImgInfo(2).Model.Depth.RawDepth; % or RawDepth |
---|
| 184 | ARayOri = ImgInfo(2).Model.Ray; |
---|
| 185 | ARayAll = ARayOri; |
---|
| 186 | ASupNeighborTable = ImgInfo(2).Model.SupNeighborTable; |
---|
| 187 | AmaskSky = ImgInfo(2).Model.maskSky; |
---|
| 188 | AmaskG = ImgInfo(2).Model.maskG; |
---|
| 189 | AMultiScaleSupTable = ImgInfo(2).Model.MultiScaleSupTable; |
---|
| 190 | |
---|
| 191 | Default.filename{1} = strrep(ImgInfo(2).ExifInfo.name, '.jpg',''); |
---|
| 192 | if 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); |
---|
| 202 | end |
---|
| 203 | |
---|
| 204 | ImgInfo(2).Model.Constrain.RayMatche = [ ARayMatched; Aconstrain.RayMatched]; |
---|
| 205 | ImgInfo(2).Model.Constrain.Depth_modified = [ ADepth_modified Aconstrain.Depth_modified]; |
---|
| 206 | ImgInfo(2).Model.Constrain.SupMatched = [ ASupMatched; Aconstrain.SupMatched]; |
---|
| 207 | model = ImgInfo(2).Model; |
---|
| 208 | % model.PlaneParaInfo = ImgInfo(2).PlaneParaInfo; |
---|
| 209 | ImgName = strrep(ImgInfo(2).ExifInfo.name,'.jpg',''); |
---|
| 210 | save( [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; |
---|
| 217 | SaveModelSatus( defaultPara.Fdir, defaultPara.Wrlname, Img2, GroundLevel, ARotation, ATranslation, Scale, FlagFirstPair) |
---|
| 218 | GlabalInfo(2).GroundLevel = GroundLevel; |
---|
| 219 | GlabalInfo(2).ARotation = ARotation; |
---|
| 220 | GlabalInfo(2).ATranslation = ATranslation; |
---|
| 221 | GlabalInfo(2).Scale = Scale; |
---|
| 222 | |
---|
| 223 | return; |
---|
| 224 | |
---|