stereo_match.cpp 9.8 KB
Newer Older
wester committed
1 2 3 4 5
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
wester committed
6 7
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/highgui/highgui.hpp"
wester committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

using namespace cv;
using namespace std;

bool help_showed = false;

struct Params
{
    Params();
    static Params read(int argc, char** argv);

    string left;
    string right;

    string method_str() const
    {
        switch (method)
        {
        case BM: return "BM";
        case BP: return "BP";
        case CSBP: return "CSBP";
        }
        return "";
    }
    enum {BM, BP, CSBP} method;
    int ndisp; // Max disparity + 1
};


struct App
{
    App(const Params& p);
    void run();
    void handleKey(char key);
    void printParams() const;

    void workBegin() { work_begin = getTickCount(); }
    void workEnd()
    {
        int64 d = getTickCount() - work_begin;
        double f = getTickFrequency();
        work_fps = f / d;
    }

    string text() const
    {
        stringstream ss;
        ss << "(" << p.method_str() << ") FPS: " << setiosflags(ios::left)
            << setprecision(4) << work_fps;
        return ss.str();
    }
private:
    Params p;
    bool running;

    Mat left_src, right_src;
    Mat left, right;
wester committed
65
    gpu::GpuMat d_left, d_right;
wester committed
66

wester committed
67 68 69
    gpu::StereoBM_GPU bm;
    gpu::StereoBeliefPropagation bp;
    gpu::StereoConstantSpaceBP csbp;
wester committed
70 71 72 73 74 75 76

    int64 work_begin;
    double work_fps;
};

static void printHelp()
{
a  
Kai Westerkamp committed
77
    cout << "Usage: stereo_match_gpu\n"
wester committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        << "\t--left <left_view> --right <right_view> # must be rectified\n"
        << "\t--method <stereo_match_method> # BM | BP | CSBP\n"
        << "\t--ndisp <number> # number of disparity levels\n";
    help_showed = true;
}

int main(int argc, char** argv)
{
    try
    {
        if (argc < 2)
        {
            printHelp();
            return 1;
        }
        Params args = Params::read(argc, argv);
        if (help_showed)
            return -1;
        App app(args);
        app.run();
    }
    catch (const exception& e)
    {
        cout << "error: " << e.what() << endl;
    }
    return 0;
}


Params::Params()
{
    method = BM;
    ndisp = 64;
}


Params Params::read(int argc, char** argv)
{
    Params p;

    for (int i = 1; i < argc; i++)
    {
        if (string(argv[i]) == "--left") p.left = argv[++i];
        else if (string(argv[i]) == "--right") p.right = argv[++i];
        else if (string(argv[i]) == "--method")
        {
            if (string(argv[i + 1]) == "BM") p.method = BM;
            else if (string(argv[i + 1]) == "BP") p.method = BP;
            else if (string(argv[i + 1]) == "CSBP") p.method = CSBP;
            else throw runtime_error("unknown stereo match method: " + string(argv[i + 1]));
            i++;
        }
        else if (string(argv[i]) == "--ndisp") p.ndisp = atoi(argv[++i]);
        else if (string(argv[i]) == "--help") printHelp();
        else throw runtime_error("unknown key: " + string(argv[i]));
    }

    return p;
}


App::App(const Params& params)
    : p(params), running(false)
{
wester committed
142
    cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());
wester committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    cout << "stereo_match_gpu sample\n";
    cout << "\nControls:\n"
        << "\tesc - exit\n"
        << "\tp - print current parameters\n"
        << "\tg - convert source images into gray\n"
        << "\tm - change stereo match method\n"
        << "\ts - change Sobel prefiltering flag (for BM only)\n"
        << "\t1/q - increase/decrease maximum disparity\n"
        << "\t2/w - increase/decrease window size (for BM only)\n"
        << "\t3/e - increase/decrease iteration count (for BP and CSBP only)\n"
        << "\t4/r - increase/decrease level count (for BP and CSBP only)\n";
}


void App::run()
{
    // Load images
    left_src = imread(p.left);
    right_src = imread(p.right);
    if (left_src.empty()) throw runtime_error("can't open file \"" + p.left + "\"");
    if (right_src.empty()) throw runtime_error("can't open file \"" + p.right + "\"");
wester committed
165 166
    cvtColor(left_src, left, CV_BGR2GRAY);
    cvtColor(right_src, right, CV_BGR2GRAY);
wester committed
167 168 169 170 171 172 173
    d_left.upload(left);
    d_right.upload(right);

    imshow("left", left);
    imshow("right", right);

    // Set common parameters
wester committed
174 175 176
    bm.ndisp = p.ndisp;
    bp.ndisp = p.ndisp;
    csbp.ndisp = p.ndisp;
wester committed
177 178 179

    // Prepare disparity map of specified type
    Mat disp(left.size(), CV_8U);
wester committed
180
    gpu::GpuMat d_disp(left.size(), CV_8U);
wester committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194

    cout << endl;
    printParams();

    running = true;
    while (running)
    {
        workBegin();
        switch (p.method)
        {
        case Params::BM:
            if (d_left.channels() > 1 || d_right.channels() > 1)
            {
                cout << "BM doesn't support color images\n";
wester committed
195 196
                cvtColor(left_src, left, CV_BGR2GRAY);
                cvtColor(right_src, right, CV_BGR2GRAY);
wester committed
197 198 199 200 201 202
                cout << "image_channels: " << left.channels() << endl;
                d_left.upload(left);
                d_right.upload(right);
                imshow("left", left);
                imshow("right", right);
            }
wester committed
203
            bm(d_left, d_right, d_disp);
wester committed
204
            break;
wester committed
205 206
        case Params::BP: bp(d_left, d_right, d_disp); break;
        case Params::CSBP: csbp(d_left, d_right, d_disp); break;
wester committed
207 208 209 210 211 212
        }
        workEnd();

        // Show results
        d_disp.download(disp);
        putText(disp, text(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::all(255));
a  
Kai Westerkamp committed
213
        imshow("disparity", disp);
wester committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

        handleKey((char)waitKey(3));
    }
}


void App::printParams() const
{
    cout << "--- Parameters ---\n";
    cout << "image_size: (" << left.cols << ", " << left.rows << ")\n";
    cout << "image_channels: " << left.channels() << endl;
    cout << "method: " << p.method_str() << endl
        << "ndisp: " << p.ndisp << endl;
    switch (p.method)
    {
    case Params::BM:
wester committed
230 231
        cout << "win_size: " << bm.winSize << endl;
        cout << "prefilter_sobel: " << bm.preset << endl;
wester committed
232 233
        break;
    case Params::BP:
wester committed
234 235
        cout << "iter_count: " << bp.iters << endl;
        cout << "level_count: " << bp.levels << endl;
wester committed
236 237
        break;
    case Params::CSBP:
wester committed
238 239
        cout << "iter_count: " << csbp.iters << endl;
        cout << "level_count: " << csbp.levels << endl;
wester committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        break;
    }
    cout << endl;
}


void App::handleKey(char key)
{
    switch (key)
    {
    case 27:
        running = false;
        break;
    case 'p': case 'P':
        printParams();
        break;
    case 'g': case 'G':
        if (left.channels() == 1 && p.method != Params::BM)
        {
            left = left_src;
            right = right_src;
        }
        else
        {
wester committed
264 265
            cvtColor(left_src, left, CV_BGR2GRAY);
            cvtColor(right_src, right, CV_BGR2GRAY);
wester committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        }
        d_left.upload(left);
        d_right.upload(right);
        cout << "image_channels: " << left.channels() << endl;
        imshow("left", left);
        imshow("right", right);
        break;
    case 'm': case 'M':
        switch (p.method)
        {
        case Params::BM:
            p.method = Params::BP;
            break;
        case Params::BP:
            p.method = Params::CSBP;
            break;
        case Params::CSBP:
            p.method = Params::BM;
            break;
        }
        cout << "method: " << p.method_str() << endl;
        break;
    case 's': case 'S':
        if (p.method == Params::BM)
        {
wester committed
291
            switch (bm.preset)
wester committed
292
            {
wester committed
293 294
            case gpu::StereoBM_GPU::BASIC_PRESET:
                bm.preset = gpu::StereoBM_GPU::PREFILTER_XSOBEL;
wester committed
295
                break;
wester committed
296 297
            case gpu::StereoBM_GPU::PREFILTER_XSOBEL:
                bm.preset = gpu::StereoBM_GPU::BASIC_PRESET;
wester committed
298 299
                break;
            }
wester committed
300
            cout << "prefilter_sobel: " << bm.preset << endl;
wester committed
301 302 303 304 305
        }
        break;
    case '1':
        p.ndisp = p.ndisp == 1 ? 8 : p.ndisp + 8;
        cout << "ndisp: " << p.ndisp << endl;
wester committed
306 307 308
        bm.ndisp = p.ndisp;
        bp.ndisp = p.ndisp;
        csbp.ndisp = p.ndisp;
wester committed
309 310 311 312
        break;
    case 'q': case 'Q':
        p.ndisp = max(p.ndisp - 8, 1);
        cout << "ndisp: " << p.ndisp << endl;
wester committed
313 314 315
        bm.ndisp = p.ndisp;
        bp.ndisp = p.ndisp;
        csbp.ndisp = p.ndisp;
wester committed
316 317 318 319
        break;
    case '2':
        if (p.method == Params::BM)
        {
wester committed
320 321
            bm.winSize = min(bm.winSize + 1, 51);
            cout << "win_size: " << bm.winSize << endl;
wester committed
322 323 324 325 326
        }
        break;
    case 'w': case 'W':
        if (p.method == Params::BM)
        {
wester committed
327 328
            bm.winSize = max(bm.winSize - 1, 2);
            cout << "win_size: " << bm.winSize << endl;
wester committed
329 330 331 332 333
        }
        break;
    case '3':
        if (p.method == Params::BP)
        {
wester committed
334 335
            bp.iters += 1;
            cout << "iter_count: " << bp.iters << endl;
wester committed
336 337 338
        }
        else if (p.method == Params::CSBP)
        {
wester committed
339 340
            csbp.iters += 1;
            cout << "iter_count: " << csbp.iters << endl;
wester committed
341 342 343 344 345
        }
        break;
    case 'e': case 'E':
        if (p.method == Params::BP)
        {
wester committed
346 347
            bp.iters = max(bp.iters - 1, 1);
            cout << "iter_count: " << bp.iters << endl;
wester committed
348 349 350
        }
        else if (p.method == Params::CSBP)
        {
wester committed
351 352
            csbp.iters = max(csbp.iters - 1, 1);
            cout << "iter_count: " << csbp.iters << endl;
wester committed
353 354 355 356 357
        }
        break;
    case '4':
        if (p.method == Params::BP)
        {
wester committed
358 359
            bp.levels += 1;
            cout << "level_count: " << bp.levels << endl;
wester committed
360 361 362
        }
        else if (p.method == Params::CSBP)
        {
wester committed
363 364
            csbp.levels += 1;
            cout << "level_count: " << csbp.levels << endl;
wester committed
365 366 367 368 369
        }
        break;
    case 'r': case 'R':
        if (p.method == Params::BP)
        {
wester committed
370 371
            bp.levels = max(bp.levels - 1, 1);
            cout << "level_count: " << bp.levels << endl;
wester committed
372 373 374
        }
        else if (p.method == Params::CSBP)
        {
wester committed
375 376
            csbp.levels = max(csbp.levels - 1, 1);
            cout << "level_count: " << csbp.levels << endl;
wester committed
377 378 379 380
        }
        break;
    }
}