Life2Coding
Displaying Webcam Video

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:

Videocapture%2Busing%2BWebcam Displaying Webcam Video
Displaying webcam video using Opencv

 

life2coding_icon [] Displaying Webcam Video

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.