2011-11-21 19:58:52 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "opencv2/core/core.hpp"
|
|
|
|
#include "opencv2/core/gpumat.hpp"
|
2011-11-30 14:20:29 +08:00
|
|
|
#include "opencv2/core/opengl_interop.hpp"
|
2011-11-21 19:58:52 +08:00
|
|
|
#include "opencv2/gpu/gpu.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/contrib/contrib.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::gpu;
|
|
|
|
|
|
|
|
struct Timer
|
|
|
|
{
|
|
|
|
Timer(const string& msg_)
|
|
|
|
{
|
|
|
|
msg = msg_;
|
|
|
|
|
|
|
|
tm.reset();
|
|
|
|
tm.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Timer()
|
|
|
|
{
|
|
|
|
tm.stop();
|
|
|
|
cout << msg << " " << tm.getTimeMilli() << " ms\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
string msg;
|
|
|
|
TickMeter tm;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
cout << "Usage: " << argv[0] << " image" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bool haveCuda = getCudaEnabledDeviceCount() > 0;
|
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
const string openGlMatWnd = "OpenGL Mat";
|
|
|
|
const string openGlBufferWnd = "OpenGL GlBuffer";
|
|
|
|
const string openGlTextureWnd = "OpenGL GlTexture";
|
|
|
|
const string openGlGpuMatWnd = "OpenGL GpuMat";
|
|
|
|
const string matWnd = "Mat";
|
|
|
|
|
|
|
|
namedWindow(openGlMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
|
|
|
|
namedWindow(openGlBufferWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
|
|
|
|
namedWindow(openGlTextureWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
|
2011-11-21 19:58:52 +08:00
|
|
|
if (haveCuda)
|
2011-11-30 14:20:29 +08:00
|
|
|
namedWindow(openGlGpuMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
|
2011-11-21 19:58:52 +08:00
|
|
|
namedWindow("Mat", WINDOW_AUTOSIZE);
|
|
|
|
|
|
|
|
Mat img = imread(argv[1]);
|
|
|
|
|
|
|
|
if (haveCuda)
|
|
|
|
setGlDevice();
|
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
setOpenGlContext(openGlBufferWnd);
|
2011-11-21 19:58:52 +08:00
|
|
|
GlBuffer buf(img, GlBuffer::TEXTURE_BUFFER);
|
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
setOpenGlContext(openGlTextureWnd);
|
2011-11-21 19:58:52 +08:00
|
|
|
GlTexture tex(img);
|
|
|
|
|
|
|
|
GpuMat d_img;
|
|
|
|
if (haveCuda)
|
|
|
|
d_img.upload(img);
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-21 19:58:52 +08:00
|
|
|
cout << "=== First call\n\n";
|
|
|
|
|
|
|
|
{
|
|
|
|
Timer t("OpenGL Mat ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlMatWnd, img);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Timer t("OpenGL GlBuffer ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlBufferWnd, buf);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Timer t("OpenGL GlTexture");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlTextureWnd, tex);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
if (haveCuda)
|
|
|
|
{
|
|
|
|
Timer t("OpenGL GpuMat ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlGpuMatWnd, d_img);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Timer t("Mat ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(matWnd, img);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
waitKey();
|
|
|
|
|
2011-11-21 19:58:52 +08:00
|
|
|
cout << "\n=== Second call\n\n";
|
|
|
|
|
|
|
|
{
|
|
|
|
Timer t("OpenGL Mat ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlMatWnd, img);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Timer t("OpenGL GlBuffer ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlBufferWnd, buf);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Timer t("OpenGL GlTexture");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlTextureWnd, tex);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
if (haveCuda)
|
|
|
|
{
|
|
|
|
Timer t("OpenGL GpuMat ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(openGlGpuMatWnd, d_img);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Timer t("Mat ");
|
2011-11-30 14:20:29 +08:00
|
|
|
imshow(matWnd, img);
|
2011-11-21 19:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "\n";
|
|
|
|
|
|
|
|
waitKey();
|
|
|
|
}
|
|
|
|
catch(const exception& e)
|
|
|
|
{
|
|
|
|
cout << e.what() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-11-28 16:23:26 +08:00
|
|
|
}
|