Ask Your Question
0

Place texture using a given UV Buffer

asked 2015-02-04 05:17:27 -0600

patofis gravatar image

updated 2015-02-05 02:29:23 -0600

Hi to every one, this is my first try on OpenCV and seems to be awesome, but now I'm facing to the first problem I cannot solve looking at the help manual or the forums.

I wanna change a texture by reprojecting it like in this image:

image description

That's trivial on OpenGL shaders since they compute by their own the derivatives etc, so you can just read the pixel value of the uv buffer and read the pixel placed at that x,y coordinates, since OpenGL takes care of selecting the mipmap, make a good mixing for shallow angles etc.

Meanwhile I only manage to read the pixel on OpenCV or at most I can blend it with the contiguous one.

EDIT maybe my question was too ambiguous, I was asking about how to make this on OpenCV without OpenGL calls.

Using OpenGL I should draw a simple rectangle and use as fragmented shader this pseudo-code:

float2 newUV=texture(UVgbufferTex,gl_TexCoord[0]).xy; float3 finalcolor=texture(DIFFgbufferTex,newUV);

return float4(finalcolor,0);

OpenGL takes care about selecting the mipmap level, the anisotropic filtering etc, meanwhile if I make this on OpenCV I get a single pixel for finalcolor so my result is crisp.

Regards, and thank you in advance.

edit retag flag offensive close merge delete

Comments

imho, you did not think this through properly. opengl maps textures to 3d objects, then renders a 2d projection to screen. opencv lives in a 2d world, there are no vertices, no normals, no texcoords, just pixels.

berak gravatar imageberak ( 2015-02-05 02:32:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-02-04 12:28:20 -0600

Hi @patofis!

You can easily render a Texture in a opengl window using OpenCV! Here is a sample code to attach a Texture to a Cube. I will leave the rest of the shapes for you. since you can get enough Opengl code samples for that!

image description

#include <windows.h>
#include <stdio.h>
#include <glut.h>

#include "opencv2/core/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <strstream>
#include <functional>

using namespace std;
using namespace cv;

GLuint TextureIdx[1]; //the array for our TextureIdx
Mat mSource_Bgr;
GLfloat angle = 45.0;
//The IplImage to OpenGl TextureIdx function
int UploadTexture(Mat image, GLuint &TextureIdx) 
{
    if (image.empty()) 
        return -1;
    glGenTextures(1, &TextureIdx);
    glBindTexture( GL_TEXTURE_2D, TextureIdx); //bind the TextureIdx to it's array
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows,0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.data);
    return 0;
}
void plane (void) 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glLoadIdentity();                                   // Reset The View
    glTranslatef(0.0f,.0f,-5.0f);

    glRotatef(angle,1.0f,1.0f,0.0f);
    glBindTexture( GL_TEXTURE_2D, TextureIdx[0]); //unbind the TextureIdx

    glBegin(GL_QUADS);
        // Front Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
        // Back Face
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
        // Top Face
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        // Bottom Face
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
        // Right face
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
        // Left Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    glEnd();


    glBindTexture( GL_TEXTURE_2D, 0); //unbind the ...
(more)
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-02-04 05:17:27 -0600

Seen: 2,105 times

Last updated: Feb 05 '15