簡體   English   中英

帶有 OpenGL 和 Kinect v2 的 QT5:錯誤的圖像呈現

[英]QT5 with OpenGL and Kinect v2: wrong image presentation

我想在 Qt 小部件中通過 openGl 顯示來自 kinect v2 傳感器的彩色圖像。 問題是,此圖像的呈現方式不正確。

這是我獲取顏色框架的代碼:

班級:

cv::Mat                     ColorMap;
std::vector<BYTE>           colorBuffer;
int                         color_width;
int                         color_height;

代碼:

if(colorBuffer.empty())
   colorBuffer.resize(color_height * color_width * 4 * sizeof(unsigned char));

hr = ColorFrame->CopyConvertedFrameDataToArray((UINT)colorBuffer.size(),
                             &colorBuffer[0], ColorImageFormat::ColorImageFormat_Bgra );

ColorMap = cv::Mat( color_height, color_width, CV_8UC4, &colorBuffer[0]);

這意味着,我以帶有 alpha 通道的 BGR 格式獲取顏色信息,並將其復制到具有 4 個通道的矩陣 (BRGA) 中,並且每個通道具有從 0 到 255 的 8Bit。正確嗎?

在下一步中,我將其調整為與小部件相同的大小:

    Kinect->CreateFrame();
    cv::Mat GUIColorImage;
    Kinect->ColorMap.copyTo(GUIColorImage);
    cv::resize(GUIColorImage,GUIColorImage,
    cv::Size(ui->Left_Widget_Color->width(),ui->Left_Widget_Color->height()));

比我嘗試過兩種轉換方法:1. 轉換為 BGR 2. 轉換為 8UC3(8 位,無符號字符,3 個通道(與 BGR 相同?))

1: GUIColorImage.convertTo(GUIColorImage,CV_BGRA2BGR);

2: GUIColorImage.convertTo(GUIColorImage,CV_8UC3);

但沒有任何解決方案有效。

轉換后,我嘗試通過 openGl 顯示它:

LeftWidgetColor.UpdateImage(GUIColorImage);

LeftWidgetColor 是一個 QOpenGLWidget:

標題:

class RenderWidget : public QOpenGLWidget
{
    Q_OBJECT
public:
    RenderWidget(QWidget *parent);
    ~RenderWidget();
    void UpdateImage(cv::Mat newimage);
    void initializeGL();
    void paintGL();

private:
    int                 width;
    int                 height;
    GLuint              texture;
    cv::Mat             image;

signals:
    void info(QString msg);
    void error(QString msg);
    void DepthValueAt(cv::Point2i DepthPosition);

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
};

代碼:

RenderWidget::RenderWidget(QWidget *parent) : QOpenGLWidget(parent)
{
    width = this->size().width();
    height = this->size().height();
    texture = 0;
    initializeGL();
}

RenderWidget::~RenderWidget()
{
    glDeleteTextures(1, &texture);
}


void RenderWidget::initializeGL()
{
    //Background Color is black
    glClearColor(0,0,0,1);

    //Storage of Pixelmode for two-dimensional textures
    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

    //Create texture
    glGenTextures(1, &texture);

    glViewport(0, 0, width, height);


}

void RenderWidget::paintGL()
{
    // Clear the screen and depth buffer (with black)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Select the model view matrix and reset it
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0.0f, 0.0f, 0.0f);

    // Abort drawing if OpenCV was unable to open the camera
    if (image.empty())
    {
        return;
    }
    glEnable(GL_TEXTURE_RECTANGLE_ARB);

    // Typical texture generation using data from the bitmap
    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);

    // Transfer image data to the GPU
    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
                 3, image.cols, image.rows, 0,
                 GL_BGR, GL_UNSIGNED_BYTE, image.data);


    if (glGetError() != GL_NO_ERROR)
    {
    }

    // Draw a 2D face with texture
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0);                   glVertex2f(1, 1);
        glTexCoord2f(image.cols, 0);          glVertex2f(-1, 1);
        glTexCoord2f(image.cols, image.rows); glVertex2f(-1, -1);
        glTexCoord2f(0, image.rows);          glVertex2f(1, -1);
    glEnd();

glDisable(GL_TEXTURE_RECTANGLE_ARB);
}

void RenderWidget::UpdateImage(cv::Mat newimage)
{
    newimage.copyTo(image);
    update();
}

我想問題出在:

glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
             3, image.cols, image.rows, 0,
             GL_BGR, GL_UNSIGNED_BYTE, image.data);

但我找不到。 我已將 3-channels, BGR, 8-Bit = 1 Byte 聲明為無符號。 有人知道錯誤在哪里嗎?

開放式Gl

如果我通過 imshow(openCv 類)顯示它,它工作正常。

在 opencv 上顯示

我的錯誤是,我使用了cv::Mat::convertTo():在這里他們寫道,convertTo() 不應該用於轉換格式(僅用於更改數據類型)。 cv::cvtColor 是正確的函數,對我來說效果很好。 所以我改變了轉換:

GUIColorImage.convertTo(GUIColorImage,CV_BGRA2BGR);

cv::cvtColor(GUIColorImage,GUIColorImage,CV_BGRA2BGR);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM