remove opencv_contrib dependancy from planar_tracking.cpp
authorberak <px1704@web.de>
Sat, 19 Mar 2016 08:55:18 +0000 (09:55 +0100)
committerberak <px1704@web.de>
Sat, 19 Mar 2016 09:29:22 +0000 (10:29 +0100)
doc/tutorials/features2d/akaze_tracking/akaze_tracking.markdown
samples/cpp/tutorial_code/features2D/AKAZE_tracking/planar_tracking.cpp
samples/cpp/tutorial_code/features2D/AKAZE_tracking/utils.h

index 41bbfdd..518cada 100644 (file)
@@ -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
index c407c86..7a36e9f 100755 (executable)
@@ -1,7 +1,6 @@
 #include <opencv2/features2d.hpp>
 #include <opencv2/videoio.hpp>
 #include <opencv2/opencv.hpp>
-#include <opencv2/tracking.hpp>     //for ROI
 #include <opencv2/highgui.hpp>      //for imshow
 #include <vector>
 #include <iostream>
@@ -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<int>(video_in.get(CAP_PROP_FPS));
     }
     else {
         video_in.open(video_name);
-        iFourCC = static_cast<int>(video_in.get(CAP_PROP_FOURCC));
-        frame_count = static_cast<int>(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 = 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<Point2f> bb;
-    if (argc < 4) {             //attempt to alow GUI selection
-        cv::Rect2d uBox = selectROI(video_name, frame);
-        bb.push_back(cv::Point2f(static_cast<float>(uBox.x), static_cast<float>(uBox.y)));
-        bb.push_back(cv::Point2f(static_cast<float>(uBox.x+uBox.width), static_cast<float>(uBox.y)));
-        bb.push_back(cv::Point2f(static_cast<float>(uBox.x+uBox.width), static_cast<float>(uBox.y+uBox.height)));
-        bb.push_back(cv::Point2f(static_cast<float>(uBox.x), static_cast<float>(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<float>(uBox.x), static_cast<float>(uBox.y)));
+    bb.push_back(cv::Point2f(static_cast<float>(uBox.x+uBox.width), static_cast<float>(uBox.y)));
+    bb.push_back(cv::Point2f(static_cast<float>(uBox.x+uBox.width), static_cast<float>(uBox.y+uBox.height)));
+    bb.push_back(cv::Point2f(static_cast<float>(uBox.x), static_cast<float>(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;
index fbd897e..2142e98 100644 (file)
@@ -12,6 +12,7 @@ void drawBoundingBox(Mat image, vector<Point2f> bb);
 void drawStatistics(Mat image, const Stats& stats);
 void printStatistics(string name, Stats stats);
 vector<Point2f> Points(vector<KeyPoint> keypoints);
+Rect2d selectROI(const String &video_name, const Mat &frame);
 
 void drawBoundingBox(Mat image, vector<Point2f> bb)
 {
@@ -56,4 +57,58 @@ vector<Point2f> Points(vector<KeyPoint> 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