d3dsample.hpp 4.18 KB
Newer Older
1 2 3 4
/*
// Sample demonstrating interoperability of OpenCV UMat with Direct X surface
// Base class for Direct X application
*/
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <string>
#include <iostream>
#include <queue>

#include "opencv2/core.hpp"
#include "opencv2/core/directx.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"

#include "winapp.hpp"

#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }


class D3DSample : public WinApp
{
public:
    enum MODE
    {
        MODE_CPU,
Vladimir Dudnik's avatar
Vladimir Dudnik committed
26 27
        MODE_GPU_RGBA,
        MODE_GPU_NV12
28 29 30 31 32 33
    };

    D3DSample(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
        WinApp(width, height, window_name)
    {
        m_shutdown          = false;
34 35
        m_mode              = MODE_CPU;
        m_modeStr[0]        = cv::String("Processing on CPU");
Vladimir Dudnik's avatar
Vladimir Dudnik committed
36 37
        m_modeStr[1]        = cv::String("Processing on GPU RGBA");
        m_modeStr[2]        = cv::String("Processing on GPU NV12");
38
        m_demo_processing   = false;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        m_cap               = cap;
    }

    ~D3DSample() {}

    virtual int create() { return WinApp::create(); }
    virtual int render() = 0;
    virtual int cleanup()
    {
        m_shutdown = true;
        return WinApp::cleanup();
    }

protected:
    virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_CHAR:
58
            if (wParam == '1')
59
            {
60
                m_mode = MODE_CPU;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
61
                return EXIT_SUCCESS;
62 63 64
            }
            if (wParam == '2')
            {
Vladimir Dudnik's avatar
Vladimir Dudnik committed
65
                m_mode = MODE_GPU_RGBA;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
66
                return EXIT_SUCCESS;
Vladimir Dudnik's avatar
Vladimir Dudnik committed
67 68 69 70
            }
            if (wParam == '3')
            {
                m_mode = MODE_GPU_NV12;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
71
                return EXIT_SUCCESS;
72 73 74
            }
            else if (wParam == VK_SPACE)
            {
75
                m_demo_processing = !m_demo_processing;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
76
                return EXIT_SUCCESS;
77 78 79 80 81 82 83 84 85 86 87 88
            }
            else if (wParam == VK_ESCAPE)
            {
                return cleanup();
            }
            break;

        case WM_CLOSE:
            return cleanup();

        case WM_DESTROY:
            ::PostQuitMessage(0);
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
89
            return EXIT_SUCCESS;
90 91 92 93 94 95 96 97 98 99
        }

        return ::DefWindowProc(hWnd, message, wParam, lParam);
    }

    // do render at idle
    virtual int idle() { return render(); }

protected:
    bool               m_shutdown;
100
    bool               m_demo_processing;
101
    MODE               m_mode;
Vladimir Dudnik's avatar
Vladimir Dudnik committed
102
    cv::String         m_modeStr[3];
103 104 105
    cv::VideoCapture   m_cap;
    cv::Mat            m_frame_bgr;
    cv::Mat            m_frame_rgba;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
106
    cv::TickMeter      m_timer;
107 108 109
};


110 111
static const char* keys =
{
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
112
    "{c camera | 0     | camera id  }"
113 114 115 116 117 118 119
    "{f file   |       | movie file name  }"
};


template <typename TApp>
int d3d_app(int argc, char** argv, std::string& title)
{
120
    cv::CommandLineParser parser(argc, argv, keys);
berak's avatar
berak committed
121
    std::string file = parser.get<std::string>("file");
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
122
    int    camera_id = parser.get<int>("camera");
123

Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
124 125 126 127 128 129 130 131
    parser.about(
        "\nA sample program demonstrating interoperability of DirectX and OpenCL with OpenCV.\n\n"
        "Hot keys: \n"
        "  SPACE - turn processing on/off\n"
        "    1   - process DX surface through OpenCV on CPU\n"
        "    2   - process DX RGBA surface through OpenCV on GPU (via OpenCL)\n"
        "    3   - process DX NV12 surface through OpenCV on GPU (via OpenCL)\n"
        "   ESC  - exit\n\n");
132 133 134 135 136

    parser.printMessage();

    cv::VideoCapture cap;

Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
137 138
    if (file.empty())
        cap.open(camera_id);
139 140 141 142 143 144
    else
        cap.open(file.c_str());

    if (!cap.isOpened())
    {
        printf("can not open camera or video file\n");
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
145
        return EXIT_FAILURE;
146 147
    }

berak's avatar
berak committed
148 149
    int width  = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
150 151 152 153 154 155 156 157 158 159 160

    std::string wndname = title;

    TApp app(width, height, wndname, cap);

    try
    {
        app.create();
        return app.run();
    }

161
    catch (const cv::Exception& e)
162 163 164 165 166 167 168 169 170 171
    {
        std::cerr << "Exception: " << e.what() << std::endl;
        return 10;
    }

    catch (...)
    {
        std::cerr << "FATAL ERROR: Unknown exception" << std::endl;
        return 11;
    }
172
}