之前在斯坦福的机器视觉算法与应用课程上学了一些东西,并用matlab编程实现,没来得及整理,现在把它整理一下,出来的效果可能不太完善,这有待后续的研究与改进。
一.特征检测(提取)
基于特征的图像配准方法是图像配准中最常见的方法之一。它不是直接利用图像像素值,二十通过像素值导出的符号特征(如特征点、特征线、特征区域)来实现图像配准,因此可以克服利用灰度信息进行图像配准的缺点,主要体现在以下三个方面:(1)利用特征点而不是图像灰度信息,大大减少了在匹配过程中的计算量;(2)特征点的匹配度量值相对位置变化比较敏感,可以提高匹配的精度;(3)特征点的提取过程可以减少噪声的影响,对灰度变化、图像形变以及遮挡等都有较好的适应能力。
一类重要的点特征:角点(corner points),其定义主要有以下:
Harris检测:数学表达
将图像窗口平移[u,v]产生灰度变化E(u,v)
由泰勒展开,得:
function [x, y, confidence, scale, orientation] = get_interest_points(image,feature_width)
feature_width = 16;
fx = [-1,0,1;-2,0,2;-1,0,1];
fy = [-1,-2,-1;0,0,0;1,2,1];
Ix = imfilter(image,fx);
Iy = imfilter(image,fy);
Ix2 = Ix.* Ix;
%subplot(2,2,1);
%imshow(Ix2);
Ixy = Ix.* Iy;
% subplot(2,2,2);
% imshow(Ixy);
Iy2 = Iy.* Iy;
% subplot(2,2,3);
% imshow(Iy2);
h = fspecial('gaussian',25,2);
% subplot(2,2,4);
% imshow(h);
Ix2 = imfilter(Ix2,h);
Ixy = imfilter(Ixy,h);
Iy2 = imfilter(Iy2,h);
%寻找最大R值
Rmax = 0;
l = 0.05;
[img_height,img_width] = size(image);
M = zeros(img_height,img_width);
for i = 1:img_height
for j = 1:img_width
A = [Ix2(i,j),Ixy(i,j);Ixy(i,j),Iy2(i,j)];
M(i,j) = det(A) - l*(trace(A))^2;
if M(i,j) > Rmax
Rmax = M(i,j);
end
end
end
%局部非极大值抑制
k = 0.01;
cnt = 0; %记录角点数目
result = zeros(img_height,img_width);
for i=2:img_height-1
for j =2:img_width-1
if M(i,j)>k*Rmax && M(i,j)>M(i-1,j-1) && M(i,j)>M(i-1,j)&&M(i,j)>M(i-1,j+1)&&M(i,j)>M(i,j-1)&&M(i,j)>M(i,j+1)&&M(i,j)>M(i+1,j-1)&&M(i,j)>M(i+1,j)&&M(i,j)>M(i+1,j+1)
result(i,j) = 1;
cnt = cnt + 1;
end
end
end
[y,x] = find(result==1);
x_indexleft = find(x <= feature_width/2 - 1); %寻找x方向上左边边缘角点
%去除边缘角点
x(x_indexleft) = [];
y(x_indexleft) = [];
y_indexup = find(y <= feature_width/2 - 1); %寻找y方向上部边缘角点
x(y_indexup) = [];
y(y_indexup) = [];
x_indexright = find(x > img_width - 8); %寻找x方向右边的边缘角点
x(x_indexright) = [];
y(x_indexright) = [];
y_indexdown = find(y > img_height - 8); %寻找y方向下部的边缘角点
x(y_indexdown) = [];
y(y_indexdown) = [];
%x(find(x
function [features] = get_features(image, x, y, feature_width)
y_gradient = imfilter(image,[-1,0,1]');
features = zeros(length(x),128);
for k = 1:length(x) %window of 16x16 of each feature
x_subwindow = x(k)-7:x(k)+8;
y_subwindow = y(k)-7:y(k)+8;
x_subwindow_gradient = x_gradient(y_subwindow,x_subwindow);
y_subwindow_gradient = y_gradient(y_subwindow,x_subwindow);
angles = atan2(y_subwindow_gradient,x_subwindow_gradient);
%for i = 1:16
%for j = 1:16
% if angles(i,j)<0
% angles(i,j) = angles(i,j) + 2*pi;
%end
%end
%end
angles(angles<0) = angles(angles<0) + 2*pi;
%angles_bin = [0 pi/4 pi/2 3*pi/4 pi 5*pi/4 3*pi/2 7*pi/4 2*pi];
angles_binranges = 0:pi/4:2*pi;
B = zeros(1,128);
for i = 1:feature_width/4:feature_width
for j = 1:feature_width/4:feature_width
%A = reshape(angles(i:i+3,j:j+3)',1,16);
subwindow = angles(i:i+3,j:j+3);
angles_bincounts = histc(subwindow(:),angles_binranges);
begin = 1 + 32*(floor(i/4)) + 8*(floor(j/4)); %tab the orientation of each 4x4 window
B(begin:begin+7) = angles_bincounts(1:8); %get the eight orientation of each window
%figure;
%bar(angles_binranges,angles_bincounts,'histc');
end
end
%disp(length(B/norm(B)));
features(k,:) = B/norm(B);
end
%cut the end
power = 0.8;
features = features.^power;
end
function [matches, confidences] = match_features(features1, features2)
ratio = 0.8;
num_matches = 0;
num_features1 = size(features1,1);
num_features2 = size(features2,1);
matches = zeros(num_features1,2);
confidences = zeros(num_features1);
for i = 1:num_features1
distances = dist(features1(i,:),features2');
[dists,index] = sort(distances);
nndr = dists(1)/dists(2);
if nndr < ratio
%num_matches = num_matches + 1;
matches(i,:) = [i,index(1)];
confidences(i) = 1 - nndr;
% else
% confidences(i) = [];
% matches(i,:) = [];
end
end
%keep only those matched
matches_index = find(confidences>0);
matches = matches(matches_index,:);
confidences = confidences(matches_index);
% Sort the matches so that the most confident onces are at the top of the
% list. You should probably not delete this, so that the evaluation
% functions can be run on the top matches easily.
[confidences, ind] = sort(confidences, 'descend');
matches = matches(ind,:);
出来的效果如下: