我们有两张图像,一张作为参考,另一张我们想要使用 Matlab 来像参考一样对齐。为此,我们需要在两个图像中找到相似点,然后使用最小二乘法计算核矩阵进行变换,如下代码所示:
clear all; close all; clc; imtool close all;
I1 = rgb2gray(im);
I2 = rgb2gray(ref);
points1 = detectSURFFeatures(I1);
points2 = detectSURFFeatures(I2);
[features1,valid_points1] = extractFeatures(I1,points1,'SURFSize',64);
[features2,valid_points2] = extractFeatures(I2,points2,'SURFSize',64);
indexPairs = matchFeatures(features1,features2);
matchedPoints1 = valid_points1(indexPairs(:,1),:);
matchedPoints2 = valid_points2(indexPairs(:,2),:);
[tform, correct1,correct2] = estimateGeometricTransform(matchedPoints1,matchedPoints2,'projective','MaxNumTrials',100000,'Confidence',99.9999,'MaxDistance',4);
sourcepoints = correct1.Location;
targetpoints = correct2.Location;
sizenum = size(sourcepoints,1);
x_source = sourcepoints(:,1);
y_source = sourcepoints(:,2);
x_target = targetpoints(:,1);
y_target = targetpoints(:,2);
zero_vec = zeros([sizenum,1]);
one_vec = ones([sizenum,1]);
H = [x_source,y_source,one_vec,zero_vec,zero_vec,zero_vec,-x_source.*x_target,-y_source.*x_target;...
zero_vec,zero_vec,zero_vec,x_source,y_source,one_vec,-x_source.*y_target,-y_source.*y_target];
Y = [x_target;y_target];
variables = (inv(H'*H))*H'*Y;
variables(9) …Run Code Online (Sandbox Code Playgroud) matlab image-processing feature-extraction feature-detection