Opencv Harris和FAST角点检测算法

本文介绍并演示了Harris角点检测算法与FAST特征检测算法在图像处理中的应用。通过OpenCV库,实现了从读取图片到检测角点或特征点的全过程,并展示了动态调整阈值对检测结果的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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`

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值