matlab特征点数量,特征点检测效果评估(matlab代码)
最近老師給我一個課題方向,要求研究特征提取的方法,找到提取出來的特征精度最高,這里的最高是指在多幀運動變化圖片里應(yīng)用此算法都能找到固定位置的特征,而且這個特征不限,可以是邊緣、角點或區(qū)域塊等,然后根據(jù)不同特征、其運動變化及圖像成像質(zhì)量提供不同的算法,以達到最優(yōu)。這個方向剛開始的確十分頭大,完全不知道從什么著手,是邊緣提取算法呢?還是先作目標(biāo)分割,大概想一兩天,決定先從特征點(包括角點)提取算法做起,先找到在特征點算法里非常優(yōu)秀的,然后結(jié)合各種背景(噪聲,運動變化)測試其變化。
這里主要的思路來自于一個仿射不變特征提取方法的檢測:http://www.robots.ox.ac.uk/~vgg/research/affine/
這個網(wǎng)站上面主要提供了一個仿射不變特征提取方法的評估,它提供了八組圖片,分別從噪聲、模糊、視點變化、縮放、旋轉(zhuǎn)、亮度變化來評估其仿射不變特征提取方法的效果。不過它主要是對相似區(qū)域進行提取,涉及到橢圓區(qū)域的仿射變換,而我主要進行點特征點提取的評估,要稍微簡單點,當(dāng)然也借鑒了其代碼(這里面只有評估代碼,沒有特征提取算法的代碼,不過提供了一系列算法的執(zhí)行文件)
我首先要測試的sift算法對視點變化的評估,從上面的網(wǎng)站里下載了幾個圖片【img1.ppm、img2.ppm】、變化矩陣【H1to2p】及特征點提取文件【img1.haraff.sift】,我主要從特征向量匹配程度和實際匹配的位置進行比較。我的代碼如下:
function [matchFeaIndex, matchFeaRatio,matchLocal1]=matchPointFea(file1,file2,H,image1,image2)
% 主要用特征點匹配效果測試:
% 用法:[matchFeaIndex, matchFeaRatio,matchLocal1]=matchPointFea('img1.haraff.sift','img2.haraff.sift','H1to2p','img1.ppm','img2.ppm');
% 輸入:
% file1 - 圖像1特征點檢測文件
% file2 - 圖像2特征點檢測文件
% H - 圖像1到圖像2的仿射變換矩陣
% image1 - 圖像1
% image2 - 圖像2
% 輸出:
% matchFeaIndex - 保存圖像1中同圖像2匹配的特征點在file2中的標(biāo)號,若不匹配則為0。
% matchFeaRatio - 對應(yīng)于matchIndex,保存特征點匹配程度,不匹配則為0。
% matchLocal1 - 保存圖像2中特征點位置同經(jīng)過仿射變換1之間的位置差別
% matchLocal2 - 保存圖像1中特征點位置同經(jīng)過仿射變換2之間的位置差別(保留)
% 特征點檢測文件格式:
% file1, file2:
% x1 y1 a1 b1 c1 c2 c3 ...
% x2 y2 a2 b2 c1 c2 c3 ...
%---------------------
% 第一行 保存描述子數(shù)目及大小
% x, y - 特征點位置
% a, b - 特征點大小及方向(主要針對于sift變換)
% d1 d2 d3 ... - 特征描述子向量(如果小于,則無效)
close all;
% loc存放位置及大小角度,des存放特征描述子向量,dimdesc存放描述子大小及數(shù)目
[des1, loc1, dimdesc1]=loadFeatures(file1);
[des2, loc2, dimdesc2]=loadFeatures(file2);
% 導(dǎo)入圖像
Im1=imread(image1);
Im2=imread(image2);
% 導(dǎo)入仿射矩陣文件
H=load(H);
fprintf(1,'numbers and length of descriptor in file1 %d %d\n',dimdesc1(1),dimdesc1(2));
fprintf(1,'numbers and length of descriptor in file2 %d %d\n',dimdesc2(1),dimdesc2(2));
if dimdesc1(2)>1 && dimdesc1(2)==dimdesc2(2)
fprintf(1,'%s, %s look like files with descriptors...\n',file1,file2);
else
error('Different descriptor dimension in %s or %s files.',file1,file2);
end
% 計算兩個特征向量的匹配程度可以通過向量空間余弦相似度來衡量.
% 設(shè)置比值distRatio,保證所匹配的特征點具有顯著相似度,即與第二相似度有較大差異.
distRatio = 0.6;
% 在圖像2中找到圖像1中每一個匹配點.
des2t = des2'; % 轉(zhuǎn)置
desNum = dimdesc1(1); % 圖像1特征點個數(shù)
matchFeaIndex=zeros(desNum,1);
matchFeaRatio=zeros(desNum,1);
matchNum=0;
for i = 1 : desNum
dotprods = des1(i,:) * des2t; % 計算乘積項
[vals,indx] = sort(acos(dotprods)); % 排序余弦相似度
% 找到最大余弦相似度.
if (vals(1) < distRatio * vals(2))
matchFeaIndex(i) = indx(1);
matchFeaRatio(i) = vals(1);
matchNum=matchNum+1;
else
matchFeaIndex(i) = 0;
end
end
fprintf(1,'numbers of match descriptor is %d \n',matchNum);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 確定圖像1中特征點變換到圖像2中的位置,及圖像2變換到圖像1中的位置
% 要進行仿射變換測試,要求圖像1和2必須大小相同
if size(Im1)==size(Im2)
HI=H(:, 1:3); %圖1到圖2
H=inv(HI); %圖2到圖1
fprintf(1,'Projecting 1 to 2...\n');
loc1t=projectPoints(loc1',HI);
loc1t=loc1t';
fprintf(1,'and 2 to 1...\n');
loc2t=projectPoints(loc2',H);
loc2t=loc2t';
ImageSize=size(Im1);
matchLocal1=matchLocal(loc1t,loc2,ImageSize,matchFeaIndex);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 畫圖
% showkeys(image1,loc1); % 特征點顯示
% showkeys(image2,loc2);
% 顯示匹配圖像過程
Im=appendimages(Im1,Im2);
% Show a figure with lines joining the accepted matches.
figure('Position', [0 0 size(Im,2) size(Im,1)]);
colormap('gray');
imagesc(Im);
hold on;
cols1 = size(Im1,2);
for i = 1: size(des1,1)
if (matchFeaIndex(i) > 0 && matchLocal1(i)<5)
line([loc1(i,1) loc2(matchFeaIndex(i),1)+cols1], ...
[loc1(i,2) loc2(matchFeaIndex(i),2)], 'Color', 'c'); %畫對應(yīng)線條
plot(loc1(i,1),loc1(i,2),'r.'); %畫特征點
plot(loc2(matchFeaIndex(i),1)+cols1,loc2(matchFeaIndex(i),2),'g.'); %畫特征點
end
end
hold off;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 點的投影運算
function loct=projectPoints(loc,H)
num=size(loc(1)); %特征點數(shù)目
loct=loc;
for i=1:num
l1=[loc(i,1),loc(i,2),1];
l1_2=H*l1';
l1_2=l1_2/l1_2(3);
loct(i,1)=l1_2(1);
loct(i,2)=l1_2(2);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 加載描述子文件
function [des, loc, dimdes]=loadFeatures(file)
fid = fopen(file, 'r');
[dimdes, count]=fscanf(fid, '%d %d',[1 2]);
if count ~= 2
error('Invalid keypoint file beginning.');
end
num = dimdes(1); %描述子數(shù)目
len = dimdes(2); %描述子長度
loc = double(zeros(num, 4));
des = double(zeros(num, len));
% 將描述子導(dǎo)入向量
for i = 1:num
[vector, count] = fscanf(fid, '%f %f %f %f', [1 len+5]); %row col scale ori
if count ~= (5+len)
error('Invalid keypoint file format');
end
loc(i, :) = vector(1, 1:4);
descrip(1, :) = vector(1, 6:len+5);
descrip = descrip / sqrt(sum(descrip.^2));
des(i, :) = descrip(1, :);
end
fclose(fid);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 計算實際特征匹配的偏差位置
function matchLoc=matchLocal(loct,loc,ImageSize,matchFeaIndex)
matchLoc=ones(size(matchFeaIndex));
for i=1:size(loct)
if matchFeaIndex(i)~=0 %找到匹配的點
%注意仿射變換,位置可能超過圖像范圍
if loct(i,1)>0&&loct(i,1)0&&loct(i,2)
&&loc(matchFeaIndex(i),1)>0&&loc(matchFeaIndex(i),1)0&&loc(matchFeaIndex(i),2)
matchLoc(i)=(loct(i,1)-loc(matchFeaIndex(i),1))^2+(loct(i,2)-loc(matchFeaIndex(i),2))^2;
matchLoc(i)=sqrt(matchLoc(i)); %這個偏差即是歐氏距離
end
end
end
matchLoc=100.*matchLoc./max(matchLoc); %計算百分比
end
最后效果圖,這里我稍微減少了匹配的特征點個數(shù)(原本匹配的點有300多個>_
總結(jié)
以上是生活随笔為你收集整理的matlab特征点数量,特征点检测效果评估(matlab代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国六排放标准影响有多大?
- 下一篇: matlab删失数据威布尔,基于混合I型