OpenCV CornerHarris corner detection (C#)

Principle:
Use a fixed window to slide in any direction on the image. Compare the two situations before and after sliding. The degree of grayscale change of the pixels in the window. If there is sliding in any direction, there will be a large grayscale change. , then we can think that there are corner points in the window.

Insert image description here

API:

public static void CornerHarris(InputArray src, OutputArray dst, int blockSize, int ksize, double k, BorderTypes borderType = BorderTypes.Reflect101);

Corner detection CornerHarris function introduction:
1.src: must be a single-channel 8-bit or 32-bit floating point image
2.dst: stores the corner result image, its format is CV_32FC1, the image size is the same as the original image size
3.blockSize: The size of the scanning window, ps: The larger this value is, the more detection results will be detected.
4.ksize: The size of the Sobel operator, four-neighborhood, eight-neighborhood, etc. The larger this value is, the more detection results will be.
5.k: This value is a The empirical value generally only uses 0.04, and its value range is between 0.04~0.06

picFile = fileDialog.FileName;


Mat tempPic = new Mat(picFile, ImreadModes.Grayscale);
Mat srcPic = new Mat(picFile, ImreadModes.AnyColor);

Mat outPic = new Mat(tempPic.Size(), tempPic.Type());

//角点检测
Cv2.CornerHarris(tempPic, outPic, 2, 3, 0.04);

//归一化
Cv2.Normalize(outPic, outPic, 0, 255, NormTypes.MinMax);

//Mat 数据类型转换
outPic.ConvertTo(outPic, MatType.CV_8UC1);



picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(outPic);
picBox_Display.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(srcPic);

Insert image description here

Idea for drawing corner points: traverse the pixel values ​​of the CornerHarris output matrix, set a threshold, and the pixel position greater than the threshold can be considered a corner point

picFile = fileDialog.FileName;


Mat tempPic = new Mat(picFile, ImreadModes.Grayscale);
Mat srcPic = new Mat(picFile, ImreadModes.AnyColor);
Mat displayPic = srcPic.Clone();

Mat outPic = new Mat(tempPic.Size(), tempPic.Type());

//角点检测
Cv2.CornerHarris(tempPic, outPic, 2, 3, 0.04);

//归一化
Cv2.Normalize(outPic, outPic, 0, 255, NormTypes.MinMax);

//Mat 数据类型转换
outPic.ConvertTo(outPic, MatType.CV_8UC1);

int threshValue = 130;//角点阈值

//遍历输出矩阵
for (int i= 0; i < outPic.Rows; i++){
    
    

    for(int j = 0; j < outPic.Cols; j++)
    {
    
    
        var value = outPic.Get<byte>(i, j);

        if (value >= threshValue)
        {
    
    
            //绘制圆
            Point point = new Point(j, i);                           
            Cv2.Circle(displayPic,point, 10,Scalar.Red, 2);
        }



    }


}

picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(displayPic);
picBox_Display.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(srcPic);

Insert image description here
Additional notes:
The OpenCV library used in .NET in this case is OpenCvSharp4

OpenCv library for .NET environment

Guess you like

Origin blog.csdn.net/weixin_40671962/article/details/127894481