Thursday, June 11, 2015

Displaying Webcam Video

Posted by Md.Hanif Ali on Thursday, June 11, 2015 in | No comments

Displaying the frames of webcame using videocapture:

In this tutorial, I am going to take video output from the webcam. The opencv handles it quite easily with FFMPEG support. The code shown below will take the videos directly from the webcam and display it in the window.

Code:


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

using namespace cv;
using namespace std;
int main()
{
    namedWindow("Webcam Video");
   VideoCapture cap(0); //default webcam device id=0;

    //check if the webcam was opened properly
    if(!cap.isOpened())
    {
        cout<<"Webcam can't be opened"<<endl;
    }
    while(cap.isOpened())
    {
        Mat frame;
        cap>>frame;
        imshow("Webcam Video",frame);
        //if q is pressed then it will stop
        if(char (waitKey(1))=='q')
        {
            break;
        }
    }
}

Output:

Displaying webcam video using Opencv

 

0 comments:

Post a Comment