source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/image3dstiching/TestVersion/ProjectionFactorization.m @ 37

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

Added original make3d

File size: 5.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 [ P, X, Outliner] = ProjectionFactorization( ARes, BRes, x_im, depth)
40% This funciton given the match in image coordinate and the initial depth
41% and estimate the Camera matrix and the 3d position of the match points
42
43% setup parameters
44Min_prjective_depth_change_ratio = 1e-6;
45ProjErrorRatio = 0.0005;
46No_Camera = size(depth,3);
47No_Points = size(x_im,2)
48MaxCount = 100;
49TotalOutlinerPercentage = 50;
50IterOutlinerPercentage = TotalOutlinerPercentage/MaxCount;
51
52% building matrix
53lamda = [];
54Xim = [];
55for i = 1:No_Camera
56    lamda = [ lamda; repmat(depth(:,:,i), 3, 1)];
57    Xim = [ Xim; [x_im(:,:,i)]];
58end
59count = 1
60Outliner = [];
61while count < MaxCount
62   
63    % normalized the depth and Xim
64    RowNormalizeFactor = 1./( sqrt( sum(lamda.^2,2)));
65    lamdaNormal = lamda.*repmat( RowNormalizeFactor, 1, No_Points);
66    ColumnNormalizeFactor = sqrt(3)./( sqrt( sum(lamdaNormal.^2,1)));
67    lamdaNormal = lamdaNormal.*repmat( ColumnNormalizeFactor,No_Camera*3,1);
68 
69    % Solving SVD
70    [ U S V] = svds(lamdaNormal.*Xim,4);
71    P = U*S;
72    X = V';
73
74    % new lamda
75    M = P*X;
76    M = M./repmat( RowNormalizeFactor, 1, No_Points);
77    M = M./repmat( ColumnNormalizeFactor,No_Camera*3,1);
78    ThirdSample = (1:No_Camera)*3;
79    TempLamda = M( ThirdSample, :);
80 
81    % calculate the geometric differency of measured and estimated projection points on the image plane
82    NewLamda = TempLamda( reshape( repmat( 1:No_Camera, 3, 1), [], 1),:);
83    x_im_est = M./NewLamda;
84    xIm = Xim( reshape( [ ThirdSample-2; ThirdSample-1], [], 1), :);
85    xImEst = x_im_est( reshape( [ ThirdSample-2; ThirdSample-1], [], 1), :);
86    DiffProj =  xIm - xImEst;
87    EclideanError = norms( reshape(DiffProj, 2, []));
88    InlinerUpperBound = prctile( EclideanError, 100 - IterOutlinerPercentage);
89    OutlinerPtr = EclideanError > InlinerUpperBound;
90    OutlinerPtr = reshape( OutlinerPtr, 2, []);
91    OutlinerPtr = sum( OutlinerPtr,1);
92    figure(15);
93    hist( EclideanError,100);
94    AAvgDiffProj(count) = mean(norms(DiffProj(1:2,:)));
95    BAvgDiffProj(count) = mean(norms(DiffProj(3:4,:)));
96    if AAvgDiffProj(count) < norm( ARes*ProjErrorRatio) && BAvgDiffProj(count) < norm( BRes*ProjErrorRatio)
97       break;
98    end   
99   
100    % Check stop
101    Ratio(count) = max( max( abs((NewLamda( ThirdSample, :) - lamda( ThirdSample, :))./lamda( ThirdSample, :))));
102    if Ratio(count) < Min_prjective_depth_change_ratio
103       break;
104    end
105    count = count +1;
106    lamda = NewLamda;
107   
108    % =============remove Outliner =============
109    Outliner = [ Outliner find(OutlinerPtr~=0)];
110    lamda(:, OutlinerPtr~=0) = [];
111    Xim(:, OutlinerPtr~=0) = [];
112    No_Points = size( Xim,2);
113    % ==========================================
114end
115No_Points
116count
117AAvgDiffProj(count-1)
118BAvgDiffProj(count-1)
119figure(100); hist( DiffProj(:), 1000);
120figure(10); scatter(xImEst(1,:),xImEst(2,:),4,ones(1,size(xImEst,2)));
121hold on; scatter(xIm(1,:),xIm(2,:),4,0.5*ones(1,size(xIm,2)));hold off;
122figure(11); scatter(xImEst(3,:),xImEst(4,:),4,ones(1,size(xImEst,2)));
123hold on; scatter(xIm(3,:),xIm(4,:),4,0.5*ones(1,size(xIm,2)));hold off;
124
125% restorge P X
126 P = P./repmat( RowNormalizeFactor, 1, 4);
127 X = X./repmat( ColumnNormalizeFactor, 4, 1);
128%  X = X./repmat(X(4,:), 4,1);
129
130return;
Note: See TracBrowser for help on using the repository browser.