Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. % =========================================================================
  2. % Exercise 8
  3. % =========================================================================
  4.  
  5. clc;
  6. close all;
  7. clear all;
  8.  
  9. % Initialize VLFeat (http://www.vlfeat.org/)
  10.  
  11. %K Matrix for house images (approx.)
  12. K = [ 670.0000 0 393.000
  13. 0 670.0000 275.000
  14. 0 0 1];
  15.  
  16. %Load images
  17. imgName1 = '../data/house.000.pgm';
  18. imgName2 = '../data/house.004.pgm';
  19.  
  20. img1 = single(imread(imgName1));
  21. img2 = single(imread(imgName2));
  22.  
  23. %extract SIFT features and match
  24. [fa, da] = vl_sift(img1);
  25. [fb, db] = vl_sift(img2);
  26.  
  27. %don't take features at the top of the image - only background
  28. filter = fa(2,:) > 100;
  29. fa = fa(:,find(filter));
  30. da = da(:,find(filter));
  31.  
  32. [matches_1, scores] = vl_ubcmatch(da, db);
  33.  
  34. showFeatureMatches(img1, fa(1:2, matches_1(1,:)), img2, fb(1:2, matches_1(2,:)), 20);
  35.  
  36. % the initial set of points used in fundamental matrix estimation
  37. x1s = makehomogeneous(fa(1:2, matches_1(1,:)));
  38. x2s = makehomogeneous(fb(1:2, matches_1(2,:)));
  39. %% Compute essential matrix and projection matrices and triangulate matched points
  40.  
  41. %use 8-point ransac or 5-point ransac - compute (you can also optimize it to get best possible results)
  42. %and decompose the essential matrix and create the projection matrices
  43.  
  44. Ps{1} = eye(4);
  45. [E, inliers_1] = ransacfitfundmatrix(x1s, x2s, 0.001);
  46.  
  47. % keep descriptors for the features used in triangularion
  48. da_inliers = da(:, inliers_1);
  49. db_inliers = db(:, inliers_1);
  50.  
  51. x1s_in = makehomogeneous(x1s(1:2, inliers_1));
  52. x2s_in = makehomogeneous(x2s(1:2, inliers_1));
  53.  
  54. x1_calibrated = inv(K) * x1s_in;
  55. x2_calibrated = inv(K) * x2s_in;
  56. showFeatureMatches(img1, x1s_in(1:2, :), img2, x2s_in(1:2, :), 30);
  57.  
  58. Ps{2} = decomposeE(E, x1_calibrated, x2_calibrated);
  59.  
  60. %triangulate the inlier matches with the computed projection matrix
  61. [XS_12, err] = linearTriangulation(Ps{1}, x1_calibrated, Ps{2}, x2_calibrated);
  62. err
  63.  
  64. %% Add an addtional view of the scene
  65.  
  66. imgName3 = '../data/house.001.pgm';
  67. img3 = single(imread(imgName3));
  68. [fc, dc] = vl_sift(img3);
  69.  
  70. [matches_3, scores] = vl_ubcmatch(da_inliers, dc);
  71. %match against the features from image 1 that where triangulated
  72.  
  73. %find the 2D-3D corresponding matched point pairs
  74. XS_matched = XS_12(:, matches_3(1, :));
  75. x1_calibrated_3 = x1_calibrated(:, matches_3(1, :));
  76.  
  77. x3s = makehomogeneous(fc(1:2, matches_3(2,:)));
  78. x3_calibrated = inv(K) * x3s;
  79.  
  80. %run 6-point ransac
  81. [Ps{3}, inliers_3] = ransacfitprojmatrix(x3_calibrated, XS_matched, 0.05);
  82. if (det(Ps{3}) < 0)
  83. Ps{3}(1:3,1:3) = -Ps{3}(1:3,1:3);
  84. Ps{3}(1:3, 4) = -Ps{3}(1:3, 4);
  85. end
  86.  
  87. x1_calibrated_3_in = x1_calibrated_3(:, inliers_3);
  88. x3_calibrated_in = x3_calibrated(:, inliers_3);
  89.  
  90. %triangulate the inlier matches with the computed projection matrix
  91. [XS_13, err] = linearTriangulation(Ps{2}, x1_calibrated_3_in, Ps{3}, x3_calibrated);
  92. err
  93.  
  94. x1_3 = K * x1_calibrated_3_in;
  95. x3 = K * x3_calibrated_in;
  96.  
  97. showFeatureMatches(img2, x1_3(1:2, :), img3, x3(1:2, :), 40);
  98.  
  99. %% Add more views...
  100.  
  101. imgName4 = '../data/house.002.pgm';
  102. img4 = single(imread(imgName4));
  103.  
  104. [fd, dd] = extractSIFT(img4);
  105. [matches_4, scores] = vl_ubcmatch(da_inliers, dd);
  106. %match against the features from image 1 that where triangulated
  107.  
  108. XS_matched = XS_12(:, matches_4(1, :));
  109. x1s_calibrated_4 = x1_calibrated(:, matches_4(1, :));
  110. x4s = makehomogeneous(fd(1:2, matches_4(2,:)));
  111. x4_calibrated = inv(K) * x4s;
  112.  
  113.  
  114. %run 6-point ransac
  115. [Ps{4}, inliers_4] = ransacfitprojmatrix(x4_calibrated, XS_matched, 0.05);
  116.  
  117. if (det(Ps{4}) < 0)
  118. Ps{4}(1:3,1:3) = -Ps{4}(1:3,1:3);
  119. Ps{4}(1:3, 4) = -Ps{4}(1:3, 4);
  120. end
  121.  
  122. x1_calibrated_4 = x1s_calibrated_4(:, inliers_4);
  123. x4_calibrated = x4_calibrated(:, inliers_4);
  124.  
  125. %triangulate the inlier matches with the computed projection matrix
  126. [XS_14, err] = linearTriangulation(Ps{1}, x1_calibrated_4, Ps{4}, x4_calibrated);
  127.  
  128. x1_4 = (K * x1_calibrated_4);
  129. x4 = (K * x4_calibrated);
  130. showFeatureMatches(img1, x1_4(1:2, :), img4, x4(1:2, :), 50);
  131.  
  132. %% Plot stuff
  133. close all
  134. fig = 10;
  135. figure(fig);
  136.  
  137. %use plot3 to plot the triangulated 3D points
  138. XS_12_ = makeinhomogeneous(XS_12);
  139. XS_13_ = makeinhomogeneous(XS_13);
  140. XS_14_ = makeinhomogeneous(XS_14);
  141. plot3(XS_12_(1, :), XS_12_(2, :), XS_12_(3, :), "*r");
  142. hold on;
  143. plot3(XS_13_(1, :), XS_13_(2, :), XS_13_(3, :), "*b");
  144. hold on;
  145. plot3(XS_14_(1, :), XS_14_(2, :), XS_14_(3, :), "*g");
  146. % draw cameras
  147. drawCameras(Ps, fig);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement