使用OpenCV和OpenGL绘制错误 [英] Wrong drawing with OpenCV and OpenGL

查看:109
本文介绍了使用OpenCV和OpenGL绘制错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenCV和OpenGL绘制圆,我必须这样做,因为我需要在纹理上进行绘制.

I'm trying to draw a circle using OpenCV and OpenGL, I have to do it like this because I need drawing to be on a texture.

当前图形如下:

生成此图像并绘制它的代码如下:

The code to generate this image and draw it is as follows:

class Texture
{
    void setup()
    {
        glEnable(GL_TEXTURE_2D);
        glGenTextures(1, &uid);
        glBindTexture(GL_TEXTURE_2D, uid);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_REPEAT );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
        glBindTexture(GL_TEXTURE_2D, 0);
        glDisable(GL_TEXTURE_2D);
    }
    ...
    void setData(unsigned char * data, uint width, uint height, GLint internalformat, GLenum format)
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, uid);
        glTexImage2D(   GL_TEXTURE_2D, 
                        0,
                        internalformat,
                        width,
                        height,
                        0,
                        format,
                        GL_UNSIGNED_BYTE,
                        data);
        glBindTexture(GL_TEXTURE_2D, 0);
        glDisable(GL_TEXTURE_2D);
    }
    ...
    void draw(float x, float y, float scale )
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, uid);
        glBegin(GL_QUADS);
            glTexCoord2i(0, 0);     glVertex2i(x, y);
            glTexCoord2i(0, 1);     glVertex2i(x, y + height * scale);
            glTexCoord2i(1, 1);     glVertex2i(x + width * scale, y + height * scale);
            glTexCoord2i(1, 0);     glVertex2i(x + width * scale, y);
        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);
        glDisable(GL_TEXTURE_2D);
    }
    ... 
};

class App
{
    Texture tex;
    ...
    void update()
    {
        const int w_mask_cv = 640;
        const int h_mask_cv = 480;

        cv::Mat mask_cv = cv::Mat::zeros(w_mask_cv, h_mask_cv, CV_8UC1);
        cv::circle(mask_cv, cv::Point(w_mask_cv/2, h_mask_cv/2), 15, cv::Scalar(255));

        tex.setData(mask_cv.data, w_mask_cv, h_mask_cv, GL_LUMINANCE, GL_LUMINANCE);
    }
    void draw()
    {
        tex.draw(0, 0, 1);
    }
    ...
};

您可以看到圆应该在中间而不是重复,但是我不知道为什么在水平轴上重复它.

As you can see the circle is supposed to be on the middle and not repeated, but I do not know why it is being repeated on the horizontal axis.

有什么建议吗?

推荐答案

在我所拥有的代码中,错误在于发送给方法的参数.

The mistake was on the parameters send to a method, in the code I have.

cv::Mat mask_cv = cv::Mat::zeros(w_mask_cv, h_mask_cv, CV_8UC1);

但是实际上cv :: Mat :: zeros(行,列,类型),所以代码必须像这样.

But actually cv::Mat::zeros(rows, cols, type), so the code has to be like.

cv::Mat mask_cv = cv::Mat::zeros(h_mask_cv, w_mask_cv, CV_8UC1);

什么愚蠢的错误:P

这篇关于使用OpenCV和OpenGL绘制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆