Advertisement
dzu181

3D scan - Nightly build (test1.m)

Apr 8th, 2020
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.79 KB | None | 0 0
  1. close all
  2. clear all
  3.  
  4. I1 = imread('7.jpg');
  5. I2 = imread('8.jpg');
  6.  
  7. % Convert to grayscale.
  8. I1gray = rgb2gray(I1);
  9. I2gray = rgb2gray(I2);
  10.  
  11. % red/cyan composed
  12. %figure;
  13. %imshow(stereoAnaglyph(I1,I2));
  14. %title('Composite Image (Red - Left Image, Cyan - Right Image)');
  15.  
  16. % Hello twins!
  17. blobs1 = detectSURFFeatures(I1gray, 'MetricThreshold', 200);
  18. blobs2 = detectSURFFeatures(I2gray, 'MetricThreshold', 200);
  19.  
  20. [features1, validBlobs1] = extractFeatures(I1gray, blobs1);
  21. [features2, validBlobs2] = extractFeatures(I2gray, blobs2);
  22.  
  23. indexPairs = matchFeatures(features1, features2, 'Metric', 'SAD', ...
  24.   'MatchThreshold', 5);
  25.  
  26. matchedPoints1 = validBlobs1(indexPairs(:,1),:);
  27. matchedPoints2 = validBlobs2(indexPairs(:,2),:);
  28.  
  29. % Cac diem chung ban dau
  30. %figure;
  31. %showMatchedFeatures(I1, I2, matchedPoints1, matchedPoints2);
  32. %legend('Diem chung trong I1', 'Diem chung trong I2');
  33.  
  34. % Epipolar constraint
  35. [fMatrix, epipolarInliers, status] = estimateFundamentalMatrix(...
  36.   matchedPoints1, matchedPoints2, 'Method', 'RANSAC', ...
  37.   'NumTrials', 10000, 'DistanceThreshold', 0.1, 'Confidence', 99.99);
  38.  
  39. if status ~= 0 || isEpipoleInImage(fMatrix, size(I1)) ...
  40.   || isEpipoleInImage(fMatrix', size(I2))
  41.   error(['Either not enough matching points were found or '...
  42.          'the epipoles are inside the images. You may need to '...
  43.          'inspect and improve the quality of detected features ',...
  44.          'and/or improve the quality of your images.']);
  45. end
  46. % Cac diem chung chinh xac
  47. inlierPoints1 = matchedPoints1(epipolarInliers, :);
  48. inlierPoints2 = matchedPoints2(epipolarInliers, :);
  49.  
  50. figure;
  51. showMatchedFeatures(I1, I2, inlierPoints1, inlierPoints2);
  52. legend('Inlier points in I1', 'Inlier points in I2');
  53.  
  54. % Tim phep hieu chinh anh
  55. [t1, t2] = estimateUncalibratedRectification(fMatrix, ...
  56.   inlierPoints1.Location, inlierPoints2.Location, size(I2));
  57. tform1 = projective2d(t1);
  58. tform2 = projective2d(t2);
  59. % Hieu chinh anh
  60. [I1Rect, I2Rect] = rectifyStereoImages(I1, I2, tform1, tform2);
  61. figure;
  62. imshow(stereoAnaglyph(I1Rect, I2Rect));
  63. title('Rectified Stereo Images (Red - Left Image, Cyan - Right Image)');
  64.  
  65. % Disparity Map
  66. I1Rectgray = rgb2gray(I1Rect);
  67. I2Rectgray = rgb2gray(I2Rect);
  68. disparityMap = disparitySGM(I1Rectgray, I2Rectgray);
  69. figure;
  70. imshow(disparityMap, [0, 128]);
  71. title('Disparity Map');
  72. colormap jet
  73. colorbar
  74.  
  75. % Doan nay can thong tin tu vi tri, goc xoay camera
  76. %points3D = reconstructScene(disparityMap, stereoParams);
  77.  
  78. % Convert to meters and create a pointCloud object
  79. %points3D = points3D ./ 1000;
  80. %ptCloud = pointCloud(points3D, 'Color', frameLeftRect);
  81.  
  82. % Create a streaming point cloud viewer
  83. %player3D = pcplayer([-3, 3], [-3, 3], [0, 8], 'VerticalAxis', 'y', ...
  84. %    'VerticalAxisDir', 'down');
  85.  
  86. % Visualize the point cloud
  87. %view(player3D, ptCloud);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement