1

opencv マット データを opengl テクスチャとして使用したいと考えています。QGLWidget を拡張する Qt4.8 アプリケーションを開発しています (ただし、qimage を通過する必要はありません)。しかし、何かが間違っている..

最初にスクリーンショットの問題、次に使用しているコード。

cv::Mat (ビデオから取得) のサイズを変更しなければ、すべて問題ありません。寸法の半分 (scaleFactor=2) としてスケーリングすると、すべて問題ありません。倍率が 2.8 または 2.9 の場合、すべて問題ありません。しかし..いくつかのscaleFactorで..バグがあります。

ここでは、opengl のクワッド ディメンションを理解するための素敵な赤い背景のスクリーンショットを示します。

スケール係数 = 2 ここに画像の説明を入力

スケール係数 = 2.8 ここに画像の説明を入力

スケール係数 = 3 ここに画像の説明を入力

スケール係数 = 3.2 ここに画像の説明を入力

次に、ペイント メソッドのコードです。cv::Mat データを gl テクスチャにコピーするためのコードを、この素敵なブログ投稿から見つけました。

void VideoViewer::paintGL()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glClearColor (1.0, 0.0, 0.0, 1.0);

    glEnable(GL_BLEND);

    // Use a simple blendfunc for drawing the background
    glBlendFunc(GL_ONE, GL_ZERO);

    if (!cvFrame.empty()) {
        glEnable(GL_TEXTURE_2D);

        GLuint tex = matToTexture(cvFrame);
        glBindTexture(GL_TEXTURE_2D, tex);

        glBegin(GL_QUADS);
        glTexCoord2f(1, 1); glVertex2f(0, cvFrame.size().height);
        glTexCoord2f(1, 0); glVertex2f(0, 0);
        glTexCoord2f(0, 0); glVertex2f(cvFrame.size().width, 0);
        glTexCoord2f(0, 1); glVertex2f(cvFrame.size().width, cvFrame.size().height);
        glEnd();

        glDeleteTextures(1, &tex);
        glDisable(GL_TEXTURE_2D);

        glFlush();
    }
}

GLuint VideoViewer::matToTexture(cv::Mat &mat, GLenum minFilter, GLenum magFilter, GLenum wrapFilter)
{
    // http://r3dux.org/2012/01/how-to-convert-an-opencv-cvmat-to-an-opengl-texture/

    // Generate a number for our textureID's unique handle
    GLuint textureID;
    glGenTextures(1, &textureID);

    // Bind to our texture handle
    glBindTexture(GL_TEXTURE_2D, textureID);

    // Catch silly-mistake texture interpolation method for magnification
    if (magFilter == GL_LINEAR_MIPMAP_LINEAR  ||
        magFilter == GL_LINEAR_MIPMAP_NEAREST ||
        magFilter == GL_NEAREST_MIPMAP_LINEAR ||
        magFilter == GL_NEAREST_MIPMAP_NEAREST)
    {
        std::cout << "VideoViewer::matToTexture > "
                  << "You can't use MIPMAPs for magnification - setting filter to GL_LINEAR"
                  << std::endl;
        magFilter = GL_LINEAR;
    }

    // Set texture interpolation methods for minification and magnification
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);

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

    // Set incoming texture format to:
    // GL_BGR       for CV_CAP_OPENNI_BGR_IMAGE,
    // GL_LUMINANCE for CV_CAP_OPENNI_DISPARITY_MAP,
    // Work out other mappings as required ( there's a list in comments in main() )
    GLenum inputColourFormat = GL_BGR;
    if (mat.channels() == 1)
    {
        inputColourFormat = GL_LUMINANCE;
    }

    // Create the texture
    glTexImage2D(GL_TEXTURE_2D,     // Type of texture
                 0,                 // Pyramid level (for mip-mapping) - 0 is the top level
                 GL_RGB,            // Internal colour format to convert to
                 mat.cols,          // Image width  i.e. 640 for Kinect in standard mode
                 mat.rows,          // Image height i.e. 480 for Kinect in standard mode
                 0,                 // Border width in pixels (can either be 1 or 0)
                 inputColourFormat, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
                 GL_UNSIGNED_BYTE,  // Image data type
                 mat.ptr());        // The actual image data itself

    return textureID;
}

cv::Mat がどのようにロードされ、スケーリングされるか:

void VideoViewer::retriveScaledFrame()
{
    video >> cvFrame;

    cv::Size s = cv::Size(cvFrame.size().width/scaleFactor, cvFrame.size().height/scaleFactor);
    cv::resize(cvFrame, cvFrame, s);
}

画像が正しくレンダリングされる場合とそうでない場合があります..なぜですか? 確かに、opencv と opengl の間のピクセル格納順序の不一致に何か問題があります..しかし、それを解決するにはどうすればよいですか? なぜ大丈夫なときとダメなときがあるの?

4

1 に答える 1

1

はい、メモリに格納するピクセルの問題でした。OpenCV と OpenGL はさまざまな方法でピクセルを保存できます。

glPixelStoreiOpenGL では、これらのパラメーターをおよびGL_UNPACK_ALIGNMENT, で指定できますGL_UNPACK_ROW_LENGTH

これについての良い答えはここにあります。

于 2013-11-14T10:14:54.357 に答える