Streaming Video from Camera using OpenCV
In this beginner level tutorial, we will be introducing how to stream a video from a camera connected to your computer. When we say that we stream a video, it is nothing but displaying images captured from the camera nonstop.. But the only difference is that the image is to be "captured" from a camera. So there must be some method to mention in our code the name of the camera, or i other words a method to point to the camera. Therefore we now feels the necessity of finding the address of the camera. Once we have the address, what we have to do is nothing but to "Query for frame/image" from the specified camera address. Now if we continuously refresh the displayed image, it will seems like we are streaming the video. So lets get started. Unlike the Tutorial 1 we wont be explaining all the functions. Only newly introduced functions will be dealt with in this and the comming tutorials.
C implementation#include <cv.h> #include <highgui.h> int main() { IplImage *im; CvCapture *cam = cvCaptureFromCAM(0); while(1) { im = cvQueryFrame(cam); cvShowImage(im); if(cvWaitKey(10) == 27) break; } cvReleaseImage(&im); cvReleaseCapture(&cam); cvDestroyAllWindows(); } ExplanationThe algorithm has been already explained in the simplest possible way in the introductory paragraph. So i continue to the explanation of the newly introduced functions
CvCapture *cam = cvCaptureFromCAM(0); CvCapture is just a video capture stricture. This can be used to create structures which can be used in various video capturing functions as parameter.
cvCaptureFromCAM(int flag) is function used too initializing a capture from a camera. The parameter specifies the camera to be used. Generally '0' means the default camera. '-1' means that only one camera is connected. cvCaptureFromCAM will therefore allocate and initialize the CvCapture structure making it ready for capturing the frames from the camera. Now the Structure cam will be pointing to the camera specified by the parameter flag. im = cvQueryFrame(cam);
cvQueryFrame(CvCapture* camera) is used to grab a frame from the camera and then retrieve and pass the frame into the IplImage stucture named im. The parameter is the address of the camera from which the frame must be grabbed. This means that it is possible to handle multiple cameras in a single OpenCV program itself.
|
C++ implementation#include <opencv2/opencv.hpp> using namespace cv; int main() { Mat im; VideoCapture cam(0); while(1) { cam.read(im); imshow("video",im); if(waitKey(10) == 27) break; } } ExplanationThe algorithm has been already explained in the simplest possible way in the introductory paragraph. So i continue to the explanation of the newly introduced functions
VideoCapture cam(0); VideoCapture is a class which is used to capture video frames from cameras/AVI files. There are a lot of methods available to capture frames from camera,from AVI files, to set the capture properties, to get the properties of the capture etc. The OOP approach simplifies the usage to a great extend. In the above step, an object named cam is created which is initialized by the constructor making the cam point toward the camera specified by the flag '0'.
To read more : VideoCapture cam.read(im);
cam >> im;
The above two lines does the same thing. It reads a frame from the cam and writes it into the Mat object im. You can follow either of the two methods mentioned above.
To read more: VideoCapture::read |