在这一篇中,我们将介绍一个新的函数:estimateGeometricTransform,其它的函数都在之前介绍学习过了。
函数/Functions
函数名称:estimateGeometricTransform
功能:利用匹配的特征点对估计几何变换(Similarity,affine,projective transformations)
语法:tform = estimateGeometricTransform(matchedPoints1, matchedPoints2,transformType);
[tform,inlierpoints1,inlierpoints2] = estimateGeometricTransform(matchedPoints1, matchedPoints2,transformType);
[_,status] = estimateGeometricTransform(matchedPoints1, matchedPoints2,transformType);
[ _ ] = estimateGeometricTransform(matchedPoints1, matchedPoints2,transformType,Name,Value);
其中,matchedPoints1和matchedPoints2分别表示两个图像匹配的对应点;tform是几何变换对象(affine2d object/projective2d object);transformType是可以取值为’similarity'/'affine'/'projective',即几何变换的类型;status为返回状态,具体取值如下表;inlierpoints1和inlierpoints2分别为两个图像对应的内点;Name为用一对单引号包含的字符串,Value为对应Name的值。
status | 含义 |
---|---|
0 | 无错误 |
1 | matchedPoints1 和 matchedPoints2点不足 |
2 | 足够的内点没有发现 |
Name | Value |
---|---|
'MaxNumTrials' | 默认值为1000,范围为正整数,为了获取最大内点进行随机试验的次数,取较大的值时,能够提高估计的鲁棒性 |
'Confidence' | 默认值为99,范围为(0,100),获取最爱内点的置信度,取较大值时能够提高估计的鲁棒性 |
'MaxDistance' | 默认值为1.5,范围为>0,可以衡量误差 |
举例:
close all;
clear all;
clc;
% 原图像
original = imread('cameraman.tif');
imshow(original);
title('Base image');
% 退化图像
distorted = imresize(original,0.7);
distorted = imrotate(distorted,31);
figure;
imshow(distorted);
title('Transformed image');
% 提取SURF特征点
ptsOriginal = detectSURFFeatures(original);
ptsDistorted = detectSURFFeatures(distorted);
% 获取描述子
[featuresOriginal,validPtsOriginal] = extractFeatures(original,ptsOriginal);
[featuresDistorted,validPtsDistorted] = extractFeatures(distorted,ptsDistorted);
% 描述子匹配
index_pairs = matchFeatures(featuresOriginal,featuresDistorted);
% 匹配的特征点
matchedPtsOriginal = validPtsOriginal(index_pairs(:,1));
matchedPtsDistorted = validPtsDistorted(index_pairs(:,2));
%显示匹配结果
figure;
showMatchedFeatures(original,distorted,matchedPtsOriginal,matchedPtsDistorted);
title('Matched SURF points, including outliers');
%估计几何变换矩阵
[tform,inlierPtsDistorted,inlierPtsOriginal] = estimateGeometricTransform(matchedPtsDistorted,matchedPtsOriginal,'similarity');
figure;
showMatchedFeatures(original,distorted,inlierPtsOriginal,inlierPtsDistorted);
title('Matched inlier points');
%反变换图像
outputView = imref2d(size(original));
Ir = imwarp(distorted,tform,'OutputView',outputView);
figure;
imshow(Ir);
title('Recovered image');