added first version of stereo match sample on gpu
authorAlexey Spizhevoy <no@email>
Tue, 21 Dec 2010 07:35:46 +0000 (07:35 +0000)
committerAlexey Spizhevoy <no@email>
Tue, 21 Dec 2010 07:35:46 +0000 (07:35 +0000)
samples/gpu/hog.cpp
samples/gpu/stereo_match.cpp [new file with mode: 0644]
samples/gpu/tsucuba_left.png [new file with mode: 0644]
samples/gpu/tsucuba_right.png [new file with mode: 0644]

index fe7d0ab..466f709 100644 (file)
@@ -91,7 +91,7 @@ int main(int argc, char** argv)
     {\r
         if (argc < 2)\r
         {\r
-            cout << "Usage:\nsample_hog\n"\r
+            cout << "Usage:\nhog_gpu\n"\r
                 << "  -src <path_to_the_source>\n"\r
                 << "  [-src_is_video <true/false>] # says to interp. src as img or as video\n"\r
                 << "  [-make_gray <true/false>] # convert image to gray one or not\n"\r
diff --git a/samples/gpu/stereo_match.cpp b/samples/gpu/stereo_match.cpp
new file mode 100644 (file)
index 0000000..db28d9e
--- /dev/null
@@ -0,0 +1,221 @@
+#include <iostream>\r
+#include <string>\r
+#include <sstream>\r
+#include <iomanip>\r
+#include <stdexcept>\r
+#include "opencv2/gpu/gpu.hpp"\r
+#include "opencv2/highgui/highgui.hpp"\r
+\r
+using namespace cv;\r
+using namespace std;\r
+\r
+\r
+struct Params\r
+{\r
+    Params();\r
+    static Params read(int argc, char** argv);\r
+\r
+    string left;\r
+    string right;\r
+    string method_str;\r
+    enum {BM, BP, CSBP} method;\r
+    int ndisp; // Max disparity + 1\r
+};\r
+\r
+\r
+struct App\r
+{\r
+    App(const Params& p);\r
+    void run();\r
+    void handleKey(char key);\r
+\r
+    void workBegin() { work_begin = getTickCount(); }\r
+    void workEnd() \r
+    {\r
+        int64 d = getTickCount() - work_begin;\r
+        double f = getTickFrequency();\r
+        work_fps = f / d;\r
+    }\r
+\r
+    string text() const\r
+    {\r
+        stringstream ss;\r
+        ss << "(" << p.method_str << ") FPS: " << setiosflags(ios::left) << setprecision(4) << work_fps;\r
+        return ss.str();\r
+    }\r
+private:\r
+    Params p;\r
+    bool running;\r
+\r
+    gpu::StereoBM_GPU bm;\r
+    gpu::StereoBeliefPropagation bp;\r
+    gpu::StereoConstantSpaceBP csbp;\r
+\r
+    int64 work_begin;\r
+    double work_fps;\r
+};\r
+\r
+\r
+int main(int argc, char** argv)\r
+{\r
+    try\r
+    {\r
+        if (argc < 2)\r
+        {\r
+            cout << "Usage: stereo_match_gpu\n"\r
+                << "\t-l <left_view> -r <right_view> # must be rectified\n"\r
+                << "\t-m <stereo_match_method> # bm | bp | csbp\n";\r
+            return 1;\r
+        }\r
+        App app(Params::read(argc, argv));\r
+        app.run();\r
+    }\r
+    catch (const exception& e)\r
+    {\r
+        cout << "error: " << e.what() << endl;\r
+    }\r
+    return 0;\r
+}\r
+\r
+\r
+Params::Params()\r
+{\r
+    ndisp = 64;\r
+}\r
+\r
+\r
+Params Params::read(int argc, char** argv)\r
+{\r
+    Params p;\r
+\r
+    for (int i = 1; i < argc - 1; i += 2)\r
+    {\r
+        string key = argv[i];\r
+        string val = argv[i + 1];\r
+        if (key == "-l") p.left = val;\r
+        else if (key == "-r") p.right = val;\r
+        else if (key == "-m") \r
+        {\r
+            if (val == "BM") p.method = BM;\r
+            else if (val == "BP") p.method = BP;\r
+            else if (val == "CSBP") p.method = CSBP;\r
+            else throw runtime_error("unknown stereo match method: " + val);\r
+            p.method_str = val;\r
+        }\r
+        else if (key == "-ndisp") p.ndisp = atoi(val.c_str());\r
+        else throw runtime_error("unknown key: " + key);\r
+    }\r
+\r
+    return p;\r
+}\r
+\r
+\r
+App::App(const Params& p)\r
+    : p(p), running(false) \r
+{\r
+    cout << "stereo_match_gpu sample\n";\r
+    cout << "\nControls:\n"\r
+        << "\tesc - exit\n"\r
+        << "\tm - change stereo match method\n"\r
+        << "\t1/q - increase/decrease max disprity\n";\r
+}\r
+\r
+\r
+void App::run()\r
+{\r
+    Mat left, right;\r
+    Mat left_aux, right_aux;\r
+    gpu::GpuMat d_left, d_right;\r
+\r
+    // Load images\r
+    left_aux = imread(p.left);\r
+    right_aux = imread(p.right);\r
+    if (left_aux.empty()) throw runtime_error("can't open file \"" + p.left + "\"");\r
+    if (right_aux.empty()) throw runtime_error("can't open file \"" + p.right + "\"");\r
+    cvtColor(left_aux, left, CV_BGR2GRAY);\r
+    cvtColor(right_aux, right, CV_BGR2GRAY);\r
+    d_left = left;\r
+    d_right = right;\r
+\r
+    imshow("left", left);\r
+    imshow("right", right);\r
+\r
+    // Create stero method descriptors\r
+    bm.ndisp = p.ndisp;\r
+    bp.ndisp = p.ndisp;\r
+    csbp.ndisp = p.ndisp;\r
+\r
+    // Prepare disparity map of specified type\r
+    Mat disp(left.size(), CV_8U);\r
+    gpu::GpuMat d_disp(left.size(), CV_8U);\r
+\r
+    // Show initial parameters\r
+    cout << "\nInitial Params:\n"\r
+        << "\timage_size: (" << left.cols << ", " << left.rows << ")\n"\r
+        << "\tmethod: " << p.method_str << endl\r
+        << "\tndisp: " << p.ndisp << endl << endl;\r
+\r
+    running = true;\r
+    while (running)\r
+    {\r
+        workBegin();\r
+        switch (p.method)\r
+        {\r
+        case Params::BM: bm(d_left, d_right, d_disp); break;\r
+        case Params::BP: bp(d_left, d_right, d_disp); break;\r
+        case Params::CSBP: csbp(d_left, d_right, d_disp); break;\r
+        }\r
+        workEnd();\r
+\r
+        // Show results\r
+        disp = d_disp;\r
+        putText(disp, text(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::all(255));\r
+        imshow("disparity", disp);\r
+\r
+        handleKey((char)waitKey(3));\r
+    }\r
+}\r
+\r
+\r
+void App::handleKey(char key)\r
+{\r
+    switch (key)\r
+    {\r
+    case 27:\r
+        running = false;\r
+        break;\r
+    case 'm': case 'M':\r
+        switch (p.method)\r
+        {\r
+        case Params::BM:\r
+            p.method = Params::BP;\r
+            p.method_str = "BP";\r
+            break;\r
+        case Params::BP:\r
+            p.method = Params::CSBP;\r
+            p.method_str = "CSBP";\r
+            break;\r
+        case Params::CSBP:\r
+            p.method = Params::BM;\r
+            p.method_str = "BM";\r
+            break;\r
+        }\r
+        cout << "method: " << p.method_str << endl;\r
+        break;\r
+    case '1':\r
+        p.ndisp = p.ndisp == 1 ? 8 : p.ndisp + 8;\r
+        bm.ndisp = p.ndisp;\r
+        bp.ndisp = p.ndisp;\r
+        csbp.ndisp = p.ndisp;\r
+        cout << "ndisp: " << p.ndisp << endl;\r
+        break;\r
+    case 'q': case 'Q':\r
+        p.ndisp = max(p.ndisp - 8, 1);\r
+        bm.ndisp = p.ndisp;\r
+        bp.ndisp = p.ndisp;\r
+        csbp.ndisp = p.ndisp;\r
+        cout << "ndisp: " << p.ndisp << endl;\r
+        break;\r
+    }\r
+}\r
+\r
diff --git a/samples/gpu/tsucuba_left.png b/samples/gpu/tsucuba_left.png
new file mode 100644 (file)
index 0000000..09679b2
Binary files /dev/null and b/samples/gpu/tsucuba_left.png differ
diff --git a/samples/gpu/tsucuba_right.png b/samples/gpu/tsucuba_right.png
new file mode 100644 (file)
index 0000000..a97b276
Binary files /dev/null and b/samples/gpu/tsucuba_right.png differ