Ask Your Question
1

OpenGL interoperability

asked 2013-03-21 08:23:14 -0500

F_Michele gravatar image

Hello all!

I'm under Linux and compiled OpenCV 2.4.4 with OpenGL support, but I don't have any idea of how using the opengl_interop.hpp functions (some of them are even undocumented!, at least on my version of documentation). Looking at window.cpp in the section with OpenGL enabled I found some hints about the use of the functions setOpenGLContext, setOpenGLDrawCallback and updateView but I can't get working even this very simple piece of code:

#include <opencv2/opencv.hpp>
#include <GL/gl.h>
#include <GL/glut.h>
#include <opencv2/core/opengl_interop.hpp>

using namespace cv;

void on_opengl(void* userdata);

int main(void)
{
    VideoCapture webcam(CV_CAP_ANY);
    Mat frame;
    namedWindow("window", CV_WINDOW_OPENGL);
    setOpenGlContext("window");
    while(waitKey(30) < 0)
    {
        webcam >> frame;
        setOpenGlDrawCallback("window", on_opengl);
        imshow("window", frame);
        updateWindow("window");
    }

    return 0;
}

void on_opengl(void* userdata)
{
    glLoadIdentity();

    glTranslated(0.0, 0.0, 1.0);

    glRotatef( 55, 1, 0, 0 );
    glRotatef( 45, 0, 1, 0 );
    glRotatef( 0, 0, 0, 1 );

    static const int coords[6][4][3] = {
        { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
        { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
        { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
        { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
        { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
        { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
    };

    for (int i = 0; i < 6; ++i) {
                glColor3ub( i*20, 100+i*10, i*42 );
                glBegin(GL_QUADS);
                for (int j = 0; j < 4; ++j) {
                        glVertex3d(0.2*coords[i][j][0], 0.2 * coords[i][j][1], 0.2*coords[i][j][2]);
                }
                glEnd();
    }
}

What is the right way of using opengl on the webcam stream? bye!

edit retag flag offensive close merge delete

Comments

what's the outcome of it ? btw, i think, you need to setOpenGlDrawCallback("window", on_opengl); only once

berak gravatar imageberak ( 2013-03-21 08:36:57 -0500 )edit

@berak: the outcome is that just the webcam image is displayed. I put setOpenGLDrawCallback in the loop because every time that imshow is called when the OpenGL support is present it resets the drawCallback to a commodity function in order to show the image as a texture.

F_Michele gravatar imageF_Michele ( 2013-03-21 08:47:02 -0500 )edit

3 answers

Sort by ยป oldest newest most voted
2

answered 2013-03-21 11:03:57 -0500

Vladislav Vinogradov gravatar image

Do not call imshow if you use your own draw callback:

void on_opengl(void* userdata);

int main(void)
{
    VideoCapture webcam(CV_CAP_ANY);

    namedWindow("window", CV_WINDOW_OPENGL);

    Mat frame;
    ogl::Texture2D tex;

    setOpenGlDrawCallback("window", on_opengl, &tex);

    while(waitKey(30) < 0)
    {
        webcam >> frame;
        tex.copyFrom(frame);
        updateWindow("window");
    }

    setOpenGlDrawCallback("window", 0, 0);
    destroyAllWindows();

    return 0;
}

void on_opengl(void* userdata)
{
    ogl::Texture2D* pTex = static_cast<ogl::Texture2D*>(userdata);
    if (pTex->empty())
        return;

    glLoadIdentity();

    glTranslated(0.0, 0.0, 1.0);

    glRotatef( 55, 1, 0, 0 );
    glRotatef( 45, 0, 1, 0 );
    glRotatef( 0, 0, 0, 1 );

    static const int coords[6][4][3] = {
        { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
        { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
        { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
        { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
        { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
        { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
    };

    glEnable(GL_TEXTURE_2D);
    pTex->bind();

    for (int i = 0; i < 6; ++i) {
                glColor3ub( i*20, 100+i*10, i*42 );
                glBegin(GL_QUADS);
                for (int j = 0; j < 4; ++j) {
                        glVertex3d(0.2*coords[i][j][0], 0.2 * coords[i][j][1], 0.2*coords[i][j][2]);
                }
                glEnd();
    }
}
edit flag offensive delete link more
0

answered 2014-08-06 09:11:50 -0500

protonmesh gravatar image

In the draw callback, consider the use of: C++: void ogl::render(const Texture2D& tex, Rect_<double> wndRect=Rect_<double>(0.0, 0.0, 1.0, 1.0), Rect_<double> texRect=Rect_<double>(0.0, 0.0, 1.0, 1.0))

which encapsulates the GL commands necessary to display a texture mapped to a rectangle in the screen plane.

edit flag offensive delete link more
-1

answered 2013-03-21 16:11:17 -0500

PhantomFav gravatar image

updated 2013-03-21 16:31:46 -0500

You must simply use opengGL with openCV and not viceversa. You can build your shape in openGL and apply the texture using the tools that opencv come with. For example this is a function to transform a IplImage into a texture for openGL:

GLuint ConvertIplToTexture(IplImage *image){
    GLuint texture;
    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D,texture);
    gluBuild2DMipmaps(GL_TEXTURE_2D,3,image->width,image->height,
                             GL_BGR,GL_UNSIGNED_BYTE,image->imageData);
    return texture;
}
edit flag offensive delete link more

Comments

I don't think that's true, right? I mean why is there a CV_WINDOW_OPENGL and opengl_interop.hpp that specifically allows you to use opencv with opengl (and not viceversa)?

Elador gravatar imageElador ( 2013-07-29 17:50:34 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-03-21 08:23:14 -0500

Seen: 9,423 times

Last updated: Aug 06 '14