# 1. VideoCaputre

Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences. Here is how the class can be used:

#### videoCapture constructors.

{% hint style="info" %}

1. `videoCapture::VideoCapture`()
2. `videoCapture::VideoCapture`(const string& **filename**)
3. `VideoCapture::VideoCapture`(int **device**)
   {% endhint %}

## Read Camera example

```cpp
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

int main(int argc, const char** argv)
{
    cv::VideoCapture    capture;
    cv::Mat             frame;

    capture.open(0);

    if (!capture.isOpened())
    {
        std::cout << "[DEBUG] open video capture error\n";
        return -1;
    }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            std::cout << "[DEBUG] No captured frame -- Break!\n";
            break;
        }

        if (cv::waitKey(30) == 27)
            break;
        cv::imshow("Capture - Face detection", frame);
    }
    return 0;
}

```

![](https://2068337880-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LRNG-039a0zYcsO2RWf%2F-LSAfVwXot_V8Fq_kq7b%2F-LSAguIdyc_UpLO1Sjqw%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-11-26%20%E4%B8%8A%E5%8D%8812.28.15.png?alt=media\&token=7d4295a6-83f2-46d2-819c-9a1953267a79)

## Morphological Gradient

```cpp
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/opencv.hpp>

#include <iostream>

void func(cv::Mat frame);

int main(int argc, const char** argv)
{
    cv::VideoCapture    capture;
    cv::Mat             frame;

    capture.open(0);

    if (!capture.isOpened())
    {
        std::cout << "[DEBUG] open video capture error\n";
        return -1;
    }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            std::cout << "[DEBUG] No captured frame -- Break!\n";
            break;
        }

        func(frame);
        
        if (cv::waitKey(30) == 27)
            break;
    }
    return 0;
}

void func(cv::Mat frame)
{
    cv::Mat dstImage;  
    int morph_size = 3;
    int morph_elem = cv::MORPH_RECT;

    cv::Mat element = cv::getStructuringElement(
        morph_elem,
        cv::Size(2 * morph_size + 1, 2 * morph_size + 1),
        cv::Point(morph_size, morph_size));

    cv::morphologyEx(frame, dstImage, cv::MORPH_GRADIENT, element);

    cv::imshow("Capture - Face detection", dstImage);
}
```

![](https://2068337880-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LRNG-039a0zYcsO2RWf%2F-LSApGb9g0ZCiy0b9He5%2F-LSApKe5AFba_9QYGkJF%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-11-26%20%E4%B8%8A%E5%8D%881.04.31.png?alt=media\&token=ea064c41-034e-4f1d-88d3-1d7e0e029aae)

## Face Detection using Haar Cascades

```cpp
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/opencv.hpp>

#include <iostream>
#include <string>

cv::Mat func(cv::Mat frame);
void detectAndDraw(cv::Mat frame, cv::CascadeClassifier& cascade);

int main(int argc, const char** argv)
{
    std::string             cascadeName;
    cv::VideoCapture        capture;
    cv::Mat                 frame;
    cv::CascadeClassifier   cascade;

    capture.open(0);

    if (!cascade.load("haarcascade_frontalface_alt.xml"))
    {
        std::cout << "[DEBUG] Could not load classifier cascade\n";
        return -1;
    }
    if (!capture.isOpened())
    {
        std::cout << "[DEBUG] open video capture error\n";
        return -1;
    }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            std::cout << "[DEBUG] No captured frame -- Break!\n";
            break;
        }

        detectAndDraw(frame, cascade);
        
        if (cv::waitKey(30) == 27)
            break;
    }
    return 0;
}

cv::Mat func(cv::Mat frame)
{
    cv::Mat     dstImage;  
    int         morph_size = 3;
    int         morph_elem = cv::MORPH_RECT;

    cv::Mat element = cv::getStructuringElement(
        morph_elem,
        cv::Size(2 * morph_size + 1, 2 * morph_size + 1),
        cv::Point(morph_size, morph_size));

    cv::morphologyEx(frame, dstImage, cv::MORPH_GRADIENT, element);

    return dstImage;
}

void detectAndDraw(cv::Mat frame, cv::CascadeClassifier& face_cascade)
{
    std::vector<cv::Rect>   faces;
    cv::Mat                 frame_gray;
 
    cv::cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
    cv::equalizeHist(frame_gray, frame_gray);

    face_cascade.detectMultiScale(
        frame_gray, 
        faces, 
        1.1, 
        2, 
        0 | cv::CASCADE_SCALE_IMAGE, 
        cv::Size(30, 30));
    
    for (size_t i = 0; i < faces.size(); i++)
    {
        cv::Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2 );
        cv::rectangle(frame, faces[0], cv::Scalar(0, 0, 255), 2, 8, 0);
    }

    cv::imshow("Capture - Face detection", frame);
}
```

![](https://2068337880-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LRNG-039a0zYcsO2RWf%2F-LSFqyb5NGQauu2KOsX6%2F-LSFrnvIxD5kfEbRfb3F%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-11-27%20%E4%B8%8A%E5%8D%8812.34.04.png?alt=media\&token=baec880f-28b0-4bfd-b14a-c7f778f73fbf)
