fast算法是由 ed Rosten,tom drummond 等提出的。算法的主页:
http://www.edwardrosten.com/work/fast.html
论文下载地址:
http://www.edwardrosten.com/work/papers.html (2006年部分)
比较好的中文介绍的博客:
http://blog.csdn.net/yang_xian521/article/details/7411438
下面介绍一下我对fast算法的理解:
fast是 Features from accelerated segment Test的缩写,它对feature的定义是:如果在以r为半径,以像素p为圆心的Bresenham圆上有连续的>=N个像素大于(或者小于)像素p+-设定的阈值t,那么这个p像素所在的位置就有一个feature。在opencv函数中FastFeatureDetector(t)中的t就是这里所说的阈值。
紧接着它讨论了直接计算的劣势,包括对N的讨论,计算量大,
它又借助ID3算法对算法进行改进,但是我对id3算法不是很了解,所以null。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
结果证明当N= 9,r=3时仅仅需要2.26个question当N=9时是效率最高的,也就是fast-9算法。。
下面贴一下我从上面的博客地址上出现的代码,在我的机器上跑了一下,完全可以。:
#include<windows.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace std;
using namespace cv;
int main()
{
cv::Mat img;
img = cv::imread("hello.jpg");
vector<cv::KeyPoint> keyPoints;
cv::FastFeatureDetector fast(40);
double freq =1;// ::getTickFrequency();
double start = GetTickCount();
fast.detect(img,keyPoints);
double end = GetTickCount();
double time = (end-start);
std::cout<<time<<std::endl;
drawKeypoints(img,keyPoints,img,Scalar::all(255),1);
imshow("fast",img);
waitKey();
return 0;
}
运行后图片发现时间为16ms。
2.程序直接读取摄像头,并让fast算法运行灰度图像像代码:
#include<windows.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace std;
using namespace cv;
int main()
{
cv::VideoCapture cap(0);
if(!cap.isOpened()){
return -1;
}
Mat frame;
Mat gray;
vector<cv::KeyPoint> keyPoints;
cv::FastFeatureDetector fast(40);
while(1)
{
cap>>frame;
cvtColor(frame,gray,CV_BGR2GRAY);
double start = GetTickCount();
fast.detect(gray,keyPoints);
double end = GetTickCount();
std::cout<<end-start<<std::endl;
drawKeypoints(gray,keyPoints,gray,Scalar::all(255));
imshow("hello",gray);
imshow("original",frame);
if(waitKey(2)>=0)
{
break;
}
}
}
时间大部分出现“0”,说明速度的确很快,第一个程序主要费时在把rgb图像转换成灰度图像上了。
代码:使用surf算法实现匹配
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include<opencv/cv.h>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
char img_filename1[]="hello.jpg";
char img_filename2[]="hello.jpg";
void main()
{
Mat img=imread("hello.jpg");
Mat imgCopy= img;
Mat imgGray;
cvtColor(img,imgGray,CV_BGR2GRAY);
Mat frame;
cv::VideoCapture cap(0) ;
if(!cap.isOpened())
{
exit(1);
}
vector<KeyPoint> keysImg;
vector<KeyPoint> keysFrame;
FastFeatureDetector fast(45);
fast.detect(img,keysImg);
SurfDescriptorExtractor extractor;
Mat descriptorsImg;
Mat descriptorsFrame;
extractor.compute(img,keysImg,descriptorsImg);
FlannBasedMatcher matcher;
vector<DMatch> matches ;
Mat imgMatch;
while(1)
{
cap>>frame;
fast.detect(frame,keysFrame);
if( keysFrame.empty()) //避免出现cv::exception ();
continue;
extractor.compute(frame,keysFrame,descriptorsFrame);
matcher.match(descriptorsImg,descriptorsFrame,matches);
drawMatches(img,keysImg,frame,keysFrame,matches,imgMatch,Scalar::all(-1),
Scalar::all(-1)
,vector<char>()
,DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS
);
imshow("match",imgMatch);
if( waitKey(2)>=0)
break;
}//while(1);endl;
}