Harris角点检测实现

参考:Harris角点检测python实现及基于opencv实现

/**
 * @description:	Harris角点检测
 * @param src		输入图像
 * @param dst		输出图像
 * @param block_size角点检测中要考虑的领域大小
 * @param ksize		求导中使用的窗口大小
 * @param k			角点检测方程中的自由参数
 */
void harris(cv::Mat& src, cv::Mat& dst, int block_size, int ksize, float k)
{
	dst = src.clone();
	src.convertTo(src, CV_32F);

	double scale = 1.0 / ((ksize - 1) * 2 * block_size * 255);

	cv::Mat img_x, img_y;
	cv::Sobel(src, img_x, -1, 1, 0, ksize, scale);
	cv::Sobel(src, img_y, -1, 0, 1, ksize, scale);

	cv::Mat img_xx, img_yy, img_xy;
	cv::boxFilter(img_x.mul(img_x), img_xx, -1, cv::Size(block_size, block_size));
	cv::boxFilter(img_y.mul(img_y), img_yy, -1, cv::Size(block_size, block_size));
	cv::boxFilter(img_x.mul(img_y), img_xy, -1, cv::Size(block_size, block_size));

	int radius = int((block_size - 1) / 2);
	int N_pre = radius;
	int N_post = block_size - radius - 1;
	int row_s = N_pre;
	int col_s = N_pre;
	int row_e = src.rows - N_post;
	int col_e = src.cols - N_post;

	cv::Mat cim = cv::Mat::zeros(src.rows, src.cols, CV_32F);
	for (int i = row_s; i < row_e; ++i)
	{
		for (int j = col_s; j < col_e; ++j)
		{
			float sum_xx = img_xx.at<float>(i, j);
			float sum_yy = img_yy.at<float>(i, j);
			float sum_xy = img_xy.at<float>(i, j);

			float det = sum_xx * sum_yy - sum_xy * sum_xy;
			float trace = sum_xx + sum_yy;
			float res = det - k * trace * trace;
			cim.at<float>(i, j) = res;
		}
	}

	double minv, maxv;
	cv::Point pt_min, pt_max;
	cv::minMaxLoc(cim, &minv, &maxv, &pt_min, &pt_max);
	std::cout << maxv << std::endl;

	cv::cvtColor(dst, dst, cv::COLOR_GRAY2BGR);

	int num = 0;
	for (int i = 0; i < cim.rows; ++i)
	{
		for (int j = 0; j < cim.cols; ++j)
		{
			if (cim.at<float>(i, j) > 0.01* maxv)
			{
				dst.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 0, 255);
				num++;
			}
		}
	}
	std::cout << num << std::endl;
}

代码传送门:https://github.com/taifyang/OpenCV-algorithm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值