Harris Corner Detector & Shi-Tomasi Corner Detector

Harris Corner

  1.         // Capture the current frame
  2.         cap >> frame;
  3.  
  4.         // Resize the frame
  5.         resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
  6.  
  7.         dst = Mat::zeros(frame.size(), CV_32FC1);
  8.  
  9.         // Convert to grayscale
  10.         cvtColor(frame, frameGray, COLOR_BGR2GRAY );
  11.  
  12.         // Detecting corners
  13.         cornerHarris(frameGray, dst, blockSize, apertureSize, k, BORDER_DEFAULT);
  14.  
  15.         // Normalizing
  16.         normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
  17.         convertScaleAbs(dst_norm, dst_norm_scaled);
  18.  
  19.         // Drawing a circle around corners
  20.         for(int j = 0; j < dst_norm.rows ; j++)
  21.         {
  22.             for(int i = 0; i < dst_norm.cols; i++)
  23.             {
  24.                 if((int)dst_norm.at<float>(j,i) > thresh)
  25.                 {
  26.                     circle(frame, Point(i, j), 8,  Scalar(0,255,0), 2, 8, 0);
  27.                 }
  28.             }
  29.         }

Good Features To Track (Shi-Tomasi)

  1.         // Capture the current frame
  2.         cap >> frame;
  3.  
  4.         // Resize the frame
  5.         resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
  6.  
  7.         // Convert to grayscale
  8.         cvtColor(frame, frameGray, COLOR_BGR2GRAY );
  9.  
  10.         // Initialize the parameters for Shi-Tomasi algorithm
  11.         vector<Point2f> corners;
  12.         double qualityThreshold = 0.02;
  13.         double minDist = 15;
  14.         int blockSize = 5;
  15.         bool useHarrisDetector = false;
  16.         double k = 0.07;
  17.  
  18.         // Clone the input frame
  19.         Mat frameCopy;
  20.         frameCopy = frame.clone();
  21.  
  22.         // Apply corner detection
  23.         goodFeaturesToTrack(frameGray, corners, numCorners, qualityThreshold, minDist, Mat(), blockSize, useHarrisDetector, k);
  24.  
  25.         // Parameters for the circles to display the corners
  26.         int radius = 8;      // radius of the cirles
  27.         int thickness = 2;   // thickness of the circles
  28.         int lineType = 8;
  29.  
  30.         // Draw the detected corners using circles
  31.         for(size_t i = 0; i < corners.size(); i++)
  32.         {
  33.             Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
  34.             circle(frameCopy, corners[i], radius, color, thickness, lineType, 0);
  35.         }

Leave a Reply

Your email address will not be published. Required fields are marked *