Harris角检测
这里有篇原理讲解的,有兴趣自己看。https://www.cnblogs.com/Jack-Elvis/p/11640931.html
在opencv有专门的函数:
CV_EXPORTS_W void cornerHarris( InputArray src, OutputArray dst, int blockSize,
int ksize, double k,
int borderType = BORDER_DEFAULT );
The function runs the Harris corner detector on the image. Similarly to cornerMinEigenVal and
cornerEigenValsAndVecs , for each pixel \((x, y)\) it calculates a \(2\times2\) gradient covariance
matrix \(M^{(x,y)}\) over a \(\texttt{blockSize} \times \texttt{blockSize}\) neighborhood. Then, it
computes the following characteristic:
\[[\texttt{dst} (x,y) = \mathrm{det} M^{(x,y)} - k \cdot \left ( \mathrm{tr} M^{(x,y)} \right )^2]
\]
@param src Input single-channel 8-bit or floating-point image.
@param dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same
size as src .
@param blockSize Neighborhood size (see the details on #cornerEigenValsAndVecs ).
@param ksize Aperture parameter for the Sobel operator.
@param k Harris detector free parameter. See the formula above.
@param borderType Pixel extrapolation method. See #BorderTypes.
那么具体怎么使用分以下几个步骤:
- 将输入图片转成灰度
cvtColor(images, gray, COLOR_BGR2GRAY);
- 定义参数
double k = 0.04;
int blocksize = 2;
int ksize = 3;
- 调用具体函数
cornerHarris(gray,dst,blocksize,ksize,k);
4.对结果处理,归一化,再转成8bit
Mat dst_norm = Mat::zeros(dst.size(),dst.type());
normalize(dst, dst_norm, 0, 255, NORM_MINMAX, -1, Mat());
convertScaleAbs(dst_norm,dst_norm);
- 在原图绘制结果
for (int row = 0; row < images.rows; row++) {
for (int col = 0; col < images.cols;col++) {
int sp = dst_norm.at<uchar>(row, col);
if (sp > 182) {
circle(images, Point(col, row), 3, Scalar(0, 0, 255), 2, 8);
}
}
}
实验效果:
原图:
结果:
具体要绘制那些值,可以调节阀值。
注意 harris 角检测是比较慢的。