Corner detection

Harris Corner Detection

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
 Mat img = imread("./2.png", 1);
 Mat dst, dst_norm, dst_norm_scaled, img_grey;
 cvtColor(img, img_grey, CV_BGR2GRAY);

 int blockSize = 2;
 int apertureSize = 3;
 double k = 0.04;
 cornerHarris(img_grey, dst, blockSize, apertureSize, k, BORDER_DEFAULT);
 
 /// Normalizing
 normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
 convertScaleAbs(dst_norm, dst_norm_scaled);
 int thresh = 180;
 /// 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(img, Point(i, j), 5, Scalar(0,0,255), 2, 8, 0);
    circle(dst_norm_scaled, Point(i, j), 5, Scalar(0), 2, 8, 0);
   }
  }
 }
 imshow("Image", img);
 imshow("Image Harris", dst_norm_scaled);
 waitKey(0);
 return 0;
}

Można również użyć kodu:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
 Mat img = imread("./2.png", 1);
 Mat dst, img_display, img_grey;
 cvtColor(img, img_grey, CV_BGR2GRAY);

 GoodFeaturesToTrackDetector harris_detector(1000, 0.01, 10, 3, true);
 vector<KeyPoint> keypoints;
 harris_detector.detect(img_grey, keypoints);
 drawKeypoints(img, keypoints, img_display, Scalar(0, 0, 255));
 imshow("Image Harris", img_display);
 waitKey(0);
 return 0;
}

FAST Corner Detection

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
 Mat img = imread("./2.png", 1);
 Mat dst, img_display, img_grey;
 cvtColor(img, img_grey, CV_BGR2GRAY);
 vector<KeyPoint> keypoints;
 FASTX(img_grey, keypoints, 50, true, FastFeatureDetector::TYPE_9_16);
 drawKeypoints(img, keypoints, img_display, Scalar(0, 0, 255));
 imshow("Image Harris", img_display);
 waitKey(0);
 return 0;
}