QT 5.2 / OpenCV 2.4.8 - Can't open video files via VideoCapture



  • Hello Guys!

    I have a big problem that i can't solve by myself. OpenCV itself works fine, but i'm not able to load videos. Here's my code:

    PRO- File
    @QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = videoredux
    TEMPLATE = app

    INCLUDEPATH += C:/OpenCV/opencv_bin/install/include
    LIBS += -LC:\OpenCV\opencv_bin\bin
    libopencv_core248d
    libopencv_highgui248d
    libopencv_imgproc248d
    libopencv_features2d248d
    libopencv_calib3d248d
    libopencv_video248d \

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui@

    and the MainWindow Class:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFileDialog>
    #include <iostream>
    #include <qdebug.h>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv/cv.h>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->videoStatusLabel->setText("Kein Video geladen.");

    // SIGNALS & SLOTS
    
    QObject::connect(ui->chooseVideoButton,SIGNAL(clicked()),
                     this,SLOT(chooseVideo()));
    QObject::connect(ui->startButton,SIGNAL(clicked()),
                     this,SLOT(startProcess()));
    

    }

    void MainWindow::chooseVideo(){

    QString fileName = QFileDialog::getOpenFileName(this,
         tr("Open Video"), "/home", tr("Video Files (*.avi *.mp4 *.mpeg *.mpg)"));
    qDebug() << "Path:" << fileName;
    ui->videoStatusLabel->setText(fileName);
    

    }

    void MainWindow::startProcess(){
    QString videoPath = ui->videoStatusLabel->text();
    QFileInfo video(videoPath);
    if(video.exists()){
    const std::string path = videoPath.toUtf8().constData();
    cv::VideoCapture capture(path);
    cv::Mat frame;

        if(!capture.isOpened()){
            qDebug() << "Error, video not loaded";
        }
    
        cv::namedWindow("window",1);
        while(true)
        {
            bool success = capture.read(frame);
            if(success == false){
                break;
            }
            cv::imshow("window",frame);
            cv::waitKey(20);
        }
        cv::waitKey(0);
    }
    else{
        qDebug() << "Error, File doesn't exist";
    }
    

    }@

    The paths are correct, I tried many different video formats but he never loads the videos. I'm running Qt on a Windows 8 machine and i have "K-Lite Codec Pack 10.2.0 Basic" and ffmpeg installed. The videos are playing properly with my video players. Does anyone know how to solve this problem?

    Thank you very much!

    Nadine


  • Lifetime Qt Champion

    Hi,

    I would say that the path you give to OpenCV is invalid. Qt uses Unix style path internally on all platforms but not OpenCV.

    Use QDir::toNativeSeparators and it should work better.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.