【发布时间】:2014-04-26 09:33:55
【问题描述】:
我正在使用 OpenCV 进行运动检测并为此使用背景减法算法。我从网上得到了以下代码。
cv::Mat frame;
cv::Mat back;
cv::Mat fore;
cv::VideoCapture cap(0);
bg.nmixtures = 3;
bg.bShadowDetection = false;
const int history = 5;
cv::BackgroundSubtractorMOG2 bg (history,nmixtures,bShadowDetection);
std::vector<std::vector<cv::Point> > contours;
cv::namedWindow("Frame");
cv::namedWindow("Background");
for(;;)
{
cap >> frame;
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
cv::erode(fore,fore,cv::Mat());
cv::dilate(fore,fore,cv::Mat());
cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
cv::imshow("Frame",frame);
cv::imshow("Background",back);
if(cv::waitKey(30) >= 0) break;
}
所以我可以设置一个阈值,以便如果新旧框架的变化超过阈值,那么不要做任何事情。或者可能是其他一些适合我只捕获缓慢移动物体的情况的算法。
【问题讨论】:
标签: c++ opencv image-processing motion-detection