The first section motion analysis
The video module of OpenCV provides KNN and MOG2 motion analysis algorithms.
1、cv::createBackgroundSubtractorKNN
Create a background extractor based on the KNN algorithm.
Ptr cv::createBackgroundSubtractorKNN(int history = 500,double dist2Threshold = 400.0,bool detectShadows = true)
parameter name | Parameter Description |
---|---|
history | History frame length |
dist2Threshold | The threshold of the squared distance between the pixel and the sample to determine whether the pixel is close to the sample. This parameter does not affect the background update. |
detectShadows | If true, the algorithm will detect shadows and mark them. It will slow down slightly, so if you don't need this feature, please set the parameter to false. |
The class BackgroundSubtractorKNN inherits the class BackgroundSubtractor, in which the apply of BackgroundSubtractor is the core method. The parameters are described as follows:
parameter name | Parameter Description |
---|---|
image | Next video frame |
fgmask | The output foreground mask is an 8-bit binary image. |
learningRate | A value between 0 and 1 indicates the speed of learning the background model. Negative parameter values cause the algorithm to use some automatically selected learning rate. 0 means that the background model is not updated at all, and 1 means that the background model is completely reinitialized from the last frame. |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 打开摄像头或视频
cv::VideoCapture cap("videos/vtest.avi");
if(!cap.isOpened()){
cerr << "cannot open camera.\n";
return EXIT_FAILURE;
}
// 创建背景提取器
cv::Ptr<cv::BackgroundSubtractorKNN> backgroundSubtractorKNN = cv::createBackgroundSubtractorKNN();
cv::Mat frame,fgmask,backgroundImage;
while(cap.isOpened()){
cap >> frame;
if(frame.empty()){
cerr << "cannot grab frame from camera.\n";
break;
}
// 执行前景和背景分离
backgroundSubtractorKNN->apply(frame,fgmask);
// 获取背景图像
backgroundSubtractorKNN->getBackgroundImage(backgroundImage);
cv::imshow("camera",frame);
cv::imshow("fgmask",fgmask);
cv::imshow("background",backgroundImage);
if(cv::waitKey(10) == 27){
break;
}
}
cv::destroyAllWindows();
return 0;
}
2、cv::createBackgroundSubtractorMOG2
Create a background extractor based on the MOG2 algorithm.
Ptr cv::createBackgroundSubtractorMOG2(int history = 500,double varThreshold = 16,bool detectShadows = true)
The parameters are as follows:
parameter name | Parameter Description |
---|---|
history | History frame length |
varThreshold | The threshold of the Mahalanobis distance squared between the pixel and the model to determine whether the background model describes the pixel well. This parameter does not affect the background update. |
detectShadows | If true, the algorithm will detect shadows and mark them. It will slow down slightly, so if you don't need this feature, please set the parameter to false. |
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
// 打开摄像头或视频
cv::VideoCapture cap("videos/vtest.avi");
if(!cap.isOpened()){
cerr << "cannot open camera.\n";
return EXIT_FAILURE;
}
// 创建背景提取器
cv::Ptr<cv::BackgroundSubtractorMOG2> backgroundSubtractor = cv::createBackgroundSubtractorMOG2();
cv::Mat frame,fgmask,backgroundImage;
while(cap.isOpened()){
cap >> frame;
if(frame.empty()){
cerr << "cannot grab frame from camera.\n";
break;
}
// 执行前景和背景分离
backgroundSubtractor->apply(frame,fgmask);
// 获取背景图像
backgroundSubtractor->getBackgroundImage(backgroundImage);
cv::imshow("camera",frame);
cv::imshow("fgmask",fgmask);
cv::imshow("background",backgroundImage);
if(cv::waitKey(10) == 27){
break;
}
}
cv::destroyAllWindows();
return 0;
}