QImage破坏了OpenCV

时间:2016-05-14 14:41:42

标签: c++ qt opencv

我有Qt和OpenCV的网络摄像头演示。基本上它会显示网络摄像头的输入,当点击一个按钮时,它会启动一个长(3/5秒)线程的线程。

问题是,当我点击按钮时,QImage会立即 损坏as you can see here,而我再也看不到视频Feed了。信号&插槽工作(我看到控制台中的输出),但我无法在这里发现问题。

有人可以帮忙吗?

window::window() : QMainWindow(NULL, 0)
{
    std::cout << "constructor start" << std::endl;

    setWindowTitle("Video");

    button = new QPushButton("Long Job");
    connect(button, SIGNAL(clicked()), this, SLOT(longjob()));

    image = new QLabel();
    image->setAlignment(Qt::AlignCenter);

    layout = new QVBoxLayout();
    layout->addWidget(image);
    layout->addWidget(button);

    mainwidget = new QWidget();
    mainwidget->setLayout(layout);

    resize(800, 600);

    setCentralWidget(mainwidget);

    cap = cv::VideoCapture(0);

    timer = new QTimer();
    timer->setInterval(100);
    timer->start();
    connect(timer, SIGNAL(timeout()), this, SLOT(newframe()));

    std::cout << "constructor end" << std::endl;
}

void window::newframe()
{
    std::cout << "FRAME " << count++ << std::endl;

    cv::Mat frame;
    cap >> frame;

    image->setPixmap(QPixmap::fromImage(showImage(frame))); // converts perfectly
}

void window::longjob()
{

    std::cout << "START THREAD" << std::endl;

    w = new worker();

    connect(w, SIGNAL(resultReady(double)), this, SLOT(detected(double)));

    w->start();

    return;
}

void window::detected(double d)
{
    disconnect(w, SIGNAL(resultReady(double)), this, SLOT(detected(double)));

    std::cout << "DETECTED " << d << std::endl;

    delete w;

    frames.clear();
}

class worker : public QThread
{
    Q_OBJECT

public:

    worker();

    ~worker();

    void run() Q_DECL_OVERRIDE;

signals:
    void resultReady(double d);
};

worker::worker() : QThread()
{

}

worker::~worker()
{
    std::cout << "THREAD EXIT" << std::endl;
}

void worker::run()  
{
    std::cout << "THREAD RUN" << std::endl;

    double d = longOpenCVJob();

    emit resultReady(d);
}

2 个答案:

答案 0 :(得分:1)

答案来自Evgeniy:QImage不拥有数据,它不会将STL容器模仿为this answer shows

答案 1 :(得分:0)

我会将此代码用于showImage():

aws s3 ls s3://your-bucket --recursive | grep your-search | cut -c 32-