How to show OpenCV video in OpenGL window

asked 2016-09-02 05:16:28 -0600

updated 2016-09-02 07:06:17 -0600

berak gravatar image

Hello, I'm looking for the way to show in a openGL window the video signal that I'm capturing from my ip camera with openCV. The reason that I need this is, because when the openGL window is activated I can send HTTP commands to my camera to move it with keyboard cursos.

I show you my code below, both OpenCV and OpenGL work perfectly independent, but I would like to integrate both. So you can see the video signal while you are moving it. Can you let me know how I can do this. I'm very new with OpenGL and OpenCV.

Thanks in advance.

#include "curlpp/cURLpp.hpp"
#include "curlpp/Options.hpp"


#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <GLUT/glut.h>


using namespace std;


//Inicio curl
curlpp::Cleanup myCleanup;

std::ostringstream os;

 //process menu option 'op'
void menu(int op) {

    switch(op) {
        case 'Q':
        case 'q':
            exit(0);
    }
}

//executed when a regular key is pressed
void keyboardDown(unsigned char key, int x, int y) {


    switch(key) {
        case 'Q':
        case 'q':
        case  27:   // ESC
            exit(0);
    }
}

//executed when a regular key is released
void keyboardUp(unsigned char key, int x, int y) {

}



//executed when a special key is pressed
void keyboardSpecialDown(int k, int x, int y) {





    switch(k)
    {
        case GLUT_KEY_UP:
            {
            //Mueve la camara hacia la arriba
            os << curlpp::options::Url("http://192.168.1.90/axis-cgi/com/ptz.cgi?move=up");
            }
            break;
        case GLUT_KEY_DOWN:
            {
            //Mueve la camara hacia la abajo
            os << curlpp::options::Url("http://192.168.1.90/axis-cgi/com/ptz.cgi?move=down");
            }
            break;
        case GLUT_KEY_LEFT:
            {
            //Mueve la camara hacia la izquierda
            os << curlpp::options::Url("http://192.168.1.90/axis-cgi/com/ptz.cgi?move=left");
            }
            break;

        case GLUT_KEY_RIGHT:
            {
            //Mueve la camara hacia la derecha
            os << curlpp::options::Url("http://192.168.1.90/axis-cgi/com/ptz.cgi?move=right");
            }
            break;
    }

}

//executed when a special key is released
void keyboardSpecialUp(int k, int x, int y) {

}

//reshaped window
void reshape(int width, int height) {

    GLfloat fieldOfView = 90.0f;
    glViewport (0, 0, (GLsizei) width, (GLsizei) height);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}




//render the scene
void draw() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //render the scene here

    glFlush();
    glutSwapBuffers();
}

//executed when program is idle
void idle() {

}

//initialize OpenGL settings
void initGL(int width, int height) {

    reshape(width, height);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

//initialize GLUT settings, register callbacks, enter main loop
int main(int argc, char** argv) {


    //RECIBIR SEÑAL DE LA CAMARA

    cv::VideoCapture camera;
    camera.open("http://192.168.1.90/axis-cgi/mjpg/video.cgi?.mjpg");

    if (camera.isOpened()==true)
    {
        cv::namedWindow("camera");
        int key = 0;
        while (key != 27)
        {
            cv::Mat_<cv::Vec3b> image;
            camera.grab();
            camera.retrieve(image);
            cv::imshow("camera",image);
            key = cv::waitKey(10);

        }
    }
    else{
        printf("No se puede leer la senal");
    }

    //cv::setOpenGlContext("camera");


    //CONTROL DE LA CAMARA



    glutInit(&argc, argv);

    glutInitDisplayMode ...
(more)
edit retag flag offensive close merge delete

Comments

may i ask, what do you need the glut window for ? is it only, because you wired your camera commands to it ?

berak gravatar imageberak ( 2016-09-02 07:43:55 -0600 )edit