Ask Your Question
0

How To Obtain Live Webcam Feed

asked 2016-05-23 22:22:17 -0600

trav gravatar image

Im new to CV and have been reading on the documentation and find some stuff clear and other things not, this is one thing thats not really clear and am failing to take off and understand this; I am using Qt I have openCV properly installed I have created a Qt widget application in my project.pro file i have the sources and libs for openCV my question is where do I actually put the openCV code and why isn't this working? I putting all the openCV code in my main method in my main.cpp at the moment. All I would like to do at the moment is open the webcam and get live feed from my local cam, How can I accomplish this and where do I need to put the openCV code if I'm not already putting this in the correct file, thanks in advance.

What I have tried main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <QTextStream>
#include <stdio.h>

using namespace cv;
using namespace std;





int main(int argc, char *argv[]){



    // disable buffer useless with GUI app
   //setbuf(stdout, NULL);
       QApplication a(argc, argv);
       MainWindow w;
       w.show();
      return a.exec();



   VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.

   if (!stream1.isOpened()) { //check if video device has been initialised
   cout << "cannot open camera";
   }

   //unconditional loop
   while (true) {
   Mat cameraFrame;
   stream1.read(cameraFrame);
   imshow("cam", cameraFrame);
   if (waitKey(30) >= 0)
   break;
   }
   return 0;
   }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-05-24 01:38:01 -0600

lama123 gravatar image

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

edit flag offensive delete link more

Comments

Hey Amal,

Thanks for the reply unfortunately this code or example is not working for me. I am getting 2 issues. symbol(s) not found for architecture x86_64 linker command failed with exit code1 (use -v to see invocation)

trav gravatar imagetrav ( 2016-05-24 10:45:24 -0600 )edit

Can you post your project file

lama123 gravatar imagelama123 ( 2016-05-24 10:51:42 -0600 )edit

Sure, worth noting I'm using Mac OSX; I had a problem before with the project file and this was the original that actually worked in the end, unless this is wrong again.. thanks again

  #-------------------------------------------------
#
# Project created by QtCreator 2016-05-21T11:35:02
#
#-------------------------------------------------

QT  += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = trabajo
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

LIBS += \
    -L/usr/local/lib \
    -lopencv_core \
    -lopencv_highgui

INCLUDEPATH += \
    /usr/local/include



HEADERS  += mainwindow.h

FORMS    += mainwindow.ui
trav gravatar imagetrav ( 2016-05-24 13:31:17 -0600 )edit
1

if you are using opencv 3 or above you need to link with opencv_videoio too for videocapture functionality. ie LIBS += \ -L/usr/local/lib \ -lopencv_core \ -lopencv_highgui \ -lopencv_videoio

lama123 gravatar imagelama123 ( 2016-05-24 22:14:46 -0600 )edit

Hey it actually works :) thanks so much I can finally play around now and get the hang of it, just needed the first initial intro and housekeeping stuff, thanks again

trav gravatar imagetrav ( 2016-05-24 22:24:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-23 22:22:17 -0600

Seen: 3,033 times

Last updated: May 24 '16