opencv学习-Harris角点检测
Posted 殇堼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv学习-Harris角点检测相关的知识,希望对你有一定的参考价值。
一、理论
二、API
三、部分代码-无滑动条
3.1部分代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src, gray_src, dst, norm_dst;
src = imread("D:/images/lena.png");
if (src.empty())
{
cout << "图片为空" << endl;
return -1;
}
imshow("input title", src);
cvtColor(src, gray_src, COLOR_BGR2GRAY);
int thres_value = 130;
dst = Mat::zeros(gray_src.size(), CV_32FC1);
cornerHarris(gray_src, dst, 2, 3, 0.04, BORDER_DEFAULT);
normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(norm_dst, norm_dst);
Mat resultImg = src.clone();
for (int row = 0; row < resultImg.rows; row++)
{
uchar* currentRow = norm_dst.ptr(row);
for (int col = 0; col < resultImg.cols; col++)
{
int value = (int)*currentRow;
if (value > thres_value)
{
circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0);
}
currentRow++;
}
}
imshow("outputTitle", resultImg);
waitKey(0);
return 0;
}
3.2效果展示
四、全部代码-有滑动条
4.1全部代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int thres_value = 130;
Mat src, gray_src, dst, norm_dst;
void Harris_demo(int, void *)
{
dst = Mat::zeros(gray_src.size(), CV_32FC1);
cornerHarris(gray_src, dst, 2, 3, 0.04, BORDER_DEFAULT);
normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(norm_dst, norm_dst);
Mat resultImg = src.clone();
for (int row = 0; row < resultImg.rows; row++)
{
uchar* currentRow = norm_dst.ptr(row);
for (int col = 0; col < resultImg.cols; col++)
{
int value = (int)*currentRow;
if (value > thres_value)
{
circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0);
}
currentRow++;
}
}
imshow("outputTitle", resultImg);
}
int main()
{
src = imread("D:/images/lena.png");
if (src.empty())
{
cout << "图片为空" << endl;
return -1;
}
imshow("input title", src);
cvtColor(src, gray_src, COLOR_BGR2GRAY);
Harris_demo(0, 0);
createTrackbar("Harris", "outputTitle", &thres_value, 255, Harris_demo);
waitKey(0);
return 0;
}
4.3效果展示
以上是关于opencv学习-Harris角点检测的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV Feature Detection and Description -- Harris Corner Detection Harris角点检测
OpenCV 例程300篇238. OpenCV 中的 Harris 角点检测