openCV 샘플 코드 (cpp 샘플 섹션 아래)에서 "bgfg_segm.cpp"라는 .cpp 파일을 찾을 수 있습니다. 그 코드는 모션 추적을위한 것이지만 나는 또한 당신이 그 반점을 탐지하는데 사용할 수 있다고 생각한다.
"스페이스 바 키"를 누르면 배경 업데이트를 시작/중지 할 수 있습니다. 일단 당신이 당신의 배경을 결정하면, 여분의 것이 그것의 자리로 감지 될 것입니다.
전략 : 웹캠 앞면의 천을 가져온 다음 배경으로 선택하고 "스페이스 바 키"를 눌러 배경에서의 변경을 중지하십시오. 그런 다음 프로그램이 옷감의 색상 변화를 추적 할 수 있어야합니다.
코드는 다음과 같습니다 :
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap;
bool update_bg_model = true;
cap.open(0);
namedWindow("image", CV_WINDOW_NORMAL);
namedWindow("foreground mask", CV_WINDOW_NORMAL);
namedWindow("foreground image", CV_WINDOW_NORMAL);
namedWindow("mean background image", CV_WINDOW_NORMAL);
// Declare "object " of class "BackgroundSubtractorMOG2"
BackgroundSubtractorMOG2 bg_model;//(100, 3, 0.3, 5);
Mat img, fgmask, fgimg;
for(;;)
{
cap >> img;
if(fgimg.empty())
fgimg.create(img.size(), img.type());
//update the model
bg_model(img, fgmask, update_bg_model ? -1 : 0); // "bg_model" is object of class "BackgroundSubtractorMOG2" as declared above.
fgimg = Scalar::all(0);
img.copyTo(fgimg, fgmask);
Mat bgimg;
bg_model.getBackgroundImage(bgimg);
imshow("image", img);
imshow("foreground mask", fgmask);
imshow("foreground image", fgimg);
if(!bgimg.empty())
imshow("mean background image", bgimg);
char k = (char)waitKey(30);
if(k == 27) break;
if(k == ' ') // Change the Background updation status by Spacebar key
{
update_bg_model = !update_bg_model; // initially "bool update_bg_model = true"
if(update_bg_model)
printf("Background update is on\n");
else
printf("Background update is off\n");
}
}
return 0;
}
출처
2014-01-19 15:18:26
skm