1
我想在使用mog2的OpenCV 2.4.10中實現背景扣除。我的目標是使用背景減法來分割手。不幸的是,用作前景的第一幀似乎在從網絡攝像頭實時捕捉期間卡住。下面是我用這個簡單的項目在OpenCV中的背景扣除
#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\video\video.hpp>
#include <opencv2\core\core.hpp>
#include <iostream>
#include <sstream>
#include <string.h>
using namespace cv;
int main()
{
Mat frame, mask, gray;
BackgroundSubtractorMOG2 mog2;
VideoCapture cap(0);
if (cap.isOpened()){
while (true)
{
if (cap.read(frame))
{
imshow("frame", frame);
cvtColor(frame, gray, cv::COLOR_RGB2GRAY);
imshow("gray", gray);
mog2(gray, mask, 0.0);// 0.1 is learning rate
imshow("Background Subtraction", mask);
if (waitKey(30) >= 0)
break;
}
}
}
cap.release();
return 0;
}
這裏的代碼輸出
是可能的,如果我可以刪除的第一幀,我相信結果會是在這種情況下很多。 @saransh – shah
顯然你正在採取一個現場輸入流。所以你不需要刪除第一幀。只要確保在前幾幀中沒有可移動的物體。 –