How to display Video in opencv-Qt ? [closed]

asked 2015-12-05 13:43:56 -0500

Noman gravatar image

After following some answers in the stackoverflow, I understood how to use timer to display videos in qt. But Still my program doesn't even get to the function that will be called repeatedly. I am not understanding what I am doing wrong. Here's my code -

VideoCapture cap(0);
QTimer *imageTimer;
Mat frame;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    const int imagePeriod = 1000 / 25;   // ms        
    imageTimer = new QTimer(this);
    imageTimer->setInterval(imagePeriod);
    connect(imageTimer, SIGNAL(timeout()), this, SLOT(showVideo()));
}

MainWindow::~MainWindow()
{

}

QImage MainWindow::getQImage(cv::Mat &timage){

    static QVector<QRgb> colorTable;

    if (colorTable.isEmpty()){
        for (int i = 0; i < 256; i++){
            colorTable.push_back(qRgb(i, i, i));
        }
    }

    if (timage.type() == CV_8UC3){
        QImage temp = QImage((const unsigned char*)(timage.data), timage.cols, timage.rows, timage.step, QImage::Format_RGB888);
        return temp.rgbSwapped();
    }
    else if (timage.type() == CV_8UC1){
        QImage temp = QImage((const unsigned char*)(timage.data), timage.cols, timage.rows, timage.step, QImage::Format_Indexed8);
        temp.setColorTable(colorTable);
        return temp;
    }
}

void MainWindow::on_startButton_pressed(){
    imageTimer->start(10);

}

void MainWindow::showVideo(){

    cap.read(frame);

    ui.videoFeed->setScaledContents(true);
    ui.videoFeed->setPixmap(QPixmap::fromImage(getQImage(frame)));
    ui.videoFeed->resize(ui.videoFeed->pixmap()->size());

    this->update();
}

void MainWindow::on_stopButton_pressed(){
    imageTimer->stop();
}

This is the Header FIle

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QImage MainWindow::getQImage(cv::Mat &timage);


private:
    Ui::MainWindowClass ui;

private slots:

    void on_startButton_pressed();
    void on_stopButton_pressed();

    public slots:

    void MainWindow::showVideo();
};
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-13 14:09:39.352859

Comments

1

This is quite OpenCV unrelated... Anyway, my two cents: I usually use threads to deal with videos in Qt, as I think it's a much better option than timers. Here you have a tutorial. Regarding your code, there are several things that seem incoherent, like setting an initial interval of 40ms but then starting the timer with and overloaded interval of 10 ms. As I said, I'd give threads a try...

LorenaGdL gravatar imageLorenaGdL ( 2015-12-05 14:25:07 -0500 )edit