Harris算法
#include<opencv2/opencv.hpp>
#include<time.h>
#include<sys/time.h>
using namespace cv;
using namespace std;
int thresh = 53;
Mat src, dst, norm_dst, gray_img, abs_dst, out1, out2;
void callback(int, void*);
int main(int arc, char** argv)
{
struct timeval t1,t2;
src = imread("cloned1.png");
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
cvtColor(src, gray_img, CV_BGR2GRAY);
namedWindow("output", CV_WINDOW_AUTOSIZE);
gettimeofday(&t1,NULL);
cout <<t1.tv_usec << endl;
createTrackbar("threshold", "output", &thresh, 255, callback);
gettimeofday(&t2,NULL);
cout << t2.tv_usec << endl;
cout <<t2.tv_usec-t1.tv_usec << endl;
callback(0, 0);
waitKey(0);
return 0;
}
void callback(int, void*) {
dst = Mat::zeros(gray_img.size(), CV_32FC1);
out1 = Mat::zeros(gray_img.size(), CV_32FC1);
cornerHarris(gray_img, dst, 2, 3, 0.04);
normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(norm_dst, abs_dst);//?
Mat result_img = src.clone();
for (int i = 0; i < result_img.rows; i++) {
for (int j = 0; j < result_img.cols; j++) {
uchar value = abs_dst.at<uchar>(i, j);
if (value > thresh) {
circle(result_img, Point(j, i), 1, Scalar(0, 255, 0), 2);
}
}
}
imshow("output", result_img);
}
Fast算法
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/core/core.hpp>
#include <cv.h>
#include <sys/time.h>
using namespace std;
using namespace cv;
int main() {
vector<cv::KeyPoint> keypoints;
Mat image= cv::imread("cloned2.png");
keypoints.clear();
FastFeatureDetector fast(10);
fast.detect(image,keypoints);
imshow("FAST特征点", image);
struct timeval t1,t2;
gettimeofday(&t1,NULL);
cout<<t1.tv_usec<<endl;
drawKeypoints(image,keypoints,image,cv::Scalar(0,255,0));
gettimeofday(&t2,NULL);
cout<<t2.tv_usec<<endl;
cout<<t2.tv_usec-t1.tv_usec<<endl;
imshow("FAST特征点", image);
imwrite("c_2.png",image);
cvWaitKey(0);
}
编译运行:g++ fast.cpp -o fast `pkg-config --cflags --libs opencv`