Opencv tutorial simple code in C++ to capture video from File, Ip camera stream and also the web camera plug into the computer. The key is to have installed the FFMPEG especially in case of reading the stream of IP cameras. In Windows just use Opencv Installation by Nugets packages Here. Simple easy under 2 minutes of installation. In Linux, you need to follow the instruction below. If you are on Debian Like package system. Under Fedora Red hat dist just use a different approach. Code is simple and installation is the key..
Windows use nugets packages
Linux you have to install and build Opencv With FFMPEG. Also simple.
It is easy to capture video in OpenCV
Video capture
in OpenCV is a really easy task, but for a little bit experienced user.
in OpenCV is a really easy task, but for a little bit experienced user.
The problem is the installation of Opencv without recommended dependencies.
Just install all basic libs that are recommended on the website.
# Basic packages
sudo apt-get -y install build-essential
sudo apt-get -y install cmake
sudo apt-get -y install pkg-config
# Basic dependencies sudo apt-get -y install libgtk2.0-dev python-dev python-numpyYou need to download and build my own in the case of Debian Jessie or some version of Ubuntu.# OpenCV dependencies part II. sudo apt-get -y install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils libv4l-0 libv4l-dev sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev sudo apt-get -y install libdc1394-22-dev libdc1394-utils sudo apt-get -y install libjpeg-dev libpng-dev libtiff-dev libjasper-dev sudo apt-get -y install libtiff5 libtiff5-dev sudo apt-get -y install libopenexr-dev sudo apt-get -y install libjasper-dev # Algebra sudo apt-get -y install libeigen3-devInstall ffmpeg for Opencv.
Just download, and extract tar archive ffmpeg.tar.bz2.
1 configure instalation ./configure -- with Right Params.
2 Make - make -j4
3 install - make install
4 write config ldconfig -v
Compile opencv
Check this table during the OpenCV installation. if you have FFMPEG and all AV libs
AVCODEC AVFORMAT AVUTIL AVSWSCALE you can continue with OpenCV installation.
![]() |
| Opencv config |
Read video file in Opencv
VideoCapture capture("input.mp4");Read rtsp stream in Opencv
VideoCapture capture("rtsp://USER:PASS@xxx.xxx.xxx.xxx/axis-media/media.amp?camera=2");Some of my next post will be about rtsp stream format. PLS subscribe.
Read web camera in Opencv
VideoCapture capture(0);Opencv 3 and higher c++ code
#include <vector>
#include <fstream>
#include <iostream>
#include <math.h>
#include <Windows.h>
#include "opencv2/video/video.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
using namespace std;
using namespace cv;
//Chose input
//VideoCapture capture(0);
VideoCapture capture("rtsp://USER:PASS@xxx.xxx.xxx.xxx/axis-media/media.amp?camera=2");
//VideoCapture capture("input.mp4");
// create mat to fill by external source
Mat frame;
for(;;)
{
bool OK = capture.grab();
if (OK == false){
cout << "cannot grab" << endl;
}
else{
// retrieve a frame of your source
capture.read(frame);
//OR
// capture >> frame;
}
}
