1

cv::Mat を QGLWidget に描画する方法を知りたいです。ここで方法を見つけて、それを実装しようとしました。しかし、私のウィジェットは黒です。

これは私のウィジェットのコードです:

hファイル

#ifndef GLVIEWER_H
#define GLVIEWER_H
#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QtOpenGL>
#include <opencv2/core/core.hpp>

class GLViewer : public QGLWidget {
    Q_OBJECT
public:
    GLViewer(QWidget *parent = NULL);
    void setNewFrame(cv::Mat newframe);
protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    cv::Mat frame;
};
#endif // GLVIEWER_H

cpp ファイル

#include "glviewer.h"
#include <iostream>
#include <QtGui/QMouseEvent>

GLViewer::GLViewer(QWidget *parent) : QGLWidget(parent) {
    setMouseTracking(true);
    frame = cv::Mat::ones(25, 50, CV_8UC3) * 255;
}

void GLViewer::setNewFrame(cv::Mat newframe) {
    frame = newframe;
    paintGL();
}

void GLViewer::initializeGL() {
    std::cout << "initializeGL" << std::endl;
    glViewport(0,0,this->width(), this->height());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-this->width()/2, this->width()/2, this->height()/2, -this->height()/2, -1, 1);
}

void GLViewer::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
//    glMatrixMode(GL_PROJECTION);
//    glLoadIdentity();
//    gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
//    glMatrixMode(GL_MODELVIEW);
//    glLoadIdentity();
}

void GLViewer::paintGL() {

    //std::cout << "paint" << std::endl;

    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0f, 0, 0, 1.0f);

    glDrawPixels(frame.rows, frame.cols,
                 GL_RGB,
                 GL_UNSIGNED_BYTE,
                 frame.data);


    }

'cv::Mat::ones(25, 50, CV_8UC3) * 255;' で cv::Mat を初期化したので、少し青い四角形が表示されると思いますが.. 何も起こらず、真っ黒なウィジェットです。

誰でも私に解決策を指摘できますか?

編集

背景を赤に変更しようとしましたが、今は赤です..これは出力です: ここに画像の説明を入力

背景は正しいですが、青い四角形は..四角形に見えても青くありません..何が起こっていますか? frame.cols と frame.cols、GL_RGB の代わりに GL_BGR とデータ型 (GL_UNSIGNED_SHORT など) を使用したいくつかのランダム試行を反転しようとしましたが、より良い結果が得られません..

4

2 に答える 2