Advertisement
Guest User

Untitled

a guest
May 29th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.38 KB | None | 0 0
  1.  
  2.  
  3. %% Wczytanie obrazów do przestrzeni roboczej
  4. I1 = imread('C:\Users\Piotr\Documents\MATLAB\Image47.png');
  5. I2 = imread('C:\Users\Piotr\Documents\MATLAB\Image48.png');
  6. imshowpair(I1, I2, 'montage');
  7. title('Para orginalnych obrazów');
  8.  
  9. %% Wczytanie parametrów kamery  
  10. load moja_kamera.mat
  11. cameraParams = parametryKamery;
  12.  
  13. %% Usunięcie zniekształceń z obrazów
  14. [I1, newOrigin1] = undistortImage(I1, cameraParams, 'OutputView', 'full');
  15. [I2, newOrigin2] = undistortImage(I2, cameraParams, 'OutputView', 'full');
  16. figure;
  17. imshowpair(I1, I2, 'montage');
  18. title('Obrazy po usunięciu zniekształceń');
  19.  
  20.  
  21. %% Obliczenie macierzy parametrów zewnętrznych i macierzy kamery dla obrazów
  22. squareSize = 23;
  23. [refPoints1, boardSize] = detectCheckerboardPoints(I1);
  24. % Przekształcenie współrzędnych wykrytych punktów do układu orginalnego
  25. % obrazu
  26. refPoints1 = bsxfun(@plus, refPoints1, newOrigin1);
  27.  
  28. worldPoints = generateCheckerboardPoints(boardSize, squareSize);
  29. % wyznaczenie położenia i orientacji kamery względem układu globalnego
  30. [R1, t1] = extrinsics(refPoints1, worldPoints, cameraParams);
  31.  
  32. [refPoints2, boardSize] = detectCheckerboardPoints(I2);
  33. refPoints2 = bsxfun(@plus, refPoints2, newOrigin2);
  34. [R2, t2] = extrinsics(refPoints2, worldPoints, cameraParams);
  35.  
  36. % obliczenie macierzy dla obu kamer
  37. cameraMatrix1 = cameraMatrix(cameraParams, R1, t1);
  38. cameraMatrix2 = cameraMatrix(cameraParams, R2, t2);
  39.  
  40. %% Ekstrakcja Feature Points
  41.  
  42. % Detekcja feature points
  43. imagePoints1 = detectSURFFeatures(rgb2gray(I1), 'MetricThreshold', 600);
  44. imagePoints2 = detectSURFFeatures(rgb2gray(I2), 'MetricThreshold', 600);
  45. % Ekstrakcja feature descriptors
  46. features1 = extractFeatures(rgb2gray(I1), imagePoints1);
  47. features2 = extractFeatures(rgb2gray(I2), imagePoints2);
  48.  
  49. % wyświetlenie 1500 najlepszych punktów
  50. figure;
  51. imshow(I1);
  52. title('1500 najsilniejszych punktów');
  53. hold on;
  54. plot(selectStrongest(imagePoints1, 1500));
  55.  
  56. %% Dopasowanie features pomiędzy obrazami za pomocą funkcji matchFeatures
  57.  
  58. indexPairs = matchFeatures(features1, features2, 'MaxRatio', 0.5);
  59. matchedPoints1 = imagePoints1(indexPairs(:, 1));
  60. matchedPoints2 = imagePoints2(indexPairs(:, 2));
  61.  
  62. % Wizualozacja powiązanych punktów
  63. figure;
  64. showMatchedFeatures(I1, I2, matchedPoints1, matchedPoints2);
  65. title('Powiązane punkty');
  66.  
  67. % Transformacja punktów do globalnego układu współrzędnych
  68. matchedPoints1.Location = bsxfun(@plus, matchedPoints1.Location, newOrigin1);
  69. matchedPoints2.Location = bsxfun(@plus, matchedPoints2.Location, newOrigin2);
  70.  
  71. %% Reconstrukcja obiektu z dopasowanych features
  72. % Wyznaczenie lokacji punktów w 3D przy użyciu funkcji  
  73. % triangulate implementującej algorytm DLT (Direct Linear Transformation)
  74. % Błąd, będący różnicą między punktami na obrazie a odwzorowaniem
  75. % jest użyty do eliminacji fałszywych dopasowań.
  76.  
  77. [points3D, reprojErrors] = triangulate(matchedPoints1, matchedPoints2, ...
  78.     cameraMatrix1, cameraMatrix2);
  79.  
  80. % Eliminacja błędnych punktów
  81. errorDists = max(sqrt(sum(reprojErrors .^ 2, 2)), [], 3);
  82. validIdx = errorDists < 1;
  83.  
  84. points3D = points3D(validIdx, :);
  85. validPoints1 = matchedPoints1(validIdx, :);
  86. validPoints2 = matchedPoints2(validIdx, :);
  87.  
  88. figure;
  89. showMatchedFeatures(I1, I2, validPoints1,validPoints2);
  90. title('Dopasowane punkty po usunięciu błędów');
  91.  
  92.  
  93. %% Wykres chmury punktów w 3D
  94. % Using the scatter3 function, draw a point cloud representation of each
  95. % feature location and its corresponding colors.
  96.  
  97. % rysowanie kamer
  98. figure;
  99. plotCamera('Location', -t1 * R1', 'Orientation', R1', 'Size', 10, ...
  100.     'Color', 'r', 'Label', '1');
  101. hold on
  102. grid on
  103. plotCamera('Location', -t2 * R2', 'Orientation', R2', 'Size', 10, ...
  104.     'Color', 'b', 'Label', '2');
  105.  
  106. % wyznaczenie koloru odwzorowywanych punktów
  107. validPoints1 = round(validPoints1.Location);
  108. numPixels = size(I1, 1) * size(I1, 2);
  109. allColors = reshape(im2double(I1), [numPixels, 3]);
  110. colorIdx = sub2ind([size(I1, 1), size(I1, 2)], validPoints1(:,2), ...
  111.     validPoints1(:, 1));
  112. color = allColors(colorIdx, :);
  113.  
  114. % dodanie zielonego punktu reprezentującego początek układu współrzędnych
  115. points3D(end+1,:) = [0,0,0];
  116. color(end+1,:) = [0,1,0];
  117.  
  118. % rysowanie chmury punktów
  119. showPointCloud(points3D, color, 'VerticalAxisDir', 'down', 'MarkerSize', 45);
  120. xlabel('x (mm)');
  121. ylabel('y (mm)');
  122. zlabel('z (mm)')
  123. title('Rekonstrukcja chmury punktów');
  124.  
  125. hold off;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement