fork download
  1. % 3. Homography computation
  2. function H = Homography(p1, p2)
  3. % Number of points
  4. num_points = size(p1, 1);
  5.  
  6. % Build the matrix A for which A * h = 0
  7. A = zeros(2 * num_points, 9);
  8. for i = 1:num_points
  9. x = p1(i, 1);
  10. y = p1(i, 2);
  11. xp = p2(i, 1);
  12. yp = p2(i, 2);
  13.  
  14. A(2 * i - 1, :) = [-x, -y, -1, 0, 0, 0, x * xp, y * xp, xp];
  15. A(2 * i, :) = [0, 0, 0, -x, -y, -1, x * yp, y * yp, yp];
  16. end
  17.  
  18. % Perform SVD on A
  19. [U, D, V] = svd(A);
  20. disp('The U is:')
  21. disp(U)
  22.  
  23. disp('The D is:')
  24. disp(D)
  25.  
  26. disp('The V is:')
  27. disp(V)
  28.  
  29. % The homography matrix is the last column of V, reshaped to 3x3
  30. H = reshape(V(:, 9), [3, 3])';
  31.  
  32. disp('The H is:')
  33. disp(H)
  34. end
  35.  
  36. %Utility function
  37.  
  38. function i = geokor (H, f, g)
  39. % ====================
  40. [h1 w1 d] = size(f); f = double(f); % Image dimensions
  41. [h2 w2 d] = size(g); g = double(g);
  42.  
  43. % Transformation of image corners to determine the resulting size
  44. cp = norm2(H * [1 1 w1 w1; 1 h1 1 h1; 1 1 1 1]);
  45. Xpr = min([cp(1, :) 0]) : max([cp(1, :) w2]); % min x : max x
  46. Ypr = min([cp(2, :) 0]) : max([cp(2, :) h2]); % min y : max y
  47. [Xp, Yp] = ndgrid(Xpr, Ypr);
  48. [wp hp] = size(Xp); % = size(Yp)
  49. X = norm2(H \ [Xp(:) Yp(:) ones(wp*hp, 1)]'); % Indirect transformation
  50.  
  51. xI = reshape(X(1, :), wp, hp)'; % Resampling of intensity values
  52. yI = reshape(X(2, :), wp, hp)';
  53. f2 = zeros(hp, wp); i = f2;
  54. for k = 1 : d
  55. f2(1:h1, 1:w1, k) = f(:, :, k);
  56. i(:, :, k) = interp2(f2(:, :, k), xI, yI); % bilinear interpolation
  57. end
  58. % Copy the original image g in the rectified image f
  59. off = -round([min([cp(1, :) 0]) min([cp(2, :) 0])]);
  60. Index = find(sum(g, 3) > 9);
  61. for k = 1 : d
  62. iPart = i(1+off(2) : h2+off(2), 1+off(1) : w2+off(1), k);
  63. fChannel = g(:, :, k);
  64. iPart(Index) = fChannel(Index);
  65. i(1+off(2) : h2+off(2), 1+off(1) : w2+off(1), k) = iPart;
  66. end
  67. i(~isfinite(i)) = 0;
  68. i = uint8(i);
  69.  
  70. end
  71.  
  72. function n = norm2(x)
  73. for i = 1 : 3
  74. n(i,:) = x(i,:) ./ x(3,:);
  75. end
  76.  
  77. end
  78.  
  79.  
  80. % 1. Image acquisition:
  81. %C:\Users\DELL\Desktop\Assignment2
  82.  
  83. jakobs_a = imread('C:\Users\DELL\Desktop\Assignment2\jakobs_a.jpg');
  84. jakobs_b = imread('C:\Users\DELL\Desktop\Assignment2\jakobs_b.jpg');
  85. jakobs_c = imread('C:\Users\DELL\Desktop\Assignment2\jakobs_c.jpg');
  86.  
  87.  
  88. % 2. Correspondence analysis: Transfer the three images into the computer (imread) and
  89. % measure interactively (figure, imshow, ginput) at least four corresponding image points
  90. % x->x between two neighboring images.
  91.  
  92. figure;
  93. subplot(1, 3, 1); imshow(jakobs_a); title('jakobs_a.jpg');
  94. subplot(1, 3, 2); imshow(jakobs_b); title('jakobs_b.jpg');
  95. subplot(1, 3, 3); imshow(jakobs_c); title('jakobs_c.jpg');
  96.  
  97. % Select 4 corresponding points between Image 1 and Image 2
  98. figure; imshow(jakobs_a); title('Select 4 points in jakobs_a');
  99. [x1, y1] = ginput(4);
  100.  
  101. figure; imshow(jakobs_b); title('Select 4 points in jakobs_b');
  102. [x2, y2] = ginput(4);
  103.  
  104. % Store points as homogeneous coordinates
  105. points1 = [x1 y1 ones(4, 1)];
  106. points2 = [x2 y2 ones(4, 1)];
  107.  
  108.  
  109.  
  110.  
  111. % Compute the homography matrix H12 from first to second image.
  112. H12 = Homography(points1, points2);
  113.  
  114.  
  115.  
  116. i = geokor(H12, jakobs_a, jakobs_b); % Transform image f using H and combine with g
  117. % figure;
  118.  
  119. imshow(i);title('Select 4 points in attached image 1 and 2');
  120. [x3, y3] = ginput(4);
  121. imshow(img3); title('Select 4 points in attached image 3');
  122. [x4, y4] = ginput(4);
  123. points12 = [x3 y3 ones(4, 1)];
  124. points4 = [x4 y4 ones(4, 1)];
  125. H23 = Homography(points12, points4);
  126.  
  127. finalResult = geokor(H23, i, jakobs_c);
  128. imshow(finalResult);
Success #stdin #stdout #stderr 0.14s 48592KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
error: imread: unable to find file 'C:\Users\DELL\Desktop\Assignment2\jakobs_a.jpg'
error: called from
    imageIO at line 73 column 5
    imread at line 106 column 30
    /home/09GoxP/prog.octave at line 83 column 10