vendor: OpenCV 5.0.0 snapshot at 40738fb16ceddb5fb3fea747585f7ce6abb0605b

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:10:33 +08:00
commit f7f077da11
6933 changed files with 2335208 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
if(APPLE)
return()
endif()
if(UNIX)
find_package(X11 QUIET)
endif()
find_package(PkgConfig QUIET)
pkg_search_module(EPOXY QUIET epoxy)
SET(OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS
opencv_core
opencv_imgproc
opencv_imgcodecs
opencv_geometry
opencv_videoio
opencv_highgui)
ocv_check_dependencies(${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS})
if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
project(opengl_samples)
ocv_include_modules_recurse(${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS})
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
if(NOT X11_FOUND)
ocv_list_filterout(all_samples "opengl_interop")
endif()
if(NOT EPOXY_FOUND)
ocv_list_filterout(all_samples "opengl3_2")
endif()
foreach(sample_filename ${all_samples})
ocv_define_sample(tgt ${sample_filename} opengl)
ocv_target_link_libraries(${tgt} PRIVATE "${OPENGL_LIBRARIES}" "${OPENCV_OPENGL_SAMPLES_REQUIRED_DEPS}")
if(sample_filename STREQUAL "opengl_interop.cpp")
ocv_target_link_libraries(${tgt} PRIVATE ${X11_LIBRARIES})
ocv_target_include_directories(${tgt} ${X11_INCLUDE_DIR})
endif()
if(sample_filename STREQUAL "opengl3_2.cpp")
ocv_target_link_libraries(${tgt} PRIVATE ${EPOXY_LIBRARIES})
ocv_target_include_directories(${tgt} PRIVATE ${EPOXY_INCLUDE_DIRS})
endif()
endforeach()
endif()
ocv_install_example_src(opengl *.cpp *.hpp CMakeLists.txt)
+114
View File
@@ -0,0 +1,114 @@
#include <iostream>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1
#include <windows.h>
#endif
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "opencv2/core.hpp"
#include "opencv2/core/opengl.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
const int win_width = 800;
const int win_height = 640;
struct DrawData
{
ogl::Arrays arr;
ogl::Texture2D tex;
ogl::Buffer indices;
};
void draw(void* userdata);
void draw(void* userdata)
{
DrawData* data = static_cast<DrawData*>(userdata);
glRotated(0.6, 0, 1, 0);
ogl::render(data->arr, data->indices, ogl::TRIANGLES);
}
int main(int argc, char* argv[])
{
string filename;
if (argc < 2)
{
cout << "Usage: " << argv[0] << " image" << endl;
filename = "lena.jpg";
}
else
filename = argv[1];
Mat img = imread(samples::findFile(filename));
if (img.empty())
{
cerr << "Can't open image " << filename << endl;
return -1;
}
namedWindow("OpenGL", WINDOW_OPENGL);
resizeWindow("OpenGL", win_width, win_height);
Mat_<Vec2f> vertex(1, 4);
vertex << Vec2f(-1, 1), Vec2f(-1, -1), Vec2f(1, -1), Vec2f(1, 1);
Mat_<Vec2f> texCoords(1, 4);
texCoords << Vec2f(0, 0), Vec2f(0, 1), Vec2f(1, 1), Vec2f(1, 0);
Mat_<int> indices(1, 6);
indices << 0, 1, 2, 2, 3, 0;
DrawData data;
data.arr.setVertexArray(vertex);
data.arr.setTexCoordArray(texCoords);
data.indices.copyFrom(indices);
data.tex.copyFrom(img);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)win_width / win_height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
glEnable(GL_TEXTURE_2D);
data.tex.bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glDisable(GL_CULL_FACE);
setOpenGlDrawCallback("OpenGL", draw, &data);
for (;;)
{
updateWindow("OpenGL");
char key = (char)waitKey(40);
if (key == 27)
break;
}
setOpenGlDrawCallback("OpenGL", 0, 0);
destroyAllWindows();
return 0;
}
+169
View File
@@ -0,0 +1,169 @@
#include <iostream>
#include <epoxy/gl.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1
#include <windows.h>
#endif
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "opencv2/core.hpp"
#include "opencv2/core/opengl.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
const int win_width = 800;
const int win_height = 640;
struct DrawData
{
GLuint vao, vbo, program, textureID;
};
static cv::Mat rot(float angle)
{
cv::Mat R_y = (cv::Mat_<float>(4,4) <<
cos(angle), 0, sin(angle), 0,
0, 1, 0, 0,
-sin(angle), 0, cos(angle), 0,
0, 0, 0, 1);
return R_y;
}
static GLuint create_shader(const char* source, GLenum type) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
return shader;
}
static void draw(void* userdata) {
DrawData* data = static_cast<DrawData*>(userdata);
static float angle = 0.0f;
angle += 1.f;
cv::Mat trans = rot(CV_PI * angle / 360.f);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(data->program);
glUniformMatrix4fv(glGetUniformLocation(data->program, "transform"), 1, GL_FALSE, trans.ptr<float>());
glBindTexture(GL_TEXTURE_2D, data->textureID);
glBindVertexArray(data->vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
}
int main(int argc, char* argv[])
{
string filename;
if (argc < 2)
{
cout << "Usage: " << argv[0] << " image" << endl;
filename = "baboon.jpg";
}
else
filename = argv[1];
Mat img = imread(samples::findFile(filename));
if (img.empty())
{
cerr << "Can't open image " << filename << endl;
return -1;
}
flip(img, img, 0);
namedWindow("OpenGL", WINDOW_OPENGL);
resizeWindow("OpenGL", win_width, win_height);
DrawData data;
glEnable(GL_DEPTH_TEST);
const char *vertex_shader_source =
"#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec2 texCoord;\n"
"out vec2 TexCoord;\n"
"uniform mat4 transform;\n"
"void main() {\n"
" gl_Position = transform * vec4(position, 1.0);\n"
" TexCoord = texCoord;\n"
"}\n";
const char *fragment_shader_source =
"#version 330 core\n"
"in vec2 TexCoord;\n"
"out vec4 color;\n"
"uniform sampler2D ourTexture;\n"
"void main() {\n"
" color = texture(ourTexture, TexCoord);\n"
"}\n";
data.program = glCreateProgram();
GLuint vertex_shader = create_shader(vertex_shader_source, GL_VERTEX_SHADER);
GLuint fragment_shader = create_shader(fragment_shader_source, GL_FRAGMENT_SHADER);
glAttachShader(data.program, vertex_shader);
glAttachShader(data.program, fragment_shader);
glLinkProgram(data.program);
glUseProgram(data.program);
GLfloat vertices[] = {
// Positions // Texture Coords
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // Top Right
1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top Left
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f // Bottom Left
};
glGenVertexArrays(1, &data.vao);
glGenBuffers(1, &data.vbo);
glBindVertexArray(data.vao);
glBindBuffer(GL_ARRAY_BUFFER, data.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Texture Coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0); // Unbind VAO
// Image to texture
glGenTextures(1, &data.textureID);
glBindTexture(GL_TEXTURE_2D, data.textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.cols, img.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, img.data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
setOpenGlDrawCallback("OpenGL", draw, &data);
for (;;)
{
updateWindow("OpenGL");
char key = (char)waitKey(40);
if (key == 27)
break;
}
setOpenGlDrawCallback("OpenGL", 0, 0);
destroyAllWindows();
return 0;
}
+527
View File
@@ -0,0 +1,527 @@
/*
// Sample demonstrating interoperability of OpenCV UMat with OpenGL texture.
// At first, the data obtained from video file or camera and placed onto
// OpenGL texture, following mapping of this OpenGL texture to OpenCV UMat
// and call cv::Blur function. The result is mapped back to OpenGL texture
// and rendered through OpenGL API.
*/
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#elif defined(__linux__)
# include <X11/X.h>
# include <X11/Xlib.h>
#endif
#include <iostream>
#include <queue>
#include <string>
#include <stdio.h>
#include "opencv2/core.hpp"
#include "opencv2/core/opengl.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "winapp.hpp"
class GLWinApp : public WinApp
{
public:
enum MODE
{
MODE_CPU = 0,
MODE_GPU
};
GLWinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
WinApp(width, height, window_name)
{
m_shutdown = false;
m_use_buffer = false;
m_demo_processing = true;
m_mode = MODE_CPU;
m_modeStr[0] = cv::String("Processing on CPU");
m_modeStr[1] = cv::String("Processing on GPU");
m_cap = cap;
}
~GLWinApp() {}
virtual void cleanup() CV_OVERRIDE
{
m_shutdown = true;
#if defined(__linux__)
glXMakeCurrent(m_display, None, NULL);
glXDestroyContext(m_display, m_glctx);
#endif
WinApp::cleanup();
}
#if defined(_WIN32)
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) CV_OVERRIDE
{
switch (message)
{
case WM_CHAR:
if (wParam == '1')
{
set_mode(MODE_CPU);
return EXIT_SUCCESS;
}
if (wParam == '2')
{
set_mode(MODE_GPU);
return EXIT_SUCCESS;
}
else if (wParam == '9')
{
toggle_buffer();
return EXIT_SUCCESS;
}
else if (wParam == VK_SPACE)
{
m_demo_processing = !m_demo_processing;
return EXIT_SUCCESS;
}
else if (wParam == VK_ESCAPE)
{
cleanup();
return EXIT_SUCCESS;
}
break;
case WM_CLOSE:
cleanup();
return EXIT_SUCCESS;
case WM_DESTROY:
::PostQuitMessage(0);
return EXIT_SUCCESS;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
#endif
#if defined(__linux__)
int handle_event(XEvent& e) CV_OVERRIDE
{
switch(e.type)
{
case ClientMessage:
if ((Atom)e.xclient.data.l[0] == m_WM_DELETE_WINDOW)
{
m_end_loop = true;
cleanup();
}
else
{
return EXIT_SUCCESS;
}
break;
case Expose:
render();
break;
case KeyPress:
switch(keycode_to_keysym(e.xkey.keycode))
{
case XK_space:
m_demo_processing = !m_demo_processing;
break;
case XK_1:
set_mode(MODE_CPU);
break;
case XK_2:
set_mode(MODE_GPU);
break;
case XK_9:
toggle_buffer();
break;
case XK_Escape:
m_end_loop = true;
cleanup();
break;
}
break;
default:
return EXIT_SUCCESS;
}
return 1;
}
#endif
int init() CV_OVERRIDE
{
#if defined(_WIN32)
m_hDC = GetDC(m_hWnd);
if (setup_pixel_format() != 0)
{
std::cerr << "Can't setup pixel format" << std::endl;
return EXIT_FAILURE;
}
m_hRC = wglCreateContext(m_hDC);
wglMakeCurrent(m_hDC, m_hRC);
#elif defined(__linux__)
m_glctx = glXCreateContext(m_display, m_visual_info, NULL, GL_TRUE);
glXMakeCurrent(m_display, m_window, m_glctx);
#endif
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, m_width, m_height);
if (cv::ocl::haveOpenCL())
{
(void) cv::ogl::ocl::initializeContextFromGL();
}
m_oclDevName = cv::ocl::useOpenCL() ?
cv::ocl::Context::getDefault().device(0).name() :
(char*) "No OpenCL device";
return EXIT_SUCCESS;
} // init()
int get_frame(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
{
if (!m_cap.read(m_frame_bgr))
return EXIT_FAILURE;
cv::cvtColor(m_frame_bgr, m_frame_rgba, cv::COLOR_RGB2RGBA);
if (do_buffer)
buffer.copyFrom(m_frame_rgba, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
else
texture.copyFrom(m_frame_rgba, true);
return EXIT_SUCCESS;
}
void print_info(MODE mode, double time, cv::String& oclDevName)
{
#if defined(_WIN32)
HDC hDC = m_hDC;
HFONT hFont = (HFONT)::GetStockObject(SYSTEM_FONT);
HFONT hOldFont = (HFONT)::SelectObject(hDC, hFont);
if (hOldFont)
{
TEXTMETRIC tm;
::GetTextMetrics(hDC, &tm);
char buf[256+1];
int y = 0;
buf[0] = 0;
snprintf(buf, sizeof(buf), "Mode: %s OpenGL %s", m_modeStr[mode].c_str(), use_buffer() ? "buffer" : "texture");
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
snprintf(buf, sizeof(buf), "Time, msec: %2.1f", time);
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
snprintf(buf, sizeof(buf), "OpenCL device: %s", oclDevName.c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
::SelectObject(hDC, hOldFont);
}
#elif defined(__linux__)
char buf[256+1];
snprintf(buf, sizeof(buf)-1, "Time, msec: %2.1f, Mode: %s OpenGL %s, Device: %s", time, m_modeStr[mode].c_str(), use_buffer() ? "buffer" : "texture", oclDevName.c_str());
XStoreName(m_display, m_window, buf);
#endif
}
void idle() CV_OVERRIDE
{
render();
}
int render() CV_OVERRIDE
{
try
{
if (m_shutdown)
return EXIT_SUCCESS;
int r;
cv::ogl::Texture2D texture;
cv::ogl::Buffer buffer;
texture.setAutoRelease(true);
buffer.setAutoRelease(true);
MODE mode = get_mode();
bool do_buffer = use_buffer();
r = get_frame(texture, buffer, do_buffer);
if (r != 0)
{
return EXIT_FAILURE;
}
switch (mode)
{
case MODE_CPU: // process frame on CPU
processFrameCPU(texture, buffer, do_buffer);
break;
case MODE_GPU: // process frame on GPU
processFrameGPU(texture, buffer, do_buffer);
break;
} // switch
if (do_buffer) // buffer -> texture
{
cv::Mat m(m_height, m_width, CV_8UC4);
buffer.copyTo(m);
texture.copyFrom(m, true);
}
#if defined(__linux__)
XWindowAttributes window_attributes;
XGetWindowAttributes(m_display, m_window, &window_attributes);
glViewport(0, 0, window_attributes.width, window_attributes.height);
#endif
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.1f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.1f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.1f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.1f);
glEnd();
#if defined(_WIN32)
SwapBuffers(m_hDC);
#elif defined(__linux__)
glXSwapBuffers(m_display, m_window);
#endif
print_info(mode, m_timer.getTimeMilli(), m_oclDevName);
}
catch (const cv::Exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
return 10;
}
return EXIT_SUCCESS;
}
protected:
void processFrameCPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
{
cv::Mat m(m_height, m_width, CV_8UC4);
m_timer.reset();
m_timer.start();
if (do_buffer)
buffer.copyTo(m);
else
texture.copyTo(m);
if (m_demo_processing)
{
// blur texture image with OpenCV on CPU
cv::blur(m, m, cv::Size(15, 15));
}
if (do_buffer)
buffer.copyFrom(m, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
else
texture.copyFrom(m, true);
m_timer.stop();
}
void processFrameGPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
{
cv::UMat u;
m_timer.reset();
m_timer.start();
if (do_buffer)
u = cv::ogl::mapGLBuffer(buffer);
else
cv::ogl::convertFromGLTexture2D(texture, u);
if (m_demo_processing)
{
// blur texture image with OpenCV on GPU with OpenCL
cv::blur(u, u, cv::Size(15, 15));
}
if (do_buffer)
cv::ogl::unmapGLBuffer(u);
else
cv::ogl::convertToGLTexture2D(u, texture);
m_timer.stop();
}
#if defined(_WIN32)
int setup_pixel_format()
{
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cRedBits = 8;
pfd.cRedShift = 0;
pfd.cGreenBits = 8;
pfd.cGreenShift = 0;
pfd.cBlueBits = 8;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 8;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
int pfmt = ChoosePixelFormat(m_hDC, &pfd);
if (pfmt == 0)
return EXIT_FAILURE;
if (SetPixelFormat(m_hDC, pfmt, &pfd) == 0)
return -2;
return EXIT_SUCCESS;
}
#endif
#if defined(__linux__)
KeySym keycode_to_keysym(unsigned keycode)
{ // note that XKeycodeToKeysym() is considered deprecated
int keysyms_per_keycode_return = 0;
KeySym *keysyms = XGetKeyboardMapping(m_display, keycode, 1, &keysyms_per_keycode_return);
KeySym keysym = keysyms[0];
XFree(keysyms);
return keysym;
}
#endif
bool use_buffer() { return m_use_buffer; }
void toggle_buffer() { m_use_buffer = !m_use_buffer; }
MODE get_mode() { return m_mode; }
void set_mode(MODE mode) { m_mode = mode; }
private:
bool m_shutdown;
bool m_use_buffer;
bool m_demo_processing;
MODE m_mode;
cv::String m_modeStr[2];
#if defined(_WIN32)
HDC m_hDC;
HGLRC m_hRC;
#elif defined(__linux__)
GLXContext m_glctx;
#endif
cv::VideoCapture m_cap;
cv::Mat m_frame_bgr;
cv::Mat m_frame_rgba;
cv::String m_oclDevName;
};
static const char* keys =
{
"{c camera | 0 | camera id }"
"{f file | | movie file name }"
};
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, keys);
int camera_id = parser.get<int>("camera");
string file = parser.get<string>("file");
parser.about(
"\nA sample program demonstrating interoperability of OpenGL and OpenCL with OpenCV.\n\n"
"Hot keys: \n"
" SPACE - turn processing on/off\n"
" 1 - process GL data through OpenCV on CPU\n"
" 2 - process GL data through OpenCV on GPU (via OpenCL)\n"
" 9 - toggle use of GL texture/GL buffer\n"
" ESC - exit\n\n");
parser.printMessage();
cv::VideoCapture cap;
if (file.empty())
cap.open(camera_id);
else
cap.open(file.c_str());
if (!cap.isOpened())
{
printf("can not open camera or video file\n");
return EXIT_FAILURE;
}
int width = (int)cap.get(CAP_PROP_FRAME_WIDTH);
int height = (int)cap.get(CAP_PROP_FRAME_HEIGHT);
#if defined(_WIN32)
string wndname = "WGL Window";
#elif defined(__linux__)
string wndname = "GLX Window";
#endif
GLWinApp app(width, height, wndname, cap);
try
{
app.create();
return app.run();
}
catch (const cv::Exception& e)
{
cerr << "Exception: " << e.what() << endl;
return 10;
}
catch (...)
{
cerr << "FATAL ERROR: Unknown exception" << endl;
return 11;
}
}
@@ -0,0 +1,549 @@
#include <iostream>
#include <map>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1
#include <windows.h>
#endif
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "opencv2/core.hpp"
#include "opencv2/core/opengl.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/geometry.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
// model data should be identical to the code from tests
enum class ModelType
{
Empty = 0,
File = 1,
Clipping = 2,
Color = 3,
Centered = 4
};
static void generateNormals(const std::vector<Vec3f>& points, const std::vector<std::vector<int>>& indices,
std::vector<Vec3f>& normals)
{
std::vector<std::vector<Vec3f>> preNormals(points.size(), std::vector<Vec3f>());
for (const auto& tri : indices)
{
Vec3f p0 = points[tri[0]];
Vec3f p1 = points[tri[1]];
Vec3f p2 = points[tri[2]];
Vec3f cross = cv::normalize((p1 - p0).cross(p2 - p0));
for (int i = 0; i < 3; i++)
{
preNormals[tri[i]].push_back(cross);
}
}
normals.reserve(points.size());
for (const auto& pn : preNormals)
{
Vec3f sum { };
for (const auto& n : pn)
{
sum += n;
}
normals.push_back(cv::normalize(sum));
}
}
class ModelData
{
public:
ModelData(ModelType type = ModelType::Empty, std::string objPath = { })
{
switch (type)
{
case ModelType::Empty:
{
position = Vec3d(0.0, 0.0, 0.0);
lookat = Vec3d(0.0, 0.0, 0.0);
upVector = Vec3d(0.0, 1.0, 0.0);
fovy = 45.0;
zNear = 0.1;
zFar = 50;
scaleCoeff = 1000.0;
vertices = std::vector<Vec3f>(4, {2.0f, 0, -2.0f});
colors = std::vector<Vec3f>(4, {0, 0, 1.0f});
indices = { };
}
break;
case ModelType::File:
{
position = Vec3d( 1.9, 0.4, 1.3);
lookat = Vec3d( 0.0, 0.0, 0.0);
upVector = Vec3d( 0.0, 1.0, 0.0);
fovy = 45.0;
zNear = 0.1;
zFar = 50;
scaleCoeff = 1000.0;
objectPath = objPath;
std::vector<vector<int>> indvec;
loadMesh(objectPath, vertices, indvec);
// using per-vertex normals as colors
generateNormals(vertices, indvec, colors);
if (vertices.size() != colors.size())
{
std::runtime_error("Model should contain normals for each vertex");
}
for (const auto &vec : indvec)
{
indices.push_back({vec[0], vec[1], vec[2]});
}
for (auto &color : colors)
{
color = Vec3f(abs(color[0]), abs(color[1]), abs(color[2]));
}
}
break;
case ModelType::Clipping:
{
position = Vec3d(0.0, 0.0, 5.0);
lookat = Vec3d(0.0, 0.0, 0.0);
upVector = Vec3d(0.0, 1.0, 0.0);
fovy = 45.0;
zNear = 0.1;
zFar = 50;
scaleCoeff = 1000.0;
vertices =
{
{ 2.0, 0.0, -2.0}, { 0.0, -6.0, -2.0}, {-2.0, 0.0, -2.0},
{ 3.5, -1.0, -5.0}, { 2.5, -2.5, -5.0}, {-1.0, 1.0, -5.0},
{-6.5, -1.0, -3.0}, {-2.5, -2.0, -3.0}, { 1.0, 1.0, -5.0},
};
indices = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
Vec3f col1(217.0, 238.0, 185.0);
Vec3f col2(185.0, 217.0, 238.0);
Vec3f col3(150.0, 10.0, 238.0);
col1 *= (1.f / 255.f);
col2 *= (1.f / 255.f);
col3 *= (1.f / 255.f);
colors =
{
col1, col2, col3,
col2, col3, col1,
col3, col1, col2,
};
}
break;
case ModelType::Centered:
{
position = Vec3d(0.0, 0.0, 5.0);
lookat = Vec3d(0.0, 0.0, 0.0);
upVector = Vec3d(0.0, 1.0, 0.0);
fovy = 45.0;
zNear = 0.1;
zFar = 50;
scaleCoeff = 1000.0;
vertices =
{
{ 2.0, 0.0, -2.0}, { 0.0, -2.0, -2.0}, {-2.0, 0.0, -2.0},
{ 3.5, -1.0, -5.0}, { 2.5, -1.5, -5.0}, {-1.0, 0.5, -5.0},
};
indices = { {0, 1, 2}, {3, 4, 5} };
Vec3f col1(217.0, 238.0, 185.0);
Vec3f col2(185.0, 217.0, 238.0);
col1 *= (1.f / 255.f);
col2 *= (1.f / 255.f);
colors =
{
col1, col2, col1,
col2, col1, col2,
};
}
break;
case ModelType::Color:
{
position = Vec3d(0.0, 0.0, 5.0);
lookat = Vec3d(0.0, 0.0, 0.0);
upVector = Vec3d(0.0, 1.0, 0.0);
fovy = 60.0;
zNear = 0.1;
zFar = 50;
scaleCoeff = 1000.0;
vertices =
{
{ 2.0, 0.0, -2.0},
{ 0.0, 2.0, -3.0},
{-2.0, 0.0, -2.0},
{ 0.0, -2.0, 1.0},
};
indices = { {0, 1, 2}, {0, 2, 3} };
colors =
{
{ 0.0f, 0.0f, 1.0f},
{ 0.0f, 1.0f, 0.0f},
{ 1.0f, 0.0f, 0.0f},
{ 0.0f, 1.0f, 0.0f},
};
}
break;
default:
CV_Error(Error::StsBadArg, "Unknown model type");
break;
}
}
ModelData(std::string modelPath, double fov, double nearPlane, double farPlane, double scale, Vec3d pos, Vec3d center, Vec3d up)
{
objectPath = modelPath;
position = pos;
lookat = center;
upVector = up;
fovy = fov;
zNear = nearPlane;
zFar = farPlane;
scaleCoeff = scale;
std::vector<vector<int>> indvec;
loadMesh(objectPath, vertices, indvec, noArray(), colors);
if (vertices.size() != colors.size())
{
std::runtime_error("Model should contain normals for each vertex");
}
for (const auto &vec : indvec)
{
indices.push_back({vec[0], vec[1], vec[2]});
}
}
Vec3d position;
Vec3d lookat;
Vec3d upVector;
double fovy, zNear, zFar, scaleCoeff;
std::vector<Vec3f> vertices;
std::vector<Vec3i> indices;
std::vector<Vec3f> colors;
string objectPath;
};
struct DrawData
{
ogl::Arrays arr;
ogl::Buffer indices;
};
void draw(void* userdata);
void draw(void* userdata)
{
DrawData* data = static_cast<DrawData*>(userdata);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ogl::render(data->arr, data->indices, ogl::TRIANGLES);
}
static void generateImage(cv::Size imgSz, TriangleShadingType shadingType, TriangleCullingMode cullingMode,
const ModelData& modelData, cv::Mat& colorImage, cv::Mat& depthImage)
{
namedWindow("OpenGL", WINDOW_OPENGL);
resizeWindow("OpenGL", imgSz.width, imgSz.height);
DrawData data;
std::vector<Vec3f> vertices;
std::vector<Vec4f> colors4f;
std::vector<int> idxLinear;
if (shadingType == RASTERIZE_SHADING_FLAT)
{
// rearrange vertices and colors for flat shading
int ctr = 0;
for (const auto& idx : modelData.indices)
{
for (int i = 0; i < 3; i++)
{
vertices.push_back(modelData.vertices[idx[i]]);
idxLinear.push_back(ctr++);
}
Vec3f ci = modelData.colors[idx[0]];
for (int i = 0; i < 3; i++)
{
colors4f.emplace_back(ci[0], ci[1], ci[2], 1.f);
}
}
}
else
{
vertices = modelData.vertices;
for (const auto& c : modelData.colors)
{
Vec3f ci = (shadingType == RASTERIZE_SHADING_SHADED) ? c: cv::Vec3f::all(1.f);
colors4f.emplace_back(ci[0], ci[1], ci[2], 1.0);
}
for (const auto& idx : modelData.indices)
{
for (int i = 0; i < 3; i++)
{
idxLinear.push_back(idx[i]);
}
}
}
data.arr.setVertexArray(vertices);
data.arr.setColorArray(colors4f);
data.indices.copyFrom(idxLinear);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(modelData.fovy, (double)imgSz.width / imgSz.height, modelData.zNear, modelData.zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
gluLookAt(modelData.position[0], modelData.position[1], modelData.position[2],
modelData.lookat [0], modelData.lookat [1], modelData.lookat [2],
modelData.upVector[0], modelData.upVector[1], modelData.upVector[2]);
if (cullingMode == RASTERIZE_CULLING_NONE)
{
glDisable(GL_CULL_FACE);
}
else
{
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
if (cullingMode == RASTERIZE_CULLING_CW)
{
glFrontFace(GL_CW);
}
else
{
glFrontFace(GL_CCW);
}
}
glEnable(GL_DEPTH_TEST);
cv::setOpenGlDrawCallback("OpenGL", draw, &data);
const int framesToSkip = 10;
for (int f = 0; f < framesToSkip; f++)
{
updateWindow("OpenGL");
colorImage = cv::Mat(imgSz.height, imgSz.width, CV_8UC3);
glReadPixels(0, 0, imgSz.width, imgSz.height, GL_RGB, GL_UNSIGNED_BYTE, colorImage.data);
cv::cvtColor(colorImage, colorImage, cv::COLOR_RGB2BGR);
cv::flip(colorImage, colorImage, 0);
depthImage = cv::Mat(imgSz.height, imgSz.width, CV_32F);
glReadPixels(0, 0, imgSz.width, imgSz.height, GL_DEPTH_COMPONENT, GL_FLOAT, depthImage.data);
// map from [0, 1] to [zNear, zFar]
for (auto it = depthImage.begin<float>(); it != depthImage.end<float>(); ++it)
{
*it = (float)(modelData.zNear * modelData.zFar / (double(*it) * (modelData.zNear - modelData.zFar) + modelData.zFar));
}
cv::flip(depthImage, depthImage, 0);
depthImage.convertTo(depthImage, CV_16U, modelData.scaleCoeff);
char key = (char)waitKey(40);
if (key == 27)
break;
}
cv::setOpenGlDrawCallback("OpenGL", 0, 0);
cv::destroyAllWindows();
}
int main(int argc, char* argv[])
{
cv::CommandLineParser parser(argc, argv,
"{ help h usage ? | | show this message }"
"{ outPath | | output path for generated images }"
"{ modelPath | | path to 3d model to render }"
"{ custom | | pass it to use custom camera parameters instead of iterating through test parameters }"
"{ fov | 45.0 | (if custom parameters are used) field of view }"
"{ posx | 1.0 | (if custom parameters are used) camera position x }"
"{ posy | 1.0 | (if custom parameters are used) camera position y }"
"{ posz | 1.0 | (if custom parameters are used) camera position z }"
"{ lookatx | 0.0 | (if custom parameters are used) lookup camera direction x }"
"{ lookaty | 0.0 | (if custom parameters are used) lookup camera direction y }"
"{ lookatz | 0.0 | (if custom parameters are used) lookup camera direction z }"
"{ upx | 0.0 | (if custom parameters are used) up camera direction x }"
"{ upy | 1.0 | (if custom parameters are used) up camera direction y }"
"{ upz | 0.0 | (if custom parameters are used) up camera direction z }"
"{ resx | 640 | (if custom parameters are used) camera resolution x }"
"{ resy | 480 | (if custom parameters are used) camera resolution y }"
"{ zNear | 0.1 | (if custom parameters are used) near z clipping plane }"
"{ zFar | 50 | (if custom parameters are used) far z clipping plane }"
"{ scaleCoeff | 1000 | (if custom parameters are used) scale coefficient for saving depth }"
"{ shading | | (if custom parameters are used) shading type: white/flat/shaded }"
"{ culling | | (if custom parameters are used) culling type: none/cw/ccw }"
"{ colorPath | | (if custom parameters are used) output path for color image }"
"{ depthPath | | (if custom parameters are used) output path for depth image }"
);
parser.about("This app is used to generate test data for triangleRasterize() function");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
std::string modelPath = parser.get<std::string>("modelPath");
if (modelPath.empty())
{
std::cout << "No model path given" << std::endl;
return -1;
}
if (parser.has("custom"))
{
double fov = parser.get<double>("fov");
Vec3d position, lookat, upVector;
position[0] = parser.get<double>("posx");
position[1] = parser.get<double>("posy");
position[2] = parser.get<double>("posz");
lookat[0] = parser.get<double>("lookatx");
lookat[1] = parser.get<double>("lookaty");
lookat[2] = parser.get<double>("lookatz");
upVector[0] = parser.get<double>("upx");
upVector[1] = parser.get<double>("upy");
upVector[2] = parser.get<double>("upz");
Size res;
res.width = parser.get<int>("resx");
res.height = parser.get<int>("resy");
double zNear = parser.get<double>("zNear");
double zFar = parser.get<double>("zFar");
double scaleCoeff = parser.get<double>("scaleCoeff");
std::map<std::string, cv::TriangleShadingType> shadingTxt = {
{ "white", RASTERIZE_SHADING_WHITE },
{ "flat", RASTERIZE_SHADING_FLAT },
{ "shaded", RASTERIZE_SHADING_SHADED },
};
cv::TriangleShadingType shadingType = shadingTxt.at(parser.get<std::string>("shading"));
std::map<std::string, cv::TriangleCullingMode> cullingTxt = {
{ "none", RASTERIZE_CULLING_NONE },
{ "cw", RASTERIZE_CULLING_CW },
{ "ccw", RASTERIZE_CULLING_CCW },
};
cv::TriangleCullingMode cullingMode = cullingTxt.at(parser.get<std::string>("culling"));
std::string colorPath = parser.get<std::string>("colorPath");
std::string depthPath = parser.get<std::string>("depthPath");
Mat colorImage, depthImage;
ModelData modelData(modelPath, fov, zNear, zFar, scaleCoeff, position, lookat, upVector);
generateImage(res, shadingType, cullingMode, modelData, colorImage, depthImage);
cv::imwrite(colorPath, colorImage);
cv::imwrite(depthPath, depthImage);
}
else
{
std::string outPath = parser.get<std::string>("outPath");
if (outPath.empty())
{
std::cout << "No output path given" << std::endl;
return -1;
}
std::array<cv::Size, 4> resolutions = {cv::Size{700, 700}, cv::Size{640, 480}, cv::Size(256, 256), cv::Size(320, 240)};
std::vector<std::pair<cv::TriangleShadingType, std::string>> shadingTxt = {
{RASTERIZE_SHADING_WHITE, "White"},
{RASTERIZE_SHADING_FLAT, "Flat"},
{RASTERIZE_SHADING_SHADED, "Shaded"},
};
std::vector<std::pair<cv::TriangleCullingMode, std::string>> cullingTxt = {
{RASTERIZE_CULLING_NONE, "None"},
{RASTERIZE_CULLING_CW, "CW"},
{RASTERIZE_CULLING_CCW, "CCW"},
};
std::vector<std::pair<ModelType, std::string>> modelTxt = {
{ModelType::File, "File"},
{ModelType::Clipping, "Clipping"},
{ModelType::Color, "Color"},
{ModelType::Centered, "Centered"},
};
for (const auto& res : resolutions)
{
for (const auto shadingPair : shadingTxt)
{
cv::TriangleShadingType shadingType = shadingPair.first;
std::string shadingName = shadingPair.second;
for (const auto cullingPair : cullingTxt)
{
cv::TriangleCullingMode cullingMode = cullingPair.first;
std::string cullingName = cullingPair.second;
for (const auto modelPair : modelTxt)
{
ModelType modelType = modelPair.first;
std::string modelName = modelPair.second;
std::string suffix = cv::format("%s_%dx%d_Cull%s", modelName.c_str(), res.width, res.height, cullingName.c_str());
std::cout << suffix + "_" + shadingName << "..." << std::endl;
cv::Mat colorImage, depthImage;
ModelData modelData(modelType, modelPath);
generateImage(res, shadingType, cullingMode, modelData, colorImage, depthImage);
std::string gtPathColor = outPath + "/example_image_" + suffix + "_" + shadingName + ".png";
std::string gtPathDepth = outPath + "/depth_image_" + suffix + ".png";
cv::imwrite(gtPathColor, colorImage);
cv::imwrite(gtPathDepth, depthImage);
}
}
}
}
}
return 0;
}
+222
View File
@@ -0,0 +1,222 @@
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#elif defined(__linux__)
# include <X11/X.h>
# include <X11/Xlib.h>
# include <X11/Xutil.h>
#endif
#include <string>
#include <GL/gl.h>
#if defined(_WIN32)
# include <GL/glu.h>
#elif defined(__linux__)
# include <GL/glx.h>
#endif
#if defined(_WIN32)
# define WINCLASS "WinAppWnd"
#endif
#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
class WinApp
{
public:
WinApp(int width, int height, std::string& window_name)
{
m_width = width;
m_height = height;
m_window_name = window_name;
#if defined(_WIN32)
m_hInstance = ::GetModuleHandle(NULL);
#endif
}
virtual ~WinApp()
{
#if defined(_WIN32)
::UnregisterClass(WINCLASS, m_hInstance);
#endif
}
int create()
{
#if defined(_WIN32)
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = &WinApp::StaticWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = m_hInstance;
wcex.hIcon = LoadIcon(0, IDI_APPLICATION);
wcex.hCursor = LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0L;
wcex.lpszClassName = WINCLASS;
wcex.hIconSm = 0;
ATOM wc = ::RegisterClassEx(&wcex);
RECT rc = { 0, 0, m_width, m_height };
::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
m_hWnd = ::CreateWindow(
(LPCTSTR)wc, m_window_name.c_str(),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
rc.right - rc.left, rc.bottom - rc.top,
NULL, NULL, m_hInstance, (void*)this);
if (!m_hWnd)
return -1;
::ShowWindow(m_hWnd, SW_SHOW);
::UpdateWindow(m_hWnd);
::SetFocus(m_hWnd);
#elif defined(__linux__)
m_display = XOpenDisplay(NULL);
if (m_display == NULL)
{
return -1;
}
m_WM_DELETE_WINDOW = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
static GLint visual_attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
m_visual_info = glXChooseVisual(m_display, 0, visual_attributes);
if (m_visual_info == NULL)
{
XCloseDisplay(m_display);
return -2;
}
Window root = DefaultRootWindow(m_display);
m_event_mask = ExposureMask | KeyPressMask;
XSetWindowAttributes window_attributes;
window_attributes.colormap = XCreateColormap(m_display, root, m_visual_info->visual, AllocNone);
window_attributes.event_mask = m_event_mask;
m_window = XCreateWindow(
m_display, root, 0, 0, m_width, m_height, 0, m_visual_info->depth,
InputOutput, m_visual_info->visual, CWColormap | CWEventMask, &window_attributes);
XMapWindow(m_display, m_window);
XSetWMProtocols(m_display, m_window, &m_WM_DELETE_WINDOW, 1);
XStoreName(m_display, m_window, m_window_name.c_str());
#endif
return init();
}
virtual void cleanup()
{
#if defined(_WIN32)
::DestroyWindow(m_hWnd);
#elif defined(__linux__)
XDestroyWindow(m_display, m_window);
XCloseDisplay(m_display);
#endif
}
#if defined(_WIN32)
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
#endif
int run()
{
#if defined(_WIN32)
MSG msg;
::ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
{
idle();
}
}
return static_cast<int>(msg.wParam);
#elif defined(__linux__)
m_end_loop = false;
do {
XEvent e;
if (!XCheckWindowEvent(m_display, m_window, m_event_mask, &e) || !handle_event(e))
{
idle();
}
} while (!m_end_loop);
return 0;
#endif
}
protected:
#if defined(_WIN32)
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WinApp* pWnd;
if (message == WM_NCCREATE)
{
LPCREATESTRUCT pCreateStruct = ((LPCREATESTRUCT)lParam);
pWnd = (WinApp*)(pCreateStruct->lpCreateParams);
::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pWnd);
}
pWnd = GetObjectFromWindow(hWnd);
if (pWnd)
return pWnd->WndProc(hWnd, message, wParam, lParam);
else
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
inline static WinApp* GetObjectFromWindow(HWND hWnd)
{
return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
}
#endif
#if defined(__linux__)
virtual int handle_event(XEvent& e) = 0;
#endif
virtual int init() = 0;
virtual int render() = 0;
virtual void idle() = 0;
#if defined(_WIN32)
HINSTANCE m_hInstance;
HWND m_hWnd;
#elif defined(__linux__)
Display* m_display;
XVisualInfo* m_visual_info;
Window m_window;
long m_event_mask;
Atom m_WM_DELETE_WINDOW;
bool m_end_loop;
#endif
int m_width;
int m_height;
std::string m_window_name;
cv::TickMeter m_timer;
};