Last active
November 28, 2017 08:31
-
-
Save six519/c1602a03a5cc340daf02512754c555b3 to your computer and use it in GitHub Desktop.
OpenCV Object Tracking Using Kernelized Correlation Filters C++ Sample Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To compile: g++ opencv_test.cpp -o opencv_test $(pkg-config --cflags --libs opencv) | |
*/ | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/tracking.hpp> | |
#include <opencv2/core/ocl.hpp> | |
#include <unistd.h> | |
#define WINDOW_NAME "Camera" | |
using namespace cv; | |
using namespace std; | |
int main(int argc, char **argv) { | |
Ptr<Tracker> objectTracker; | |
Scalar color = Scalar(0, 165, 255); //Orange | |
objectTracker = TrackerKCF::create(); //KCF tracker | |
VideoCapture camera(0); //open camera | |
//set the video size to 640x480 to process faster | |
camera.set(3, 640); | |
camera.set(4, 480); | |
sleep(3); | |
Mat frame; | |
camera.read(frame); | |
Rect2d rect; | |
rect = selectROI(WINDOW_NAME, frame, false); | |
rectangle(frame, rect, color, 1); | |
imshow(WINDOW_NAME, frame); | |
objectTracker->init(frame, rect); | |
while(camera.read(frame)) { | |
if (objectTracker->update(frame, rect)) { | |
rectangle(frame, rect, color, 1); | |
} | |
imshow(WINDOW_NAME, frame); | |
if(waitKey(1) == 27){ | |
//exit if ESC is pressed | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment