SSD介绍参考:https://blog.csdn.net/hujingshuang/article/details/47759579
Matlab代码实现:
注:img1,img2为参与特征匹配的图像,corners1,corners2分别为img1,img2上的角点(特征点)
function [count,matched]=match(img1,img2,corners1,corners2)
img1=im2double(img1);img2=im2double(img2);
if size(img1,3)==3
img1_gray=rgb2gray(img1);
else
img1_gray=img1;
end
if size(img2,3)==3
img2_gray=rgb2gray(img2);
else
img2_gray=img2;
end
c1=size(corners1,1);
c2=size(corners2,1);
[m,n]=size(img2_gray);
count=1;
matched=zeros(max(c1,c2),2);
for i=1:c1
d_t=99;
for j=1:c2
if corners2(j,1)>1&&corners2(j,1)<m&&corners2(j,2)>1&&corners2(j,2)<n
d=0;
for x=-1:1
for y=-1:1
d=d+(img1_gray(corners1(i,1)+x,corners1(i,2)+y)-img2_gray(corners2(j,1)+x,corners2(j,2)+y))^2;
end
end
if d<d_t&&d<0.05 % 此处设置阈值,这是多次实验设置出的阈值
d_t=d;
matched(count,:)=[i,j];
end
end
end
if matched(count,1)~=0
count=count+1;
end
end
count=count-1;
end
展示代码为:
clc;clear;close all;
img1=imread('figure1.png');
[c1,o1]=corner_detect(img1,0.3);
img2=imread('figure2.png');
[c2,o2]=corner_detect(img2,0.2);
[count,matched]=match(img1,img2,o1,o2);
m=max(size(img1,1),size(img2,1));n=max(size(img1,2),size(img2,2));
o2(:,2)=o2(:,2)+n;
final=zeros(m,2*n,3);
for i=1:size(img1,1)
for j=1:size(img1,2)
final(i,j,:)=img1(i,j,:);
end
end
for i=1:size(img2,1)
for j=n+1:n+size(img2,2)
final(i,j,:)=img2(i,j-n,:);
end
end
figure,imshow(uint8(final));
hold on;
plot(o1(:,2),o1(:,1),'ro');
plot(o2(:,2),o2(:,1),'g+');
hold off;
figure,imshow(uint8(final));
hold on;
for i=1:count
plot(o1(matched(i,1),2),o1(matched(i,1),1),'ro');
plot(o2(matched(i,2),2),o2(matched(i,2),1),'g+');
plot([o1(matched(i,1),2),o2(matched(i,2),2)],[o1(matched(i,1),1),o2(matched(i,2),1)],'y');
end
hold off;
测试结果:
调用Matlab自带Harris角点检测和匹配函数:
clc;clear;close all;
img1 = imread('figure3.jpg');
img2 = imread('figure4.jpg');
img1_gray = rgb2gray(img1);
img2_gray =rgb2gray(img2);
corners1 = detectHarrisFeatures(img1_gray);
corners2= detectHarrisFeatures(img2_gray);
[features1, corners1] = extractFeatures(img1_gray, corners1);
[features2, corners2] = extractFeatures(img2_gray, corners2);
boxPairs = matchFeatures(features1, features2);
matched1 = corners1(boxPairs(:, 1), :);
matched2 = corners2(boxPairs(:, 2), :);
showMatchedFeatures(img1, img2, matched1,matched2, 'montage');
结果如下: