基于Matlab平台的视觉特征匹配与显示

 clc;
 clear;
 close all;
 
 load colorImage3005;
 load colorImage3006;
 I11=colorImage3005;
 I22=colorImage3006;

% extractFeatures 函数输入为灰度图像,因此要将RGB图像用函数 rgb2gray 进行转换
image1= rgb2gray(I11);
image2= rgb2gray(I22);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Surf 特征检测
ptsImage1 = detectSURFFeatures(image1,'MetricThreshold',800,'NumOctaves',6);
ptsImage2 = detectSURFFeatures(image2,'MetricThreshold',800,'NumOctaves',6);
disp('SURF Features of Image1')
size(ptsImage1)
disp('SURF Features of Image2')
size(ptsImage2)
% Surf 特征提取
[featuresOriginal,validptsImage1] = extractFeatures(image1,ptsImage1);
[featuresDistorted,validptsImage2] = extractFeatures(image2,ptsImage2);

% Surf 特征匹配
index_pairs = matchFeatures(featuresOriginal,featuresDistorted,'MatchThreshold',1,'MaxRatio',1);

matchedptsImage1 = validptsImage1(index_pairs(:,1));
matchedptsImage2 = validptsImage2(index_pairs(:,2));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  points1 = detectHarrisFeatures(image1);
%  points2 = detectHarrisFeatures(image2);
%   
%  [f1, vpts1] = extractFeatures(image1, points1);
%  [f2, vpts2] = extractFeatures(image2, points2);
% 
%  indexPairs = matchFeatures(f1, f2) ;
%  matchedptsImage1 = vpts1(indexPairs(1:20, 1));
%  matchedptsImage2 = vpts2(indexPairs(1:20, 2));

% 显示有误匹配的情况
figure;
subplot(2,1,1)
showMatchedFeatures(I11,I22,matchedptsImage1,matchedptsImage2,'montage');
str=sprintf('Matched inlier points\n(including outliers)');
title(str,'fontname','Times New Roman','FontSize',12);
disp('Matched features without MLESAC')
size(matchedptsImage1)
% estimateGeometricTransform 函数 剔除误匹配 (MSAC算法)
[tform,inlierptsImage2,inlierptsImage1] =estimateGeometricTransform(matchedptsImage2,matchedptsImage1,'similarity','Confidence',99,'MaxDistance',3);

disp('Matched features with MLESAC')
size(inlierptsImage2)
% 显示没有误匹配的情况
subplot(2,1,2)
showMatchedFeatures_fgy(I11,I22,inlierptsImage1,inlierptsImage2,'montage');
str=sprintf('Matched inlier points\n(excluding outliers)');
title(str,'fontname','Times New Roman','FontSize',12);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    显示匹配图像(有一定间距)    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% N 是两幅图像中间空出的像素距离
N=500;
I1=I11;I2=I22;
  I = zeros([size(I1,1) size(I1,2)*2+N size(I1,3)]);
  I(:,1:size(I1,2),:)=I1; 
  I(:,size(I1,2)+1:size(I1,2)+size(I2,2)+N,:)=255;
  I(:,size(I1,2)+1+N:size(I1,2)+size(I2,2)+N,:)=I2;
  figure, imshow(I/255); hold on;
  n_matched_features=size(inlierptsImage1);
% 显示匹配点
for i=1:n_matched_features
    % 画点
    % plot([inlierptsImage1.Location(i,1) inlierptsImage2.Location(i,1)+size(I1,2)+N],[inlierptsImage1.Location(i,2) inlierptsImage2.Location(i,2)],'o','Color','b')
    plot(inlierptsImage1.Location(i,1),inlierptsImage1.Location(i,2),'o','Color','b', 'MarkerSize',9,'LineWidth',1.5)
    plot(inlierptsImage2.Location(i,1)+size(I1,2)+N,inlierptsImage2.Location(i,2),'+','Color','g', 'MarkerSize',9,'LineWidth',1.5)
    % 画线
    plot([inlierptsImage1.Location(i,1) inlierptsImage2.Location(i,1)+size(I1,2)+N],[inlierptsImage1.Location(i,2) inlierptsImage2.Location(i,2)],'-','Color','y','LineWidth',1.5)
 end

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值