Background and foreground in OpencV

I am working on a project using OpenCV243, I need to get the foreground during the stream, my problem is that I use cv :: absdiff to get it, it really does not 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; } 

Backgroundforeground as you can see the subtraction works, but what I want to get is only the values ​​that have been changed, for example, if the pixel in the background is from [130,130,130] and the same pixel has [200,200,200] in the frame I want to get exactly last values, not [70,70,70] I already saw this tutorial: http://mateuszstankiewicz.eu/?p=189 but I can’t understand the code and I have problems installing cv :: BackgroundSubtractorMOG2 with my version openCV

in advance for help

+6
source share
1 answer

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

  • bgfg_segm.cpp shows how to use BackgroundSubtractorMOG2
  • bgfg_gmg.cpp uses BackgroundSubtractorGMG

To get the latest values ​​(and if you want to get the values ​​of the foreground pixels), you can 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); 
+3
source

Source: https://habr.com/ru/post/946065/


All Articles