Skip to content

Instantly share code, notes, and snippets.

@clynamen
Created October 25, 2015 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clynamen/09d885ab70e70783c8c8 to your computer and use it in GitHub Desktop.
Save clynamen/09d885ab70e70783c8c8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
VideoCapture cap(0);
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
namedWindow("corners", CV_WINDOW_AUTOSIZE);
namedWindow("harris", CV_WINDOW_AUTOSIZE);
namedWindow("Control");
int harrisThreshold = 200;
int blockSize = 2;
int ksize = 3;
int k = 4;
createTrackbar("harris threshold", "Control", &harrisThreshold, 255);
createTrackbar("blockSize", "Control", &blockSize, 20);
createTrackbar("kernel size", "Control", &ksize, 30);
createTrackbar("k", "Control", &k, 100);
while(true) {
Mat image;
cap.read(image);
Mat grayImage;
cvtColor(image, grayImage, CV_BGR2GRAY);
Mat harrisImage;
if (ksize % 2 == 0) {
ksize--;
}
cornerHarris(grayImage, harrisImage, max(blockSize, 2),
max(min(ksize, 31), 1),
k/100);
Mat normalized;
normalize(harrisImage, normalized, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
imshow("corners", normalized);
/// Drawing a circle around corners
int max = 50;
int count = 0;
for( int j = 0; j < normalized.rows ; j++ ) {
for( int i = 0; i < normalized.cols && count < max; i++ ) {
if( (int) normalized.at<float>(j,i) > harrisThreshold ) {
circle(image, Point( i, j ), 8, Scalar(0, 0, 0xff), 1, 1, 0 );
count++;
}
}
}
imshow("harris", image);
if (waitKey(30) == 27) {
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment