-
-
Save envyen/2165502708b3bab00b95c2436481673c to your computer and use it in GitHub Desktop.
First Samplle for OpenCV with ImGui
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required( VERSION 3.6 ) | |
set( CMAKE_CXX_STANDARD 11 ) | |
set( CMAKE_CXX_STANDARD_REQUIRED ON ) | |
set( CMAKE_CXX_EXTENSIONS OFF ) | |
find_package( Git ) | |
execute_process( | |
COMMAND ${GIT_EXECUTABLE} clone "https://github.com/ocornut/imgui.git" -b v1.72b | |
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} | |
) | |
set( IMGUI_DIR ${CMAKE_CURRENT_BINARY_DIR}/imgui ) | |
set( imgui_files | |
${IMGUI_DIR}/imconfig.h | |
${IMGUI_DIR}/imgui.cpp | |
${IMGUI_DIR}/imgui.h | |
${IMGUI_DIR}/imgui_demo.cpp | |
${IMGUI_DIR}/imgui_draw.cpp | |
${IMGUI_DIR}/imgui_internal.h | |
${IMGUI_DIR}/imgui_widgets.cpp | |
${IMGUI_DIR}/imstb_rectpack.h | |
${IMGUI_DIR}/imstb_textedit.h | |
${IMGUI_DIR}/imstb_truetype.h | |
) | |
set( imgui_impl_files | |
${IMGUI_DIR}/examples/imgui_impl_glfw.h | |
${IMGUI_DIR}/examples/imgui_impl_glfw.cpp | |
${IMGUI_DIR}/examples/imgui_impl_opengl3.h | |
${IMGUI_DIR}/examples/imgui_impl_opengl3.cpp | |
) | |
set( gl3w | |
${IMGUI_DIR}/examples/libs/gl3w/GL/gl3w.c | |
) | |
project( sample ) | |
add_executable( sample ${imgui_files} ${imgui_impl_files} ${gl3w} main.cpp ) | |
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "sample" ) | |
find_package( OpenCV REQUIRED ) | |
find_package( OpenGL REQUIRED ) | |
include( ExternalProject ) | |
ExternalProject_Add( | |
glfw PREFIX glfw | |
GIT_REPOSITORY https://github.com/glfw/glfw.git | |
GIT_TAG 3.3 | |
CMAKE_ARGS | |
"-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>" | |
"-DCMAKE_BUILD_TYPE=Release" | |
"-DGLFW_BUILD_DOCS=OFF" | |
"-DGLFW_BUILD_EXAMPLES=OFF" | |
"-DGLFW_BUILD_TESTS=OFF" | |
) | |
ExternalProject_Get_Property( glfw INSTALL_DIR ) | |
set( GLFW_DIR ${INSTALL_DIR} ) | |
set( GLFW_INCLUDE_DIR ${GLFW_DIR}/include ) | |
set( GLFW_LIBRARIES ${GLFW_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}glfw3${CMAKE_STATIC_LIBRARY_SUFFIX} ) | |
add_dependencies( sample glfw ) | |
if( OpenCV_FOUND AND OPENGL_FOUND ) | |
include_directories( ${IMGUI_DIR} ) | |
include_directories( ${IMGUI_DIR}/examples ) | |
include_directories( ${IMGUI_DIR}/examples/libs/gl3w ) | |
include_directories( ${OPENGL_INCLUDE_DIR} ) | |
include_directories( ${GLFW_INCLUDE_DIR} ) | |
target_link_libraries( sample ${OpenCV_LIBS} ) | |
target_link_libraries( sample ${OPENGL_LIBRARIES} ) | |
target_link_libraries( sample ${GLFW_LIBRARIES} ) | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <opencv2/opencv.hpp> | |
#include "imgui.h" | |
#include "imgui_impl_glfw.h" | |
#include "imgui_impl_opengl3.h" | |
#include <GL/gl3w.h> | |
#include <GLFW/glfw3.h> | |
int main( int argc, char* argv[] ) | |
{ | |
cv::Mat image = cv::imread( "../lena.jpg", cv::IMREAD_COLOR ); | |
if( image.empty() ){ | |
return -1; | |
} | |
cv::cvtColor( image, image, cv::COLOR_BGR2RGBA ); | |
if( !glfwInit() ){ | |
return -1; | |
} | |
GLFWwindow* window = glfwCreateWindow( 800, 600, "glfw window", nullptr, nullptr ); | |
glfwSetWindowCloseCallback( window, []( GLFWwindow* window ){ glfwSetWindowShouldClose( window, GL_FALSE ); } ); | |
glfwMakeContextCurrent( window ); | |
glfwSwapInterval( 1 ); | |
gl3wInit(); | |
IMGUI_CHECKVERSION(); | |
ImGui::CreateContext(); | |
ImGui_ImplGlfw_InitForOpenGL( window, true ); | |
ImGui_ImplOpenGL3_Init( "#version 330" ); | |
bool is_show = true; | |
while( is_show ){ | |
glfwPollEvents(); | |
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); | |
glClear( GL_COLOR_BUFFER_BIT ); | |
ImGui_ImplOpenGL3_NewFrame(); | |
ImGui_ImplGlfw_NewFrame(); | |
ImGui::NewFrame(); | |
ImGui::Begin( "imgui image", &is_show ); | |
GLuint texture; | |
glGenTextures( 1, &texture ); | |
glBindTexture( GL_TEXTURE_2D, texture ); | |
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); | |
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); | |
glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 ); | |
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, image.cols, image.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.data ); | |
ImGui::Image( reinterpret_cast<void*>( static_cast<intptr_t>( texture ) ), ImVec2( image.cols, image.rows ) ); | |
ImGui::End(); | |
ImGui::Render(); | |
ImGui_ImplOpenGL3_RenderDrawData( ImGui::GetDrawData() ); | |
glfwSwapBuffers( window ); | |
} | |
ImGui_ImplGlfw_Shutdown(); | |
ImGui_ImplOpenGL3_Shutdown(); | |
ImGui::DestroyContext(); | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment