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.

  1. videoCapture::VideoCapture()

  2. videoCapture::VideoCapture(const string& filename)

  3. VideoCapture::VideoCapture(int device)

Read Camera example

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

Morphological Gradient

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

Face Detection using Haar Cascades

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

Last updated

Was this helpful?