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

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

Added original make3d

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