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