OpenCV源码系列|背景分割

2022-06-16 16:00:51 浏览数 (1)

应用场景:静态摄像头统计路过的行人数量,交通摄像头提取路过的交通车辆等。

技术核心:从静态的背景中把动态的物体提取并分割。

输入视频:打开摄像头,没有物体进入(仅仅做的截图)

输出视频:手机突然进入摄像头范围内

代码实现:

代码语言:javascript复制

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/video.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, const char** argv)
{
    const String keys = "{c camera     | 0 | use video stream from camera (device index starting from 0) }"
                        "{fn file_name |   | use video file as input }"
                        "{m method | mog2 | method: background subtraction algorithm ('knn', 'mog2')}"
                        "{h help | | show help message}";
    CommandLineParser parser(argc, argv, keys);
    parser.about("This sample demonstrates background segmentation.");
    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }
    int camera = parser.get<int>("camera");
    String file = parser.get<String>("file_name");
    String method = parser.get<String>("method");
    if (!parser.check())
    {
        parser.printErrors();
        return 1;
    }

    VideoCapture cap;
    if (file.empty())
        cap.open(camera);
    else
    {
        file = file;  // ignore gstreamer pipelines
        cap.open(file.c_str());
    }
    if (!cap.isOpened())
    {
        cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
        return 2;
    }

    Ptr<BackgroundSubtractor> model;
    if (method == "knn")
        model = createBackgroundSubtractorKNN();
    else if (method == "mog2")
        model = createBackgroundSubtractorMOG2();
    if (!model)
    {
        cout << "Can not create background model using provided method: '" << method << "'" << endl;
        return 3;
    }

    cout << "Press <space> to toggle background model update" << endl;
    cout << "Press 's' to toggle foreground mask smoothing" << endl;
    cout << "Press ESC or 'q' to exit" << endl;
    bool doUpdateModel = true;
    bool doSmoothMask = false;

    Mat inputFrame, frame, foregroundMask, foreground, background;
    for (;;)
    {
        // prepare input frame
        cap >> inputFrame;
        if (inputFrame.empty())
        {
            cout << "Finished reading: empty frame" << endl;
            break;
        }
        const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
        resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);

        // pass the frame to background model
        model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);

        // show processed frame
        imshow("image", frame);

        // show foreground image and mask (with optional smoothing)
        if (doSmoothMask)
        {
            GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
            threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
        }
        if (foreground.empty())
            foreground.create(scaledSize, frame.type());
        foreground = Scalar::all(0);
        frame.copyTo(foreground, foregroundMask);
        imshow("foreground mask", foregroundMask);
        imshow("foreground image", foreground);

        // show background image
        model->getBackgroundImage(background);
        if (!background.empty())
            imshow("mean background image", background );

        // interact with user
        const char key = (char)waitKey(30);
        if (key == 27 || key == 'q') // ESC
        {
            cout << "Exit requested" << endl;
            break;
        }
        else if (key == ' ')
        {
            doUpdateModel = !doUpdateModel;
            cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
        }
        else if (key == 's')
        {
            doSmoothMask = !doSmoothMask;
            cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;
        }
    }
    return 0;
}

输入视频:http://mpvideo.qpic.cn/0bf25iaakaaammahggokbvpfb2wdaxvaabia.f10003.mp4?dis_k=41a63fe5b9f9f7f5273b07cfabd4218a&dis_t=1655366387&vid=wxv_1354502663010091008&format_id=10003&support_redirect=0&mmversion=false

输出视频:

代码实现:

代码语言:javascript复制

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    VideoCapture video = VideoCapture("cup.mp4");
    if (!video.isOpened()) {
        cout << "could not load video file ..." << endl;
        return -1;
    }
    
    Mat frame, bgMask_MOG2, bgMask_KNN, background;
    Mat kernel = getStructuringElement(MORPH_RECT, Size(3,3));
    
    namedWindow("highway_test", WINDOW_AUTOSIZE);
    namedWindow("background_mask_by_MOG2", WINDOW_AUTOSIZE);
    namedWindow("background_by_KNN", WINDOW_AUTOSIZE);
    namedWindow("background_mask_by_KNN", WINDOW_AUTOSIZE);
    
    Ptr<BackgroundSubtractor> ptrMOG2 = createBackgroundSubtractorMOG2();
    Ptr<BackgroundSubtractor> ptrKNN = createBackgroundSubtractorKNN();
    
    while (video.read(frame)) {
        imshow("highway_test", frame);
        
        ptrMOG2->apply(frame, bgMask_MOG2);
        morphologyEx(bgMask_MOG2, bgMask_MOG2, MORPH_OPEN, kernel);
        ptrKNN->apply(frame, bgMask_KNN);
        ptrKNN->getBackgroundImage(background);
        
        imshow("background_by_KNN", background);
        imshow("background_mask_by_MOG2", bgMask_MOG2);
        imshow("background_mask_by_KNN", bgMask_KNN);
        
        char c = waitKey(50);
        if (c == 27) {
            break;
        }
    }
    waitKey(0);
    video.release();
    return 0;
}

0 人点赞