From e0c65499fd92ad6fe1f372896a8a87ecc8da9ff4 Mon Sep 17 00:00:00 2001 From: berak Date: Sat, 19 Mar 2016 09:55:18 +0100 Subject: [PATCH] remove opencv_contrib dependancy from planar_tracking.cpp --- .../akaze_tracking/akaze_tracking.markdown | 4 +- .../AKAZE_tracking/planar_tracking.cpp | 63 ++++++------------- .../features2D/AKAZE_tracking/utils.h | 55 ++++++++++++++++ 3 files changed, 77 insertions(+), 45 deletions(-) diff --git a/doc/tutorials/features2d/akaze_tracking/akaze_tracking.markdown b/doc/tutorials/features2d/akaze_tracking/akaze_tracking.markdown index 41bbfdd6b0..518cada76a 100644 --- a/doc/tutorials/features2d/akaze_tracking/akaze_tracking.markdown +++ b/doc/tutorials/features2d/akaze_tracking/akaze_tracking.markdown @@ -28,9 +28,9 @@ To do the tracking we need a video and object position on the first frame. You can download our example video and data from [here](https://docs.google.com/file/d/0B72G7D4snftJandBb0taLVJHMFk). -To run the code you have to specify input and output video path and object bounding box. +To run the code you have to specify input (camera id or video_file). Then, select a bounding box with the mouse, and press any key to start tracking @code{.none} -./planar_tracking blais.mp4 result.avi blais_bb.xml.gz +./planar_tracking blais.mp4 @endcode Source Code diff --git a/samples/cpp/tutorial_code/features2D/AKAZE_tracking/planar_tracking.cpp b/samples/cpp/tutorial_code/features2D/AKAZE_tracking/planar_tracking.cpp index c407c8611d..7a36e9f910 100755 --- a/samples/cpp/tutorial_code/features2D/AKAZE_tracking/planar_tracking.cpp +++ b/samples/cpp/tutorial_code/features2D/AKAZE_tracking/planar_tracking.cpp @@ -1,7 +1,6 @@ #include #include #include -#include //for ROI #include //for imshow #include #include @@ -121,43 +120,29 @@ Mat Tracker::process(const Mat frame, Stats& stats) int main(int argc, char **argv) { - if(argc < 3) { + if(argc < 2) { cerr << "Usage: " << endl - << "akaze_track input_path output_path [bounding_box_path]" << endl - << " (for camera input_path=N for camera N)" << endl; + << "akaze_track input_path" << endl + << " (input_path can be a camera id, like 0,1,2 or a video filename)" << endl; return 1; } std::string video_name = argv[1]; std::stringstream ssFormat; ssFormat << atoi(argv[1]); + VideoCapture video_in; - int iFourCC = 0, frame_count = 0; - if (video_name.compare(ssFormat.str())==0) { //test str==str(num) + if (video_name.compare(ssFormat.str())==0) { //test str==str(num) video_in.open(atoi(argv[1])); - cerr << "Capturing for 10 seconds from camera..." << endl; - iFourCC = CV_FOURCC('D', 'I', 'V', 'X'); //default to mp4 (sample) - frame_count = 10*static_cast(video_in.get(CAP_PROP_FPS)); } else { video_in.open(video_name); - iFourCC = static_cast(video_in.get(CAP_PROP_FOURCC)); - frame_count = static_cast(video_in.get(CAP_PROP_FRAME_COUNT)); } - VideoWriter video_out(argv[2], iFourCC, - (int)video_in.get(CAP_PROP_FPS), - Size(2 * (int)video_in.get(CAP_PROP_FRAME_WIDTH), - 2 * (int)video_in.get(CAP_PROP_FRAME_HEIGHT))); - if(!video_in.isOpened()) { cerr << "Couldn't open " << argv[1] << endl; return 1; } - if(!video_out.isOpened()) { - cerr << "Couldn't open " << argv[2] << endl; - return 1; - } Stats stats, akaze_stats, orb_stats; Ptr akaze = AKAZE::create(); @@ -169,33 +154,29 @@ int main(int argc, char **argv) Mat frame; video_in >> frame; + namedWindow(video_name, WINDOW_NORMAL); + cv::resizeWindow(video_name, frame.cols, frame.rows); + + cout << "Please select a bounding box, and press any key to continue." << endl; vector bb; - if (argc < 4) { //attempt to alow GUI selection - cv::Rect2d uBox = selectROI(video_name, frame); - bb.push_back(cv::Point2f(static_cast(uBox.x), static_cast(uBox.y))); - bb.push_back(cv::Point2f(static_cast(uBox.x+uBox.width), static_cast(uBox.y))); - bb.push_back(cv::Point2f(static_cast(uBox.x+uBox.width), static_cast(uBox.y+uBox.height))); - bb.push_back(cv::Point2f(static_cast(uBox.x), static_cast(uBox.y+uBox.height))); - } - else { - FileStorage fs(argv[3], FileStorage::READ); - if(fs["bounding_box"].empty()) { - cerr << "Couldn't read bounding_box from " << argv[3] << endl; - return 1; - } - fs["bounding_box"] >> bb; - } + cv::Rect2d uBox = selectROI(video_name, frame); + bb.push_back(cv::Point2f(static_cast(uBox.x), static_cast(uBox.y))); + bb.push_back(cv::Point2f(static_cast(uBox.x+uBox.width), static_cast(uBox.y))); + bb.push_back(cv::Point2f(static_cast(uBox.x+uBox.width), static_cast(uBox.y+uBox.height))); + bb.push_back(cv::Point2f(static_cast(uBox.x), static_cast(uBox.y+uBox.height))); + akaze_tracker.setFirstFrame(frame, bb, "AKAZE", stats); orb_tracker.setFirstFrame(frame, bb, "ORB", stats); Stats akaze_draw_stats, orb_draw_stats; Mat akaze_res, orb_res, res_frame; - int i = 1; - for(i = 1; i < frame_count; i++) { + int i = 0; + for(;;) { + i++; bool update_stats = (i % stats_update_period == 0); video_in >> frame; // stop the program if no more images - if(frame.rows==0 || frame.cols==0) break; + if(frame.empty()) break; akaze_res = akaze_tracker.process(frame, stats); akaze_stats += stats; @@ -213,12 +194,8 @@ int main(int argc, char **argv) drawStatistics(akaze_res, akaze_draw_stats); drawStatistics(orb_res, orb_draw_stats); vconcat(akaze_res, orb_res, res_frame); - video_out << res_frame; cv::imshow(video_name, res_frame); - if (i==1) //resize for easier display - cv::resizeWindow(video_name, frame.cols, frame.rows); - if(cv::waitKey(1)==27)break; //quit on ESC button - cout << i << "/" << frame_count - 1 << endl; + if(cv::waitKey(1)==27) break; //quit on ESC button } akaze_stats /= i - 1; orb_stats /= i - 1; diff --git a/samples/cpp/tutorial_code/features2D/AKAZE_tracking/utils.h b/samples/cpp/tutorial_code/features2D/AKAZE_tracking/utils.h index fbd897ec52..2142e988c0 100644 --- a/samples/cpp/tutorial_code/features2D/AKAZE_tracking/utils.h +++ b/samples/cpp/tutorial_code/features2D/AKAZE_tracking/utils.h @@ -12,6 +12,7 @@ void drawBoundingBox(Mat image, vector bb); void drawStatistics(Mat image, const Stats& stats); void printStatistics(string name, Stats stats); vector Points(vector keypoints); +Rect2d selectROI(const String &video_name, const Mat &frame); void drawBoundingBox(Mat image, vector bb) { @@ -56,4 +57,58 @@ vector Points(vector keypoints) return res; } +Rect2d selectROI(const String &video_name, const Mat &frame) +{ + struct Data + { + Point center; + Rect2d box; + + static void mouseHandler(int event, int x, int y, int flags, void *param) + { + Data *data = (Data*)param; + switch( event ) + { + // start to select the bounding box + case EVENT_LBUTTONDOWN: + data->box = cvRect( x, y, 0, 0 ); + data->center = Point2f((float)x,(float)y); + break; + // update the selected bounding box + case EVENT_MOUSEMOVE: + if(flags == 1) + { + data->box.width = 2 * (x - data->center.x); + data->box.height = 2 * (y - data->center.y); + data->box.x = data->center.x - data->box.width / 2.0; + data->box.y = data->center.y - data->box.height / 2.0; + } + break; + // cleaning up the selected bounding box + case EVENT_LBUTTONUP: + if( data->box.width < 0 ) + { + data->box.x += data->box.width; + data->box.width *= -1; + } + if( data->box.height < 0 ) + { + data->box.y += data->box.height; + data->box.height *= -1; + } + break; + } + } + } data; + + setMouseCallback(video_name, Data::mouseHandler, &data); + while(waitKey(1) < 0) + { + Mat draw = frame.clone(); + rectangle(draw, data.box, Scalar(255,0,0), 2, 1); + imshow(video_name, draw); + } + return data.box; +} + #endif // UTILS_H