算法思想:如果一個像素與它鄰域的像素差別較大(過亮或過暗) , 那它更可能是角點。
算法步驟:
1.上圖所示,一個以像素p為中心,半徑為3的圓上,有16個像素點(p1、p2、...、p16)。
2.定義一個閾值。計算p1、p9與中心p的像素差,若它們絕對值都小於閾值,則p點不可能是特征點,直接pass掉;否則,當做候選點,有待進一步考察;
3.若p是候選點,則計算p1、p9、p5、p13與中心p的像素差,若它們的絕對值有至少3個超過閾值,則當做候選點,再進行下一步考察;否則,直接pass掉;
4.若p是候選點,則計算p1到p16這16個點與中心p的像素差,若它們有至少9個超過閾值,則是特征點;否則,直接pass掉。
5.對圖像進行非極大值抑制:計算特征點出的FAST得分值(即score值,也即s值),判斷以特征點p為中心的一個鄰域(如3x3或5x5)內,計算若有多個特征點,則判斷每個特征點的s值(16個點與中心差值的絕對值總和),若p是鄰域所有特征點中響應值最大的,則保留;否則,抑制。若鄰域內只有一個特征點(角點),則保留。
代碼如下:
clear all; close all; clc; img=imread('lena.jpg'); imshow(img) [m n]=size(img); score=zeros(m,n); t=60; %閾值 for i=4:m-3 for j=4:n-3 p=img(i,j); %步驟1,得到以p為中心的16個鄰域點 pn=[img(i-3,j) img(i-3,j+1) img(i-2,j+2) img(i-1,j+3) img(i,j+3) img(i+1,j+3) img(i+2,j+2) img(i+3,j+1) ... img(i+3,j) img(i+3,j-1) img(i+2,j-2) img(i+1,j-3) img(i,j-3) img(i-1,j-3) img(i-2,j-2) img(i-3,j-1)]; %步驟2 if abs(pn(1)-p)<t && abs(pn(9)-p)<t continue; end %步驟3 p1_5_9_13=[abs(pn(1)-p)>t abs(pn(5)-p)>t abs(pn(9)-p)>t abs(pn(13)-p)>t]; if sum(p1_5_9_13)>=3 ind=find(abs(pn-p)>t); %步驟4 if length(ind)>=9 score(i,j) = sum(abs(pn-p)); end end end end %步驟5,非極大抑制,並且畫出特征點 for i=4:m-3 for j=4:n-3 if score(i,j)~=0 if max(max(score(i-2:i+2,j-2:j+2)))==score(i,j) [img(i-3,j), img(i-3,j+1), img(i-2,j+2), img(i-1,j+3), img(i,j+3), img(i+1,j+3), img(i+2,j+2), img(i+3,j+1), ... img(i+3,j), img(i+3,j-1), img(i+2,j-2), img(i+1,j-3), img(i,j-3), img(i-1,j-3), img(i-2,j-2), img(i-3,j-1)]= ... deal(255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255); end end end end figure; imshow(img);
結果如下:
原圖:
檢測結果: