Matlab CV ToolBox的使用之matchFeatures

Matlab的自带说明:

matchFeatures Find matching features

 INDEX_PAIRS = matchFeatures(FEATURES1, FEATURES2) 

返回INDEX_PAIRS为[P,2]数组,包含最可能相关的特征点的数组。FEATURES1为[M1,N]的数组,FEATURES2为[M2,N]的数组,都可以是由FREAK描述子产生的二值特征。



  [INDEX_PAIRS, MATCH_METRIC] = matchFeatures(FEATURES1, FEATURES2, ...)

除了上述返回值,还返回和INDEX_PAIRS相关的度量值MATCH_METRIC,他是[P,1]的数组。


  [INDEX_PAIRS, MATCH_METRIC] = matchFeatures(...,Name, Value) specifies

附加name-value可以为:

'Method'   两个特征向量之间的距离小于MatchThreshold设置的阈值时他们为匹配的。有三种:

‘Threshold’:只是用匹配阈值。每个特征点能返回多个匹配点。

‘NearestNeighborSymmetric’:使用匹配阈值且只返回一个匹配点。只选择最近邻的匹配点。

'NearestNeighborRatio':使用匹配且消除了模糊匹配。只有当两特征点之间的最近距离满足一定比率,才符合。比率为特征点到第一个最近邻和到第二个最近邻之间的比值。

         Default: 'NearestNeighborRatio'


‘MatchThreshold’   0 < T <= 100。大于百分之T的特征点确定为匹配点。减小T可获得更多匹配。

                      Default: 10.0 for binary feature vectors (二分特征向量)

                                1.0 otherwise


'MaxRatio'    0 < R <= 1,较小R能得到更多匹配点。只能和'NearestNeighborRatio' method一起使用。

                      Default: 0.6


'Metric'    字符串,当FEATURES1和FEATURES2是binaryFeatures式不可用。

 'SAD'         : Sum of absolute differences

 'SSD'         : Sum of squared differences 

'normxcorr'   : Normalized cross-correlation


 Default: 'SSD'

注意:当FATURES1和FEATURES2是binaryFeatures时,使用Hamming 距离来计算相似的度量。


'Prenormalized'   逻辑标量。TRUE表示FEATURES1和FEATURES2在匹配前已经正规化到单元向量。设置FALSE会导致FEATURES1和FEATURES2的正规化。注意,如果匹配前没有正规化将产生错误结果。FEATURES1和FEATURES2是binaryFeature的时候,参数不可用。

  Default: false


注意:
%   Notes
%   ----- 
%   The range of values of MATCH_METRIC varies as a function of the feature
%   matching metric being used. Prior to computation of SAD and SSD
%   metrics, the feature vectors are normalized to unit vectors. The table
%   below summarizes the metric ranges and perfect match values:
%
%      Metric      Range                            Perfect Match Value
%      ----------  -------------------------------  ------------------- 
%      SAD         [0, 2*sqrt(size(FEATURES1, 1))]          0
%      SSD         [0, 4]                                   0
%      normxcorr   [-1, 1]                                  1
%
%      Hamming     [0, FEATURES1.NumBits]                   0

%



matlab:函数文档

%matchFeatures Find matching features
%   INDEX_PAIRS = matchFeatures(FEATURES1, FEATURES2) returns a P-by-2
%   matrix, INDEX_PAIRS, containing indices to the features most likely to
%   correspond between the two input feature matrices. The function takes
%   two inputs, FEATURES1, an M1-by-N matrix, and FEATURES2, an M2-by-N
%   matrix. FEATURES1 and FEATURES2 can also be binaryFeatures objects in 
%   the case of binary descriptors produced by the FREAK descriptor.
%
%   [INDEX_PAIRS, MATCH_METRIC] = matchFeatures(FEATURES1, FEATURES2, ...)
%   also returns the metric values that correspond to the associated
%   features indexed by INDEX_PAIRS in a P-by-1 matrix MATCH_METRIC.
%
%   [INDEX_PAIRS, MATCH_METRIC] = matchFeatures(...,Name, Value) specifies
%   additional name-value pairs described below:
%
%   'Method'           A string used to specify the matching strategy. All
%                      three methods use the match threshold. Two feature 
%                      vectors match when the distance between them is less 
%                      than the threshold set by the MatchThreshold 
%                      parameter. Set the method to one of the following: 
%
%                      'Threshold': Only uses the match threshold. This 
%                         method can return more than one match for each 
%                         feature. 
%      
%                      'NearestNeighborSymmetric': Only returns unique 
%                         matches in addition to using the match threshold. 
%                         A feature vector only matches to its nearest 
%                         neighbor in the other feature set.
%
%                      'NearestNeighborRatio': Eliminates ambiguous matches 
%                         in addition to using the match threshold. A 
%                         feature vector is matched to its nearest neighbor 
%                         in the other feature set, when the nearest 
%                         neighbor satisfies a ratio test. The ratio test 
%                         compares the distances from the feature vector to
%                         its first and second nearest neighbors in the 
%                         other feature set.
%
%                      Default: 'NearestNeighborRatio'
%
%   'MatchThreshold'   A scalar T, 0 < T <= 100, specifying a threshold
%                      for selecting the strongest matches. Matches having
%                      metrics more than T percent from a perfect match 
%                      are rejected. Increase T to return more matches.

%                      Default: 10.0 for binary feature vectors 
%                                1.0 otherwise
%
%   'MaxRatio'         A scalar R, 0 < R <= 1, specifying a ratio threshold 
%                      for rejecting ambiguous matches. Increase R to return
%                      more matches. This parameter is used only with
%                      'NearestNeighborRatio' method.
%
%                      Default: 0.6
%
%   'Metric'           A string used to specify the feature matching
%                      metric. This parameter is not applicable when 
%                      FEATURES1 and FEATURES2 are binaryFeatures objects.
%                      Possible values are:
%                        'SAD'         : Sum of absolute differences
%                        'SSD'         : Sum of squared differences 
%                        'normxcorr'   : Normalized cross-correlation
%
%                      Default: 'SSD'
%
%                      Note: When FEATURES1 and FEATURES2 are binaryFeatures 
%                            objects, Hamming distance is used to compute
%                            the similarity metric.
%
%   'Prenormalized'    A logical scalar. Use true to indicate that FEATURES1
%                      and FEATURES2 are already normalized to unit vectors 
%                      prior to matching. Setting this flag to false will 
%                      result in normalizing FEATURES1 and FEATURES2. Note 
%                      that setting this flag to true when features are not 
%                      normalized in advance will produce wrong results. 
%                      This parameter is not applicable when FEATURES1 and 
%                      FEATURES2 are binaryFeatures objects.
%
%                      Default: false
%
%   Notes
%   ----- 
%   The range of values of MATCH_METRIC varies as a function of the feature
%   matching metric being used. Prior to computation of SAD and SSD
%   metrics, the feature vectors are normalized to unit vectors. The table
%   below summarizes the metric ranges and perfect match values:
%
%      Metric      Range                            Perfect Match Value
%      ----------  -------------------------------  ------------------- 
%      SAD         [0, 2*sqrt(size(FEATURES1, 1))]          0
%      SSD         [0, 4]                                   0
%      normxcorr   [-1, 1]                                  1
%
%      Hamming     [0, FEATURES1.NumBits]                   0
%
%   This function changed in the release R2012b. Previous versions
%   used a different matching strategy. If you need the same results 
%   produced by the previous implementation, use 
%   matchFeatures(FEATURES1, FEATURES2, 'Method', 'NearestNeighbor_old',...).
%   
%   Class Support
%   -------------
%   FEATURES1 and FEATURES2 can be logical, int8, uint8, int16, uint16,
%   int32, uint32, single, double, or binaryFeatures object.
%
%   The output class of INDEX_PAIRS is uint32. MATCH_METRIC is double when
%   FEATURES1 and FEATURES2 are double. Otherwise, it is single.
%

% [matchLoc1 matchLoc2] = siftMatch(img1, img2)
% 初匹配用Matlab自带的函数:matchFeatures(feature1,feature2,Name,Value);
%
% This function reads two images, finds their SIFT features, and
%   displays lines connecting the matched keypoints.  A match is accepted
%   only if its distance is less than distRatio times the distance to the
%   second closest match.
% It returns the matched points of both images, matchLoc1 = [x1,y1;x2,y2;...]
%
% Example: match('scene.pgm','book.pgm');

function [matchLoc1,matchLoc2] = siftMatch(img1, img2)

% load matchdata
% load img1data
% load img2data
%{,
% Find SIFT keypoints for each image
[des1, loc1] = sift(img1);
[des2, loc2] = sift(img2);
% save img1data des1 loc1,des是特征算子,loc是坐标和~
% save img2data des2 loc2
% 'Prenormalized'
desPairs = matchFeatures(des1,des2,...
        'MaxRatio',0.601,...
        'MatchThreshold',11,...
        'Prenormalized',true);
matched1Points = loc1(desPairs(:,1),:);
matched2Points = loc2(desPairs(:,2),:);
figure;
showMatchedFeatures(img1, img2, matched1Points(:,1:1:2), ...
    matched2Points(:,1:1:2), 'montage');
title('Putatively Matched Points (Including Outliers)');

x1 = matched1Points(:,2);
x2 = matched2Points(:,2);
y1 = matched1Points(:,1);
y2 = matched2Points(:,1);

matchLoc1 = [x1,y1];
matchLoc2 = [x2,y2];

%% 对这一段代码进行修改
% % For efficiency in Matlab, it is cheaper to compute dot products between
% %  unit vectors rather than Euclidean distances.  Note that the ratio of 
% %  angles (acos of dot products of unit vectors) is a close approximation
% %  to the ratio of Euclidean distances for small angles.
% %在Matlab中,dot的运算比欧几里德的减法运算的效率更高。
% % distRatio: Only keep matches in which the ratio of vector angles from the
% %   nearest to second nearest neighbor is less than distRatio.
% distRatio = 0.6;   
% 
% % For each descriptor in the first image, select its match to second image.
% %在image1中寻找与image2相匹配的点
% des2t = des2';                          % Precompute matrix transpose
% matchTable = zeros(1,size(des1,1));
% for i = 1 : size(des1,1)
%    dotprods = des1(i,:) * des2t;        % Computes vector of dot products
%    [vals,indx] = sort(acos(dotprods));  % Take inverse cosine and sort results
%    %采用反余弦和结果排序,vals是结果排序的值,index是反余弦的值
% 
%    % Check if nearest neighbor has angle less than distRatio times 2nd.
%    if (vals(1) < distRatio * vals(2))
%       matchTable(i) = indx(1);
%    else
%       matchTable(i) = 0;
%    end
% end
% % save matchdata matchTable
% %}
% 
% % Create a new image showing the two images side by side.
% %创建一个新的图像,把两张图片都放在其中
% img3 = appendimages(img1,img2);
% 
% % Show a figure with lines joining the accepted matches.
% figure('Position', [100 100 size(img3,2) size(img3,1)]);
% colormap('gray');
% imagesc(img3);
% hold on;
% cols1 = size(img1,2);
% for i = 1: size(des1,1)
%   if (matchTable(i) > 0)
%     line([loc1(i,2) loc2(matchTable(i),2)+cols1], ...
%          [loc1(i,1) loc2(matchTable(i),1)], 'Color', 'c');
%   end
% end
% hold off;
% num = sum(matchTable > 0);
% fprintf('Found %d matches.\n', num);
% 
% idx1 = find(matchTable);
% idx2 = matchTable(idx1);
% x1 = loc1(idx1,2);
% x2 = loc2(idx2,2);
% y1 = loc1(idx1,1);
% y2 = loc2(idx2,1);
% 
% matchLoc1 = [x1,y1];
% matchLoc2 = [x2,y2];

end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用knnsearch函数进行特征匹配的MATLAB代码: ``` % 读入两张待配准的图像 I1 = imread('image1.jpg'); I2 = imread('image2.jpg'); % 转换为灰度图像 grayI1 = rgb2gray(I1); grayI2 = rgb2gray(I2); % 检测SIFT特征点 points1 = detectSURFFeatures(grayI1); points2 = detectSURFFeatures(grayI2); % 提取特征描述子 [features1, ~] = extractFeatures(grayI1, points1); [features2, ~] = extractFeatures(grayI2, points2); % 进行特征匹配 indexPairs = knnsearch(features2, features1, 'K', 2); % 选取匹配点对 matchedPoints1 = points1; matchedPoints2 = points2(indexPairs(:,1)); % 显示配准结果 figure; showMatchedFeatures(I1, I2, matchedPoints1, matchedPoints2); title('匹配点对'); figure; imshowpair(I1, I2, 'montage'); title('配准结果'); ``` 这段代码中,我们使用MATLAB自带的Computer Vision System Toolbox中的函数来进行SIFT特征点检测、特征提取和图像配准。其中,`detectSURFFeatures`函数用于检测SIFT特征点,`extractFeatures`函数用于提取特征描述子,`knnsearch`函数用于进行特征匹配,`showMatchedFeatures`函数用于显示匹配点对,`imshowpair`函数用于显示配准结果。 在使用`knnsearch`函数进行特征匹配时,我们将features2作为查询点集,features1作为参考点集,并设置`K`参数为2,表示返回每个查询点的最近邻和次近邻。然后,我们选取匹配点对时,直接将points1作为matchedPoints1,将features2中与features1最近邻匹配的点作为matchedPoints2。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值