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

```

![](/files/-LSAguIdyc_UpLO1Sjqw)

## 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);
}
```

![](/files/-LSApKe5AFba_9QYGkJF)

## 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);
}
```

![](/files/-LSFrnvIxD5kfEbRfb3F)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stanley7342.gitbook.io/programming/opencv/1.-videocaputre.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
