The Background subtraction technique consists of obtaining the important objects over a background.
Now, let's see the methods available in OpenCV for the Background subtraction technique. Currently, the following four important techniques are required for this task:
Next, we are going to see an example (backgroundSubKNN
) using the KNN technique:
#include<opencv2/opencv.hpp> using namespace cv; using namespace std; int backGroundSubKNN() { //1-Set the parameters and initializations Mat frame; Mat background; Mat foreground; bool finish = false; int history = 500; double dist2Threshold = 400.0; bool detectShadows = false; vector< vector<Point>> contours; namedWindow("Frame"); namedWindow("Background"); VideoCapture capture(0); //Check if the video camera is opened if(!capture.isOpened()) return 1; //2-Create the background subtractor KNN Ptr <BackgroundSubtractorKNN> bgKNN = createBackgroundSubtractorKNN (history, dist2Threshold, detectShadows); while(!finish) { //3-Read every frame if possible if(!capture.read(frame)) return 1; //4-Using apply and getBackgroundImage method to get //foreground and background from this frame bgKNN->apply(frame, foreground); bgKNN->getBackgroundImage(background); //5-Reduce the foreground noise erode(foreground, foreground, Mat()); dilate(foreground, foreground, Mat()); //6-Find the foreground contours findContours(foreground,contours,RETR_EXTERNAL,CHAIN_APPROX_NONE); drawContours(frame,contours,-1,Scalar(0,0,255),2); //7-Show the results imshow("Frame", frame); imshow("Background", background); moveWindow("Frame", 0, 100); moveWindow("Background",800, 100); //Press Esc to finish if(waitKey(1) == 27) finish = true; } capture.release(); return 0; } int main() { backGroundSubKNN(); }
The Background subtracted frame and screen capture are shown in the following screenshot in which a person is walking in front of the camera:
The preceding example shows you two windows with the subtracted background images and draws contours of the person found. First, parameters are set as the distance threshold between background and each frame to detect objects (dist2Threshol
) and the disabling of the shadow detection (detectShadows
). In the second step, using the createBackgroundSubtractorKNN()
function, a background subtractor is created and a smart pointer construct is used (Ptr<>
) so that we will not have to release it. The third step is to read each frame, if possible. Using the apply()
and getBackgroundImage()
methods, the foreground and background images are obtained. The fifth step is to reduce the foreground noise by applying a morphological closing operation (in the erosion—erode()
—and dilation—dilate()
—order). Then, contours are detected on the foreground image and then they're drawn. Finally, the background and current frame image are shown.