5

In my work I overlap a part of a captured frame with an image. I open my webcam with openCV and then I transform the captured frame in a texture and display it in a GLUT window. Also, I overlap a part of this texture with this image:

enter image description here

I do this in real time, and the result is:

enter image description here

As you can see, edges of projected image are inaccurate. I think it is an aliasing problem, but I don't know how to do the antialiasing process with opengl. I've tried to look for on web, but I didn't find a good solution for my problem.

In my "calculate" function I transform the mat image into a texture usign the following code:

GLvoid calculate(){
...
...
cvtColor(image, image, CV_BGR2RGB);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glBindTexture(GL_TEXTURE_2D, textures[1]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    //glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.cols, image.rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}

and I show the result using this code:

GLvoid Show(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    // Matrice di proiezione
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, WIDTH, HEIGHT, 0); 

    // Matrice model view
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 
...
...
glBindTexture(GL_TEXTURE_2D, textures[1]);
        glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((coord[3].x)),(GLfloat)(coord[3].y));
        glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((coord[0].x)),(GLfloat)(coord[0].y));
        glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((coord[1].x)),(GLfloat)(coord[1].y));
        glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((coord[2].x)),(GLfloat)(coord[2].y)); 

        glEnd();    
    }
    glFlush();
    glutSwapBuffers();

}

In initialization function I write this:

GLvoid Init() {  

    glGenTextures(2, textures);
    glClearColor (0.0, 0.0, 0.0, 0.0);

    glEnable (GL_POLYGON_SMOOTH);
    glHint (GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
    glDisable(GL_DEPTH_TEST);

}

but it doesn't work...

I work on Win7 x64, with OPenGL 4.0 and Glut 3.7. My video card is an NVidia GeForce gt 630. also I enabled antialiasing from Nvidia control panel, but nothing is changed. does anyone know how to help me?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Cristina1986
  • 505
  • 1
  • 8
  • 21
  • Could you explain what you mean by "the edges of projected image are inaccurate"? Is the problem that they are "jaggy"? Or is it that the colored rectangle doesn't cover the entire mat? – bwroga Apr 02 '13 at 12:49
  • They are "jaggy". The projected image covers an area that I decided in advance, so coverage is not the problem. My problem is that edges are "jaggy" as you say! – Cristina1986 Apr 02 '13 at 12:55
  • Have you tried anything like what is described in [this page](http://bankslab.berkeley.edu/members/chris/AntiAliasing/AntiAliasingInOpenGL.html)? – bwroga Apr 02 '13 at 13:20
  • As a sidenote, your camera will give a certain "bulge" depending on the distance to your actual object. The projected square won't get that bulge, due to a perspective-projection peculiarity. If you tessellate the square into more smaller squares that'll fix it. May not be relevant, but probably is. – dascandy Apr 02 '13 at 15:12

2 Answers2

3

I solved my problem! I used GLFW insted of GLUT, as @Michael IV suggested me!

in order to do antialiasing with GLFW i used this line of code:

glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);

and the result now is very good, as you can see in the following image.

enter image description here

Thanks for your help!

Cristina1986
  • 505
  • 1
  • 8
  • 21
2

First I wonder why you are using OpenGL 4.0 to work with fixed (deprecated ) pipeline... But let's get to the problem.What you need is MSAA .I am not sure ,enabling it via control panel will always do the trick.Usually it is done inside the code. Unfortunately for you , you selected to use GLUT which has no option to set hardware MSAA level.If you want to be able to do so switch to GLFW.Another option is do do it manually but that implies you use custom FBOs.In such a scenario you can create FBO with MSAA texture attachment setting MSAA level for the texture (also you can apply custom multisampling algorithms in fragment shader if you wish). Here is a thread on this topic.

GLFW allows you specifying MSAA level on window setup.See the related API.

MSAA does degrade the performance ,but how much depends on your hardware and probably OpenGL drivers.

Community
  • 1
  • 1
Michael IV
  • 11,016
  • 12
  • 92
  • 223
  • Thank you for your answer! I'll try to use GLFW instead of GLUT, if this solve my problem! If you know, I solve my problem only writing `glfwOpenWindowHint(GLFW_FSAA_SAMPLES, int value grater than 0 )`?? Or I must write other code??? and why are you surprised that I use opengl 4.0 in order to project an image on a portion of a captured frame? I have another question: How does MSAA influence and degrade performances of a real-time appplication?? – Cristina1986 Apr 02 '13 at 14:53
  • See my answer.For OpenGL 4.0 - it is meant to be used with modern (programmable pipeline) You are using old and deprecated functionality which still works but is not OpenGL programming standard anymore. – Michael IV Apr 02 '13 at 14:57