背景和前景在OpenCV中 [英] Background and foreground in OpencV

查看:757
本文介绍了背景和前景在OpenCV中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用OpenCV243工作,我需要一个流中拿到前台,我的问题是,我用CV :: absdiff得到它并不能真正帮助,这是我的code和的结果。

i'm working on a project using OpenCV243, I need to get the foreground during a stream, my Problem is that I use the cv::absdiff to get it doesn't really help, here is my code and the result .

#include <iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\calib3d\calib3d.hpp>
#include<opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

int main (){
    cv::VideoCapture cap(0);
    cv::Mat frame,frame1,frame2;
    cap >> frame;
    frame.copyTo(frame1);
    cv::imwrite("background.jpeg",frame1);
    int key = 0;
    while(key!=27){
        cap >> frame;
        cv::absdiff(frame, frame1, frame2);  // frame2 = frame  -frame1
        cv::imshow("foreground", frame2);
        if(key=='c'){
            //frame.copyTo(frame2);
            cv::imwrite("foreground.jpeg", frame2); 
            key = 0;
        }

        cv::imshow("frame",frame);
        key = cv::waitKey(10);
    }
    cap.release();
    return 0;
}



正如你所看到的减法工作,但我想要得到的是的,改变,例如,如果在后台是 [130130130] 并在同一像素的 [200200200一个像素只值] 在帧我要得到完全的最后一个值,而不是 [70,70,70]
我已经看到这个教程: http://mateuszstankiewicz.eu/?p=189
但我不明白的code和我有问题设置CV :: BackgroundSubtractorMOG2我OpenCV的版本

as you can see the subtraction work but what I want to get is only the values of that changed for example if have a Pixel in the background with [130,130,130] and the same pixel has [200,200,200] in the frame I want to get exactly the last values and not [70,70,70] I've already seen this tutorial : http://mateuszstankiewicz.eu/?p=189 but I can't understand the code and I have problems setting cv::BackgroundSubtractorMOG2 with my openCV version

在此先感谢您的帮助。

推荐答案

BackgroundSubtractorMOG2应的#includeopencv2 /视频/ background_segm.hpp工作
与OpenCV的样品有两个不错的C ++示例(在samples \\ CPP目录)。

BackgroundSubtractorMOG2 should work with #include "opencv2/video/background_segm.hpp" The samples with OpenCV have two nice c++ examples (in the samples\cpp directory).


  • bgfg_segm.cpp展示了如何使用BackgroundSubtractorMOG2

  • bgfg_gmg.cpp使用BackgroundSubtractorGMG

要获得最后的值(和asuming你的意思是让前台的像素值),你可以复制使用前景掩模框架。这也是在第一个例子做,下面的代码片段:

To get the last values (and asuming you meant to get the foreground pixel values) you could copy the frame using the foreground mask. This is also done in the first example, in the following snippet:

bg_model(img, fgmask, update_bg_model ? -1 : 0);
fgimg = Scalar::all(0);
img.copyTo(fgimg, fgmask);

这篇关于背景和前景在OpenCV中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆