当前位置:   article > 正文

ORB特征提取和匹配_orb特征点匹配matlab

orb特征点匹配matlab


一、步骤

Step1:读取彩色图片
1.新建实验用的文件夹,准备好实验用到的图片,在MATLAB2021a软件中编写相关脚本文件。
2.清理变量空间以及命令行。
3.用imread()函数读取彩色图片,使用imfuse()函数进行简单的图像复合用imshow()函数显示出来即可看到2张图的差异;将上诉3幅图用subplot()展示在一张figure中。
Step2:彩色图像转化为灰度图
1.直接使用rgb2gray()函数将上述2幅rgb图像转化为灰度图。
2.做与Step1一样的展示,这里使用imshowpair()函数来展示两种灰度图的差异。
3.使用edge()函数同时选用canny参数对灰度图进行canny算子边缘检测,修改sigma值。
4.采用与2相同的方式对结果进行展示。
Step3 对左右图像分别进行特征点提取:
1.使用detectORBFeatures()函数对灰度图进行特征点提取。
2.观察特征点变量发现两张图都提取出了9000余个特征点,不便直接展示出来。
3.分别展示出两幅灰度图的原图像,并在其基础上使用selectStrongest()函数分别展示其最大的3000个特征点。
Step 4:根据特征点生成图像的特征向量
使用extractFeatures()函数生成图像的特征向量。
Step 5:对左右图像提取的特征点进行匹配
1.使用matchFeatures()函数进行两张图的特征点匹配,返回6856*2的向量,即互相匹配的6856对点所对应的坐标索引。
2.使用showMatchedFeatures()函数显示结果,分别选用默认参数falsecolor和montage,
第一种参数将第一幅图显示为red,第二张图显示为cyan,放在一张图里面;而第二种参数则将两幅图并排显示。
Step 6: 预测几何变化,去除不满足变化的野值
1.使用estimateGeometricTransform()函数预测几何变化,选用不同参数,去除不满足变化的野值。
2.采用Step5中相似的方式显示结果 。


二、代码

clc
clear
close all

%%  Step1:读取彩色图片
img_left_color  = imread('orange1.PNG');
img_right_color = imread('orange2.PNG');

% 显示结果
figure('Name','Step1:读取彩色图片')
subplot('Position',[0.0 0.0 0.66 1])
imshow([img_left_color img_right_color])
title("左图和右图")
subplot('Position',[0.67 0.0 0.33 1])
imshow(imfuse(img_left_color, img_right_color))
title("左右图像差异")

%% Step2:彩色图像转化为灰度图
img_left_gray  = rgb2gray(img_left_color);
img_right_gray = rgb2gray(img_right_color);

% 显示结果
figure('Name','Step2:彩色图像转化为灰度图')
subplot('Position',[0.0 0.0 0.66 1])
imshow([img_left_gray img_right_gray])
title("左图和右图")
subplot('Position',[0.67 0.0 0.33 1])
imshowpair(img_left_gray,img_right_gray) %等效于 imshow(imfuse(img_left_gray, img_right_gray))
title("左右图像差异")

%% Step2.1:对灰度图进行边缘检测
img_left_edge  = edge(img_left_gray,'canny',[],1);      % 使用canny算子进行边缘检测
img_right_edge = edge(img_right_gray,'canny',[],1);     % 使用canny算子进行边缘检测

% 显示结果
figure('Name','Step2.1:对灰度图进行边缘检测')
subplot('Position',[0.0 0.0 0.66 1])
imshow([img_left_edge img_right_edge])
title("左图和右图")
subplot('Position',[0.67 0.0 0.33 1])
imshowpair(img_left_edge,img_right_edge)
title("左右图像差异")

%% Step3:对左右图像分别进行特征点提取
left_ORB_points  = detectORBFeatures(img_left_gray);
right_ORB_points = detectORBFeatures(img_right_gray);

% 显示结果
figure('Name','Step3:对左右图像分别进行特征点提取')
%subplot('Position',[0.0 0.0 0.5 4])
subplot(1,2,1)
imshow(img_left_gray);hold on;
title("部分左图特征点")
plot(left_ORB_points(1:2:9825,:),'showOrientation',true);
%subplot('Position',[0.5 0.0 0.5 4])
subplot(1,2,2)
imshow(img_right_gray);hold on;
title("部分右图特征点")
plot(right_ORB_points(1:2:9994,:),'showOrientation',true);

%% Step4:根据特征点生成图像的特征向量
[left_features,  left_ORB_points]  = extractFeatures(img_left_gray,  left_ORB_points);
[right_features, right_ORB_points] = extractFeatures(img_right_gray, right_ORB_points);

%% Step5: 对左右图像提取的特征点进行匹配
image_pairs = matchFeatures(left_features, right_features,'MatchThreshold',15); % 调整匹配阈值观察变化
left_matched_points  = left_ORB_points(image_pairs(:, 1), :);
right_matched_points = right_ORB_points(image_pairs(:, 2), :);

% 显示结果
figure('Name','Step5:对左右图像提取的特征点进行匹配')
subplot('Position',[0.0 0.0 0.66 1])
showMatchedFeatures(img_left_gray, img_right_gray, left_matched_points,right_matched_points, 'montage');
title("特征点匹配结果")
subplot('Position',[0.67 0.0 0.33 1])
showMatchedFeatures(img_left_gray, img_right_gray, left_matched_points,right_matched_points);
title("特征点匹配结果")

%% Step6: 预测几何变化,去除不满足变化的野值(三种几何变换变化的方法:% 'similarity'(相似) | 'affine'(仿射) | 'projective'(投影))
[tform, left_matched_points_estimate, right_matched_points_estimate] = estimateGeometricTransform(left_matched_points, right_matched_points, 'projective'); 
% 显示结果
figure('Name','Step6: 预测几何变化,去除不满足变化的野值')
subplot('Position',[0.0 0.0 0.66 1])
showMatchedFeatures(img_left_gray, img_right_gray, left_matched_points_estimate,right_matched_points_estimate, 'montage');
title("去除不满足几何变化的特征点的匹配结果")
subplot('Position',[0.67 0.0 0.33 1])
showMatchedFeatures(img_left_gray, img_right_gray, left_matched_points_estimate,right_matched_points_estimate);
title("去除不满足几何变化的特征点的匹配结果")


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

三、部分结果展示

3.1 使用Sobel算子且方向为vertical,进行边缘检测

在这里插入图片描述

3.2 特征点提取(部分)

在这里插入图片描述

3.3 特征点匹配

在这里插入图片描述

四、问题汇总

首先,不要上来就复制代码,先看看每一行代码都是什么含义,这样才不会出错。

4.1 特征点提取没有任何图像

step3里面有这样的代码:

subplot('Position',[0.0 0.0 0.5 4])
  • 1

由于我当时用的是显示器,这样可以更精准的排列2个图像。但是到了你们的电脑上可能显示器尺寸什么的不一样,导致显示不出来。我现在已经修改了,你们可以直接使用

subplot(1,2,1)
  • 1

这种写法,或者根据你们的显示器调节参数

4.2 报错:超出数组索引

不同的图片,尺寸、特征点是不同的,所以:

subplot(1,2,1)
imshow(img_left_gray);hold on;
title("部分左图特征点")
plot(left_ORB_points(1:2:9825,:),'showOrientation',true);
%subplot('Position',[0.5 0.0 0.5 4])
subplot(1,2,2)
imshow(img_right_gray);hold on;
title("部分右图特征点")
plot(right_ORB_points(1:2:9994,:),'showOrientation',true);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里面的9825、9994之类的特征点数量,需要你根据实际修改。一般报错信息会很明显的提示你的,不要超过多少。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/898888
推荐阅读
相关标签
  

闽ICP备14008679号