2013-07-29 67 views
0

我在OpenCV中工作,并有一个窗口可以添加图像纹理。到目前为止我的代码:将图像纹理加载到CVWindow上

void display(void) { 
    GLuint texture[1]; 
    IplImage *image; 

    image=cvLoadImage("Particle.png"); 

    if(!image){ 
     std::cout << "image empty" << std::endl; 
    } else { 
     glGenTextures(1, texture); 
     glBindTexture(1, GL_TEXTURE_2D); 

     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

     // Set texture clamping method 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    } 

    cvReleaseImage(&image); 
} 

int main(int argc, char**argv) 
{ 
    display(); 
} 

//window to draw the texture on 
cvNamedWindow("video"); 

我是新的OpenGL和发现它混淆所以,关于如何提高这将是巨大或者任何提示,从这里走(如何绘制它的窗口)。

回答

1

幸运的是,您不需要在OpenGL中做任何工作就可以在窗口中绘制图像:OpenCV已经通过函数cvShowImage()提供了此功能。然后

代码来显示窗口可能是:

if(!image) 
{ 
    std::cout << "image empty" << std::endl; 
} 
else 
{ 
    cvNamedWindow("image");  // Create the window 
    cvShowImage("image", image); // Display image in the window 
    cvWaitKey();     // Display window until a key is pressed 
} 
+0

感谢您的答复!由于某种原因窗户没有出现,但至少错误消失了,可能现在我的图像出于某种原因不存在。 –

+0

@AndreaF我怀疑窗口显示并很快消失。添加'cvWaitKey()'将保持窗口打开,直到你按下一个键(见我的编辑)。如果我的回答有帮助,你会接受吗?另外,如果可以,您应该使用C++ API而不是C API。 – Aurelius

+0

仍然有一个空的形象,但会弄清楚,然后应该工作。我会切换,但我没有多少cv :: mat,因为我正在做彩色跟踪,不确定是否可以像使用C那样容易地使用C++。至少颜色跟踪教程似乎是在C中。 –