Water's Home

Just another Life Style

0%

Harris Corner Detector & Shi-Tomasi Corner Detector

Harris Corner

    // Capture the current frame
    cap >> frame;
    
    // Resize the frame
    resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER\_AREA);
    
    dst = Mat::zeros(frame.size(), CV\_32FC1);
    
    // Convert to grayscale
    cvtColor(frame, frameGray, COLOR\_BGR2GRAY );
    
    // Detecting corners
    cornerHarris(frameGray, dst, blockSize, apertureSize, k, BORDER\_DEFAULT);
    
    // Normalizing
    normalize(dst, dst\_norm, 0, 255, NORM\_MINMAX, CV\_32FC1, Mat());
    convertScaleAbs(dst\_norm, dst\_norm\_scaled);
    
    // Drawing a circle around corners
    for(int j = 0; j < dst\_norm.rows ; j++)
    {
        for(int i = 0; i < dst\_norm.cols; i++)
        {
            if((int)dst\_norm.at(j,i) > thresh)
            {
                circle(frame, Point(i, j), 8,  Scalar(0,255,0), 2, 8, 0);
            }
        }
    } 

Good Features To Track (Shi-Tomasi)

    // Capture the current frame
    cap >> frame;
    
    // Resize the frame
    resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER\_AREA);
    
    // Convert to grayscale
    cvtColor(frame, frameGray, COLOR\_BGR2GRAY );
    
    // Initialize the parameters for Shi-Tomasi algorithm
    vector corners;
    double qualityThreshold = 0.02;
    double minDist = 15;
    int blockSize = 5;
    bool useHarrisDetector = false;
    double k = 0.07;
    
    // Clone the input frame
    Mat frameCopy;
    frameCopy = frame.clone();
    
    // Apply corner detection
    goodFeaturesToTrack(frameGray, corners, numCorners, qualityThreshold, minDist, Mat(), blockSize, useHarrisDetector, k);
    
    // Parameters for the circles to display the corners
    int radius = 8;      // radius of the cirles
    int thickness = 2;   // thickness of the circles
    int lineType = 8;
    
    // Draw the detected corners using circles
    for(size\_t i = 0; i < corners.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
        circle(frameCopy, corners\[i\], radius, color, thickness, lineType, 0);
    }