Opencv VideoCapture File, Camera and stream

Opencv VideoCapture File, Camera and stream

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 espetially in case of reading the strem of IP cameras. In windows just use Opencv Installation by Nugets packages Here. Simple easy under 2 minutes instalation. In Linux you need to follow the instruction bellow. If you are on Debian Like package system. Under Fedora Red hat dist just use different aproach. 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 capture video in opencv

Video capture
 in opencv is really easy task, but for little bit experienced user. 

 

What is problem?
Problem is instalation of Opencv without recommended dependencies.

Just install all basic libs that is recommended on 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-numpy

# 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-dev

Install ffmpeg .

You need to download and build by own in case of Debian Jessie or some version of Ubuntu.
Just download, 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 instalation. if you have FFMPEG and all AV libs 

AVCODEC AVFORMAT AVUTIL AVSWSCALE you can continue with opencv installation.

opencv config ffmpef
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 
#include 
#include 

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;
       }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV VideoCapture is a class that provides an interface for capturing video from cameras or files. It is a part of the OpenCV library that is used for computer vision applications. VideoCapture can be used to capture live video from a webcam or to read video files. To use VideoCapture, you need to create an instance of the class and specify the device or file you want to capture video from. The device is identified by its index number, which starts from 0. For example, to capture video from the default camera, you can use the following code: ```python import cv2 cap = cv2.VideoCapture(0) ``` This creates a VideoCapture object and sets it to capture video from the first camera device available on the system. You can also specify a video file to read using the constructor. For example, to read a video file named "test.mp4", you can use the following code: ```python import cv2 cap = cv2.VideoCapture("test.mp4") ``` Once you have created a VideoCapture object, you can use the read() method to read frames from the video stream. The read() method returns a tuple containing a boolean value and a frame. The boolean value indicates whether the frame was successfully read or not, and the frame is a NumPy array containing the image data. ```python import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if ret: cv2.imshow("Frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ``` This code captures video from the default camera and displays each frame in a window. The loop continues until the user presses the 'q' key to quit. Finally, the VideoCapture object is released and the display window is destroyed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值