전경 물체를 감지하기 위해 비디오 처리 프로젝트를 진행 중입니다. 아래는 전경과 배경을 분리하는 데 사용되는 코드의 일부입니다.배경 빼기 개선
#include "opencv2/core/core.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;
//this is a sample for foreground detection functions
int main(int argc, const char** argv)
{
VideoCapture cap;
bool update_bg_model = true;
cap.open(0);
if(!cap.isOpened())
{
printf("can not open camera or video file\n");
return -1;
}
namedWindow("image", CV_WINDOW_NORMAL);
namedWindow("foreground mask", CV_WINDOW_NORMAL);
namedWindow("foreground image", CV_WINDOW_NORMAL);
namedWindow("mean background image", CV_WINDOW_NORMAL);
BackgroundSubtractorMOG2 bg_model;
Mat img, fgmask, fgimg;
for(;;)
{
cap >> img;
if(img.empty())
break;
if(fgimg.empty())
fgimg.create(img.size(), img.type());
//update the model
bg_model(img, fgmask, update_bg_model ? -1 : 0);
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 == ' ')
{
update_bg_model = !update_bg_model;
if(update_bg_model)
printf("Background update is on\n");
else
printf("Background update is off\n");
}
}
return 0;
}
전경 마스크 창에서 실제 전경 오브젝트와 함께 많은 노이즈가 발생합니다. 또한 fulll 객체는 전경으로 인식됩니다. 전경 오브젝트를 사각형으로 묶어야합니다. Wil BoundRect()는 전경 마스크에서 윤곽선을 그리면 작업을 수행합니까? ... 윤곽선을 찾는 동안 (findcontour()) 전달할 가장 권장되는 매개 변수는 무엇입니까? BoundRect 함수 ... 미리 감사드립니다.
이미지에 노이즈가있는 경우 (모든 비디오 에서처럼) 가장 간단한 해결책은 일종의 가우시안 블러를 실행하는 것입니다. 나는이 부분을 이해하지 못했다 : "또한 fulll 객체는 전경으로 인식된다"는 의미는 무엇입니까? 그것에 대해 좀 더 명확히해야합니다. – OopsUser
이 뜻은 다음과 같습니다. http://answers.opencv.org/upfiles/13623024413088434.png – hunter