Skip to content

Instantly share code, notes, and snippets.

@TheEpicFace007
Forked from UnaNancyOwen/CMakeLists.txt
Created January 5, 2021 01:31
Show Gist options
  • Save TheEpicFace007/777af742cb233a7d97920c94eebe5b1e to your computer and use it in GitHub Desktop.
Save TheEpicFace007/777af742cb233a7d97920c94eebe5b1e to your computer and use it in GitHub Desktop.
First Samplle for OpenCV with ImGui
#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