-
-
Save anonymous/5606da4e3e37380ed5e5 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
VideoCapture videoInput; | |
videoInput.open(this->media_file_path); | |
if (!videoInput.isOpened()) { | |
cout << "Could not open video " << this->media_file_path << endl; | |
return; | |
} | |
if (this->logos.size()==0){ | |
cout << "No logos were loaded." << endl; | |
return; | |
} | |
Size S = Size((int) videoInput.get(CV_CAP_PROP_FRAME_WIDTH), // Acquire input size | |
(int) videoInput.get(CV_CAP_PROP_FRAME_HEIGHT)); | |
VideoWriter videoOutput; | |
videoOutput.open("output.avi", CV_FOURCC('M','J','P','G'), videoInput.get(CV_CAP_PROP_FPS), S, true); | |
if (!videoOutput.isOpened()) | |
{ | |
cout << "Could not open the output video for write." << endl; | |
return; | |
} | |
cout << "\nNow detecting logos in " << this->media_file_path << endl; | |
int nFrames = 0; | |
Mat frame_rgb, frame_gray; | |
char c; | |
namedWindow("Video", WINDOW_AUTOSIZE); | |
int nfeatures = 0; | |
int nOctaveLayers = 4; | |
double contrastThreshold=0.1; | |
double edgeThreshold=5; | |
double sigma=1.2; | |
Ptr<SIFT> detector = SIFT::create( nfeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma ); | |
vector<KeyPoint> scene_keypoints; | |
Mat scene_descriptors; | |
map<string,vector<KeyPoint>> logo_keypoints; | |
map<string, Mat> logo_descriptors; | |
//Ptr<FlannBasedMatcher> matcher = new FlannBasedMatcher(); | |
Ptr<BFMatcher> matcher = new BFMatcher(); | |
for (auto const &it : this->logos) { | |
detector->detectAndCompute( it.second, Mat(), logo_keypoints[it.first], logo_descriptors[it.first] ); | |
} | |
while (videoInput.read(frame_rgb)){ | |
cvtColor(frame_rgb, frame_gray, CV_RGB2GRAY); | |
try { | |
detector->detectAndCompute( frame_gray, Mat(), scene_keypoints, scene_descriptors ); | |
if (!scene_descriptors.empty()){ | |
for(auto const &it : logo_descriptors) { | |
vector< vector< DMatch > > matches; | |
matcher->knnMatch( it.second, scene_descriptors, matches, 2, noArray(), false); | |
vector< DMatch > good_matches; | |
double ratio; | |
for( int i=0; i<matches.size(); i++){ | |
ratio = matches[i][0].distance/matches[i][1].distance; | |
if (ratio<0.80){ | |
good_matches.push_back( matches[i][0] ); | |
} | |
} | |
// Get good match keypoint locations, for plotting | |
vector<KeyPoint> kps; | |
if (good_matches.size()>0){ | |
for (int i=0; i<good_matches.size(); i++){ | |
kps.push_back( scene_keypoints[good_matches[i].trainIdx] ); | |
} | |
} | |
drawKeypoints(frame_rgb, kps, frame_rgb, Scalar(0,255,0)); | |
if(good_matches.size()>=7){ | |
vector<Point2f> obj, scene; | |
for( int i = 0; i < good_matches.size(); i++ ) | |
{ | |
//-- Get the keypoints from the good matches | |
obj.push_back( logo_keypoints[it.first][ good_matches[i].queryIdx ].pt ); | |
scene.push_back( scene_keypoints[ good_matches[i].trainIdx ].pt ); | |
} | |
vector<Point2f> obj_corners(4); | |
obj_corners[0] = Point2f(0,0); | |
obj_corners[1] = Point2f( it.second.cols, 0 ); | |
obj_corners[2] = Point2f( it.second.cols, it.second.rows ); | |
obj_corners[3] = Point2f( 0, it.second.rows ); | |
vector<Point2f> scene_corners(4); | |
Mat inlierMask; | |
Mat H = findHomography( obj, scene, RANSAC , 7.0, inlierMask); | |
if (sum(inlierMask)[0]>=5){ | |
perspectiveTransform( obj_corners, scene_corners, H); | |
//Draw lines between the corners (the mapped object in the scene image ) | |
putText(frame_rgb, it.first, scene_corners[2], FONT_HERSHEY_COMPLEX_SMALL, 1, cvScalar(0,255,255), 1, CV_AA); | |
line( frame_rgb, scene_corners[0], scene_corners[1], Scalar(0, 255, 0), 4 ); | |
line( frame_rgb, scene_corners[1], scene_corners[2], Scalar( 0, 255, 0), 4 ); | |
line( frame_rgb, scene_corners[2], scene_corners[3], Scalar( 0, 255, 0), 4 ); | |
line( frame_rgb, scene_corners[3], scene_corners[0], Scalar( 0, 255, 0), 4 ); | |
} | |
} | |
} | |
} | |
} catch(...) { | |
continue; | |
} | |
nFrames+=1; | |
imshow("Video", frame_rgb); | |
videoOutput.write(frame_rgb); | |
c = (char)waitKey(1); | |
if (c == 27) break; // ESC to stop | |
} | |
cout << nFrames << " frames processed." << endl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment