imgui 개발 노트 <3>, 이미지 카메라

카메라에서 이미지 가져오기, 디스플레이

//
// Created by sry on 2021/6/30.
//

#include"imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
// Initialize with gl3wInit()
#include<GL/gl3w.h>
// Include glfw3.h after our OpenGL definitions
#include <GLFW/glfw3.h>
#include<iostream>

#include<opencv2/opencv.hpp>

GLuint textureID;
cv::VideoCapture vcap;
bool FlagWetCamera = false;
bool FlagOpenCamera = false;

void CreateTexture1(cv::VideoCapture vcap)
{
    cv::Mat image;
    if (!vcap.read(image))
    {
        printf("Can not load Cameras\n");
    }
    else
    {
        //设置长宽
        int width = image.cols;
        int height = image.rows;
        int channel = image.channels();

        //获取图像指针
        int pixellength = width * height * channel;
        GLubyte* pixels = new GLubyte[pixellength];
        memcpy(pixels, image.data, pixellength * sizeof(char));

        // 使用GL指令生成贴图,获取贴图ID
        glGenTextures(1, &textureID);
        glBindTexture(GL_TEXTURE_2D, textureID);
        //必须一个RGB  一个BGR(opencv的mat类的颜色通道是BGR) 否则会出现颜色偏差
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, pixels);
        //纹理放大缩小使用线性插值
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glBindTexture(GL_TEXTURE_2D, 0);
        free(pixels);
    }
}

static void glfw_error_callback(int error, const char* description)
{

    fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

int main()
{
    glfwSetErrorCallback(glfw_error_callback);
    if (!glfwInit())
    {
        return 1;
    }
    const char* glsl_version = "#version 130"; //
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    // 创建窗口
    GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", NULL, NULL);
    if (window == NULL)
    {
        return 1;
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); //  开启垂直同步

    if (gl3wInit() != 0)
    {
        fprintf(stderr, "Failed to initialize OpenGL loader!\n");
        return -1;
    }
    // 启动 ImGui 上下文
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    (void)io;

    io.WantSaveIniSettings = true;
    io.IniFilename = "sry.ini";

    // 主题
    ImGui::StyleColorsDark();
    // 启动 平台、渲染 后端
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init(glsl_version);
    ImVec4 clear_color = ImVec4(0.f, 0.75f, 0.f, 0.30f);

    // 基于OpenCV加载图片
    

    //是否正常打开相机标识
    FlagWetCamera = vcap.open(1);
  


    while (!glfwWindowShouldClose(window))
    {
        // 处理所有待办时间
        glfwPollEvents();
        // 启动imgui frame,刷新帧
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();
      
        // 显示图像
        {
            // //设置窗口的padding为0是图片控件充满窗口
            ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
            //设置窗口的边框大小为0
            ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);
            // 改变窗口的背景颜色
            ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 100, 0.3));

            ImGui::Begin("camera", NULL, ImGuiWindowFlags_NoBringToFrontOnFocus);

            CreateTexture1(vcap);

            ImTextureID image_id = (GLuint*)textureID;
            ImGui::Image(image_id, ImGui::GetContentRegionAvail());
            //ImGui::Image(image_id, ImGui::GetContentRegionAvail(), ImVec2(0.0, 0.0), ImVec2(1.0, 1.0), ImVec4(0, 0, 255, 1), ImVec4(0, 255, 0, 1));

            ImGui::End();

            ImGui::PopStyleVar(2);
            ImGui::PopStyleColor(1);

        }


        // 渲染子窗口
        ImGui::Render();
        // 主窗口背景颜色
        gl3wClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
        glClear(GL_COLOR_BUFFER_BIT);

        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        glfwSwapBuffers(window);
    }

    // Release
    
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwDestroyWindow(window);
    glfwTerminate();
    return 1;
}

Supongo que te gusta

Origin blog.csdn.net/m0_72734364/article/details/133428282
Recomendado
Clasificación