Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is the code for the webcam. It is using opencv 3 and qt 5.6 . As far as your code is concerned one of the errors is given below

w.show();
return a.exec();

this return should be at the end , otherwise the opencv part is never called

#include "mainwindow.h"
#include <QApplication>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <iostream>


using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    // setup video capture
    VideoCapture capture(0);
    if (!capture.isOpened())
    {
      std::cout << "Could not open VideoCapture" << std::endl;
      return 3;

        }

        Mat imgRead;

        for (;;)
        {
          // capture a frame
          capture >> imgRead;
          imshow("Video", imgRead);
          // close if key pressed
          if(waitKey(27) > 0)
              break;
        }

        capture.release();
        destroyAllWindows();

        return a.exec();
    }

the project file is given below

#-------------------------------------------------
#
# Project created by QtCreator 2016-05-24T11:38:47
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = webcamgui
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += "D:\work\opencv-3.1.0\opencv\build\include"

Release
{
LIBS += -LD:\work\opencv-3.1.0\Qt5.6MinGWQtContribRel\lib \
-llibopencv_core310 \
-llibopencv_highgui310 \
-llibopencv_videoio310 \
}
Debug
{
LIBS += -LD:\work\opencv-3.1.0\Qt5.6MinGWQtContribDeb\lib \
-llibopencv_core310d \
-llibopencv_highgui310d \
-llibopencv_videoio310d \

}

The better way is in fact to embed the video window inside the qt mainframe window using a widget like label. One can convert the mat frames to qt frames and show it in qt widgets.

Thanks

Amal