www.gusucode.com > images 案例代码 matlab源码程序 > images/CreateGeometricTransformationForImageAlignmentExample.m
%% Create Geometric Transformation for Image Alignment % This example shows how to create a geometric transformation that can be % used to align two images. %% % Create a checkerboard image and rotate it to create a misaligned image. % Copyright 2015 The MathWorks, Inc. I = checkerboard; J = imrotate(I,30); imshowpair(I,J,'montage') %% % Define some control points on the fixed image (the checkerboard) and % moving image (the rotated checkerboard). You can define points % interactively using the Control Point Selection tool. fixedPoints = [11 11; 41 71]; movingPoints = [14 44; 70 81]; %% % Create a geometric transformation that can be used to align the two % images, returned as an |affine2d| geometric transformation object. tform = fitgeotrans(movingPoints,fixedPoints,'NonreflectiveSimilarity') %% % Use the |tform| estimate to resample the rotated image to register it % with the fixed image. The regions of color (green and magenta) in the % false color overlay image indicate error in the registration due to lack % of precise correspondence in the control points. Jregistered = imwarp(J,tform,'OutputView',imref2d(size(I))); falsecolorOverlay = imfuse(I,Jregistered); figure imshow(falsecolorOverlay,'InitialMagnification','fit'); %% % Recover angle and scale of the transformation by checking how a unit % vector parallel to the x-axis is rotated and stretched. u = [0 1]; v = [0 0]; [x, y] = transformPointsForward(tform, u, v); dx = x(2) - x(1); dy = y(2) - y(1); angle = (180/pi) * atan2(dy, dx) scale = 1 / sqrt(dx^2 + dy^2)