Created
October 25, 2015 00:21
-
-
Save clynamen/8a079d93bc68abd6ce21 to your computer and use it in GitHub Desktop.
This file contains 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
#include <iostream> | |
#include "opencv2/highgui/highgui.hpp" | |
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/core/core.hpp" | |
#include "opencv2/features2d/features2d.hpp" | |
#include "opencv2/nonfree/nonfree.hpp" | |
using namespace std; | |
using namespace cv; | |
// credits to | |
// https://jayrambhia.wordpress.com/2012/09/24/sift-based-tracker/ | |
// http://www.jayrambhia.com/blog/selecting-roibb-in-opencvmat/ | |
Point point1, point2; | |
int drag = 0; | |
Rect rect; | |
Mat img, roiImg; | |
int select_flag = 0; | |
vector<KeyPoint> keypoints_roi, keypoints_img; | |
Mat descriptor_roi, descriptor_img; | |
SIFT sift; | |
void mouseHandler(int event, int x, int y, int flags, void* param) | |
{ | |
if (event == CV_EVENT_LBUTTONDOWN && !drag) | |
{ | |
point1 = Point(x, y); | |
drag = 1; | |
} | |
if (event == CV_EVENT_MOUSEMOVE && drag) | |
{ | |
Mat img1 = img.clone(); | |
point2 = Point(x, y); | |
rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0); | |
imshow("image", img1); | |
} | |
if (event == CV_EVENT_LBUTTONUP && drag) | |
{ | |
point2 = Point(x, y); | |
int width = abs(x-point1.x); | |
int height = abs(y-point1.y); | |
Point begin = point1; | |
if(point1.x>point2.x) { | |
begin = point2; | |
} | |
if(begin.x + width >= img.size().width) { | |
width = img.size().width-begin.x; | |
} | |
if(begin.y + height >= img.size().height) { | |
height = img.size().height-begin.y; | |
} | |
rect = Rect(begin.x, begin.y, | |
width, height); | |
drag = 0; | |
roiImg = img(rect); | |
roiImg = roiImg.clone(); | |
sift(roiImg, Mat(), keypoints_roi, descriptor_roi); | |
drawKeypoints(roiImg, keypoints_roi, roiImg, | |
Scalar::all(-1), | |
DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS & DrawMatchesFlags::DRAW_RICH_KEYPOINTS); | |
} | |
if (event == CV_EVENT_LBUTTONUP) | |
{ | |
/* ROI selected */ | |
select_flag = 1; | |
drag = 0; | |
} | |
} | |
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("image", CV_WINDOW_AUTOSIZE); | |
namedWindow("ROI", CV_WINDOW_AUTOSIZE); | |
while(true) { | |
Mat descriptors; | |
vector<KeyPoint> keypoints; | |
cap.read(img); | |
cvSetMouseCallback("image", mouseHandler, NULL); | |
if (select_flag == 1) | |
{ | |
imshow("ROI", roiImg); /* show the image bounded by the box */ | |
sift(img, Mat(), keypoints_img, descriptor_img); | |
FlannBasedMatcher matcher; | |
vector<DMatch> matches; | |
matcher.match(descriptor_roi, descriptor_img, matches); | |
vector<DMatch> good_matches; | |
double min = 200.0; | |
for (int i=0; i<descriptor_roi.rows; i++) | |
{ | |
if(matches[i].distance < min) | |
{ | |
good_matches.push_back(matches[i]); | |
} | |
} | |
vector<KeyPoint> keypoints1; | |
for (int i=0; i<good_matches.size(); i++) | |
{ | |
keypoints1.push_back(keypoints_img[good_matches[i].trainIdx]); | |
} | |
drawKeypoints(img, keypoints1, img, | |
Scalar::all(-1), | |
DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS & DrawMatchesFlags::DRAW_RICH_KEYPOINTS); | |
} | |
imshow("image", img); | |
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