368a6f60847b997a0a348093c93dd814ac62e87b
[profile/ivi/opencv.git] / samples / gpu / highgui_gpu.cpp
1 #include <iostream>
2 #include <string>
3
4 #include "opencv2/core/core.hpp"
5 #include "opencv2/core/gpumat.hpp"
6 #include "opencv2/core/opengl_interop.hpp"
7 #include "opencv2/gpu/gpu.hpp"
8 #include "opencv2/highgui/highgui.hpp"
9 #include "opencv2/contrib/contrib.hpp"
10
11 using namespace std;
12 using namespace cv;
13 using namespace cv::gpu;
14
15 struct Timer
16 {
17     Timer(const string& msg_)
18     {
19         msg = msg_;
20
21         tm.reset();
22         tm.start();
23     }
24
25     ~Timer()
26     {
27         tm.stop();
28         cout << msg << " " << tm.getTimeMilli() << " ms\n";
29     }
30
31     string msg;
32     TickMeter tm;
33 };
34
35 int main(int argc, char* argv[])
36 {
37     if (argc < 2)
38     {
39         cout << "Usage: " << argv[0] << " image" << endl;
40         return -1;
41     }
42
43     try
44     {
45         bool haveCuda = getCudaEnabledDeviceCount() > 0;
46
47         const string openGlMatWnd = "OpenGL Mat";
48         const string openGlBufferWnd = "OpenGL GlBuffer";
49         const string openGlTextureWnd = "OpenGL GlTexture";
50         const string openGlGpuMatWnd = "OpenGL GpuMat";
51         const string matWnd = "Mat";
52
53         namedWindow(openGlMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
54         namedWindow(openGlBufferWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
55         namedWindow(openGlTextureWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
56         if (haveCuda)
57             namedWindow(openGlGpuMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
58         namedWindow("Mat", WINDOW_AUTOSIZE);
59
60         Mat img = imread(argv[1]);
61
62         if (haveCuda)
63             setGlDevice();
64
65         setOpenGlContext(openGlBufferWnd);
66         GlBuffer buf(img, GlBuffer::TEXTURE_BUFFER);
67
68         setOpenGlContext(openGlTextureWnd);
69         GlTexture tex(img);
70
71         GpuMat d_img;
72         if (haveCuda)
73             d_img.upload(img);
74
75         cout << "=== First call\n\n";
76
77         {
78             Timer t("OpenGL Mat      ");
79             imshow(openGlMatWnd, img);
80         }
81         {
82             Timer t("OpenGL GlBuffer ");
83             imshow(openGlBufferWnd, buf);
84         }
85         {
86             Timer t("OpenGL GlTexture");
87             imshow(openGlTextureWnd, tex);
88         }
89         if (haveCuda)
90         {
91             Timer t("OpenGL GpuMat   ");
92             imshow(openGlGpuMatWnd, d_img);
93         }
94         {
95             Timer t("Mat             ");
96             imshow(matWnd, img);
97         }
98
99         waitKey();
100
101         cout << "\n=== Second call\n\n";
102
103         {
104             Timer t("OpenGL Mat      ");
105             imshow(openGlMatWnd, img);
106         }
107         {
108             Timer t("OpenGL GlBuffer ");
109             imshow(openGlBufferWnd, buf);
110         }
111         {
112             Timer t("OpenGL GlTexture");
113             imshow(openGlTextureWnd, tex);
114         }
115         if (haveCuda)
116         {
117             Timer t("OpenGL GpuMat   ");
118             imshow(openGlGpuMatWnd, d_img);
119         }
120         {
121             Timer t("Mat             ");
122             imshow(matWnd, img);
123         }
124
125         cout << "\n";
126
127         waitKey();
128     }
129     catch(const exception& e)
130     {
131         cout << e.what() << endl;
132     }
133
134     return 0;
135 }