赞
踩
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("去除不满足几何变化的特征点的匹配结果")
首先,不要上来就复制代码,先看看每一行代码都是什么含义,这样才不会出错。
step3里面有这样的代码:
subplot('Position',[0.0 0.0 0.5 4])
由于我当时用的是显示器,这样可以更精准的排列2个图像。但是到了你们的电脑上可能显示器尺寸什么的不一样,导致显示不出来。我现在已经修改了,你们可以直接使用
subplot(1,2,1)
这种写法,或者根据你们的显示器调节参数
不同的图片,尺寸、特征点是不同的,所以:
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);
这里面的9825、9994
之类的特征点数量,需要你根据实际修改。一般报错信息会很明显的提示你的,不要超过多少。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。