added the first T-API example - CamShift tracking
authorIlya Lavrenov <ilya.lavrenov@itseez.com>
Sat, 28 Dec 2013 22:28:43 +0000 (02:28 +0400)
committerIlya Lavrenov <ilya.lavrenov@itseez.com>
Sun, 29 Dec 2013 10:36:59 +0000 (14:36 +0400)
samples/CMakeLists.txt
samples/c/CMakeLists.txt
samples/cpp/CMakeLists.txt
samples/gpu/CMakeLists.txt
samples/ocl/CMakeLists.txt
samples/tapi/CMakeLists.txt [new file with mode: 0644]
samples/tapi/camshift.cpp [new file with mode: 0644]

index 9dd3df0..01f376d 100644 (file)
@@ -14,6 +14,7 @@ add_subdirectory(c)
 add_subdirectory(cpp)
 add_subdirectory(gpu)
 add_subdirectory(ocl)
+add_subdirectory(tapi)
 
 if(WIN32 AND HAVE_DIRECTX)
   add_subdirectory(directx)
@@ -23,7 +24,6 @@ if(ANDROID AND BUILD_ANDROID_EXAMPLES)
   add_subdirectory(android)
 endif()
 
-
 #
 # END OF BUILD CASE 1: Build samples with library sources
 #
@@ -73,4 +73,4 @@ endif()
 #
 # END OF BUILD CASE 2: Build samples with library binaries
 #
-endif()
\ No newline at end of file
+endif()
index 77a4294..b8dfe64 100644 (file)
@@ -51,7 +51,7 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
   endforeach()
 endif()
 
-if (INSTALL_C_EXAMPLES AND NOT WIN32)
+if(INSTALL_C_EXAMPLES AND NOT WIN32)
   file(GLOB C_SAMPLES *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
   install(FILES ${C_SAMPLES}
           DESTINATION share/OpenCV/samples/c
index 4b0bf01..eaebcb9 100644 (file)
@@ -99,7 +99,7 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
   endforeach()
 endif()
 
-if (INSTALL_C_EXAMPLES AND NOT WIN32)
+if(INSTALL_C_EXAMPLES AND NOT WIN32)
   file(GLOB C_SAMPLES *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
   install(FILES ${C_SAMPLES}
           DESTINATION share/OpenCV/samples/cpp
index 64c25fc..1d19fbd 100644 (file)
@@ -91,7 +91,7 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
   include("performance/CMakeLists.txt")
 endif()
 
-if (INSTALL_C_EXAMPLES AND NOT WIN32)
+if(INSTALL_C_EXAMPLES AND NOT WIN32)
   file(GLOB install_list *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
   install(FILES ${install_list}
           DESTINATION share/OpenCV/samples/${project}
index b4f7afa..9344fb0 100644 (file)
@@ -1,7 +1,6 @@
-SET(OPENCV_OCL_SAMPLES_REQUIRED_DEPS opencv_core opencv_flann opencv_imgproc opencv_highgui
+SET(OPENCV_OCL_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_highgui
                                      opencv_ml opencv_video opencv_objdetect opencv_features2d
-                                     opencv_calib3d opencv_legacy opencv_contrib opencv_ocl
-                                     opencv_nonfree opencv_bioinspired)
+                                     opencv_ocl opencv_nonfree opencv_bioinspired)
 
 ocv_check_dependencies(${OPENCV_OCL_SAMPLES_REQUIRED_DEPS})
 
@@ -51,7 +50,7 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
   endforeach()
 endif()
 
-if (INSTALL_C_EXAMPLES AND NOT WIN32)
+if(INSTALL_C_EXAMPLES AND NOT WIN32)
   file(GLOB install_list *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
   install(FILES ${install_list}
           DESTINATION share/OpenCV/samples/${project}
diff --git a/samples/tapi/CMakeLists.txt b/samples/tapi/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4cfb580
--- /dev/null
@@ -0,0 +1,52 @@
+SET(OPENCV_TAPI_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_video opencv_highgui)
+
+ocv_check_dependencies(${OPENCV_TAPI_SAMPLES_REQUIRED_DEPS})
+
+if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
+  set(project "tapi")
+  string(TOUPPER "${project}" project_upper)
+
+  project("${project}_samples")
+
+  ocv_include_modules(${OPENCV_TAPI_SAMPLES_REQUIRED_DEPS})
+
+  # ---------------------------------------------
+  #      Define executable targets
+  # ---------------------------------------------
+  MACRO(OPENCV_DEFINE_TAPI_EXAMPLE name srcs)
+    set(the_target "example_${project}_${name}")
+    add_executable(${the_target} ${srcs})
+
+    target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${OPENCV_TAPI_SAMPLES_REQUIRED_DEPS})
+
+    set_target_properties(${the_target} PROPERTIES
+      OUTPUT_NAME "${project}-example-${name}"
+      PROJECT_LABEL "(EXAMPLE_${project_upper}) ${name}")
+
+    if(ENABLE_SOLUTION_FOLDERS)
+      set_target_properties(${the_target} PROPERTIES FOLDER "samples//${project}")
+    endif()
+
+    if(WIN32)
+      if(MSVC AND NOT BUILD_SHARED_LIBS)
+        set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
+      endif()
+      install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${project}" COMPONENT main)
+    endif()
+  ENDMACRO()
+
+  file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
+
+  foreach(sample_filename ${all_samples})
+    get_filename_component(sample ${sample_filename} NAME_WE)
+    file(GLOB sample_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${sample}.*)
+    OPENCV_DEFINE_TAPI_EXAMPLE(${sample} ${sample_srcs})
+  endforeach()
+endif()
+
+if(INSTALL_C_EXAMPLES AND NOT WIN32)
+  file(GLOB install_list *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )
+  install(FILES ${install_list}
+          DESTINATION share/OpenCV/samples/${project}
+          PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)
+endif()
diff --git a/samples/tapi/camshift.cpp b/samples/tapi/camshift.cpp
new file mode 100644 (file)
index 0000000..d6e3532
--- /dev/null
@@ -0,0 +1,209 @@
+#include "opencv2/core/utility.hpp"
+#include "opencv2/video/tracking.hpp"
+#include "opencv2/imgproc/imgproc.hpp"
+#include "opencv2/highgui/highgui.hpp"
+
+#include <iostream>
+#include <cctype>
+
+static cv::Mat image;
+static bool backprojMode = false;
+static bool selectObject = false;
+static int trackObject = 0;
+static bool showHist = true;
+static cv::Point origin;
+static cv::Rect selection;
+static int vmin = 10, vmax = 256, smin = 30;
+
+static void onMouse(int event, int x, int y, int, void*)
+{
+    if (selectObject)
+    {
+        selection.x = std::min(x, origin.x);
+        selection.y = std::min(y, origin.y);
+        selection.width = std::abs(x - origin.x);
+        selection.height = std::abs(y - origin.y);
+
+        selection &= cv::Rect(0, 0, image.cols, image.rows);
+    }
+
+    switch(event)
+    {
+    case cv::EVENT_LBUTTONDOWN:
+        origin = cv::Point(x, y);
+        selection = cv::Rect(x, y, 0, 0);
+        selectObject = true;
+        break;
+    case cv::EVENT_LBUTTONUP:
+        selectObject = false;
+        if (selection.width > 0 && selection.height > 0)
+            trackObject = -1;
+        break;
+    default:
+        break;
+    }
+}
+
+static void help()
+{
+    std::cout << "\nThis is a demo that shows mean-shift based tracking using Transparent API\n"
+            "You select a color objects such as your face and it tracks it.\n"
+            "This reads from video camera (0 by default, or the camera number the user enters\n"
+            "Usage: \n"
+            "   ./camshiftdemo [camera number]\n";
+
+    std::cout << "\n\nHot keys: \n"
+            "\tESC - quit the program\n"
+            "\tc - stop the tracking\n"
+            "\tb - switch to/from backprojection view\n"
+            "\th - show/hide object histogram\n"
+            "\tp - pause video\n"
+            "To initialize tracking, select the object with mouse\n";
+}
+
+int main(int argc, const char** argv)
+{
+    help();
+
+    cv::VideoCapture cap;
+    cv::Rect trackWindow;
+    int hsize = 16;
+    float hranges[2] = { 0, 180 };
+    const float * phranges = hranges;
+
+    const char * const keys = { "{@camera_number| 0 | camera number}" };
+    cv::CommandLineParser parser(argc, argv, keys);
+    int camNum = parser.get<int>(0);
+
+    cap.open(camNum);
+
+    if (!cap.isOpened())
+    {
+        help();
+        std::cout << "***Could not initialize capturing...***\n";
+        std::cout << "Current parameter's value: \n";
+        parser.printMessage();
+
+        return EXIT_FAILURE;
+    }
+
+    cv::namedWindow("Histogram", cv::WINDOW_NORMAL);
+    cv::namedWindow("CamShift Demo", cv::WINDOW_NORMAL);
+    cv::setMouseCallback("CamShift Demo", onMouse, NULL);
+    cv::createTrackbar("Vmin", "CamShift Demo", &vmin, 256, NULL);
+    cv::createTrackbar("Vmax", "CamShift Demo", &vmax, 256, NULL);
+    cv::createTrackbar("Smin", "CamShift Demo", &smin, 256, NULL);
+
+    cv::Mat frame, hsv, hue, mask, hist, histimg = cv::Mat::zeros(200, 320, CV_8UC3), backproj;
+    bool paused = false;
+
+    for ( ; ; )
+    {
+        if (!paused)
+        {
+            cap >> frame;
+            if (frame.empty())
+                break;
+        }
+
+        frame.copyTo(image);
+
+        if (!paused)
+        {
+            cv::cvtColor(image, hsv, cv::COLOR_BGR2HSV);
+
+            if (trackObject)
+            {
+                int _vmin = vmin, _vmax = vmax;
+
+                cv::inRange(hsv, cv::Scalar(0, smin, std::min(_vmin, _vmax)),
+                        cv::Scalar(180, 256, std::max(_vmin, _vmax)), mask);
+
+                int ch[2] = { 0, 0 };
+                hue.create(hsv.size(), hsv.depth());
+                cv::mixChannels(&hsv, 1, &hue, 1, ch, 1);
+
+                if (trackObject < 0)
+                {
+                    cv::Mat roi(hue, selection), maskroi(mask, selection);
+                    cv::calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
+                    cv::normalize(hist, hist, 0, 255, cv::NORM_MINMAX);
+
+                    trackWindow = selection;
+                    trackObject = 1;
+
+                    histimg = cv::Scalar::all(0);
+                    int binW = histimg.cols / hsize;
+                    cv::Mat buf (1, hsize, CV_8UC3);
+                    for (int i = 0; i < hsize; i++)
+                        buf.at<cv::Vec3b>(i) = cv::Vec3b(cv::saturate_cast<uchar>(i*180./hsize), 255, 255);
+                    cv::cvtColor(buf, buf, cv::COLOR_HSV2BGR);
+
+                    for (int i = 0; i < hsize; i++)
+                    {
+                        int val = cv::saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
+                        cv::rectangle(histimg, cv::Point(i*binW, histimg.rows),
+                                   cv::Point((i+1)*binW, histimg.rows - val),
+                                   cv::Scalar(buf.at<cv::Vec3b>(i)), -1, 8);
+                    }
+                }
+
+                cv::calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
+                backproj &= mask;
+                cv::RotatedRect trackBox = cv::CamShift(backproj, trackWindow,
+                                    cv::TermCriteria(cv::TermCriteria::EPS | cv::TermCriteria::COUNT, 10, 1));
+                if (trackWindow.area() <= 1)
+                {
+                    int cols = backproj.cols, rows = backproj.rows, r = (std::min(cols, rows) + 5)/6;
+                    trackWindow = cv::Rect(trackWindow.x - r, trackWindow.y - r,
+                                       trackWindow.x + r, trackWindow.y + r) &
+                                  cv::Rect(0, 0, cols, rows);
+                }
+
+                if (backprojMode)
+                    cv::cvtColor(backproj, image, cv::COLOR_GRAY2BGR);
+                cv::ellipse(image, trackBox, cv::Scalar(0, 0, 255), 3, cv::LINE_AA);
+            }
+        }
+        else if (trackObject < 0)
+            paused = false;
+
+        if (selectObject && selection.width > 0 && selection.height > 0)
+        {
+            cv::Mat roi(image, selection);
+            cv::bitwise_not(roi, roi);
+        }
+
+        cv::imshow("CamShift Demo", image);
+        cv::imshow("Histogram", histimg);
+
+        char c = (char)cv::waitKey(10);
+        if (c == 27)
+            break;
+
+        switch(c)
+        {
+        case 'b':
+            backprojMode = !backprojMode;
+            break;
+        case 'c':
+            trackObject = 0;
+            histimg = cv::Scalar::all(0);
+            break;
+        case 'h':
+            showHist = !showHist;
+            if (!showHist)
+                cv::destroyWindow("Histogram");
+            else
+                cv::namedWindow("Histogram", cv::WINDOW_AUTOSIZE);
+            break;
+        case 'p':
+            paused = !paused;
+            break;
+        default:
+            break;
+        }
+    }
+
+    return EXIT_SUCCESS;
+}