Please share a code in matlab for image alignment using affi
Please share a code in matlab for image alignment using affine transformation.
Solution
A) Read and crop image:
I = imread(\'demo.png\');
I = I(:,1:end-3,:);
B) egde detection:
BW = edge(rgb2gray(I), \'canny\');
C) hough transform:
[H T R] = hough(BW); P = houghpeaks(H, 4, \'threshold\',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);
D) shearing transformation:
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform(\'affine\', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);
E) show edges :
figure, imshow(BW)
F) show accumlation matrix and peaks :
figure, imshow(imadjust(mat2gray(H)), [], \'XData\',T, \'YData\',R, \'InitialMagnification\',\'fit\') xlabel(\'\\theta (degrees)\'), ylabel(\'\ ho\'), colormap(hot), colorbar hold on, plot(T(P(:,2)), R(P(:,1)), \'gs\', \'LineWidth\',2), hold off axis on, axis normal
G) show image with lines overlayed, and the aligned/rotated image:
figure subplot(121), imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), \'g.-\', \'LineWidth\',2);
end, hold off subplot(122),
imshow(II)
