1
我正在處理一個視頻處理項目,該項目將攝像頭饋送作爲輸入並獲得靜態背景。我不需要像openSv中BackgroundSubtractorMOG那樣的動態背景生成。我正在嘗試綁定邊界框內的前景對象。因此,這是我所做的從視頻流中提取前景
cv::absdiff(back_frame,matProcessed,temp); //back_frame is the background matProcessed is the frame from CAMERA in grayscale
cv::threshold(temp,temp,20,255,THRESH_BINARY);
cv::findContours(temp,contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
std::vector<Rect> boundRect(contours.size());
std::vector< std::vector<Point> > contours_poly(contours.size());
int j=0;
for(int i = 0; i < contours.size(); i++)
{
if(contourArea(contours[i])>100)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 10, true);
boundRect[j] = boundingRect(Mat(contours_poly[i]));
j++;
}
}
cv::rect r;
for (int i = 0; i < boundRect.size(); i++)
{
r = boundRect[i];
cv::rectangle(
frame,
cv::Point(r.x, r.y),
cv::Point(r.x + r.width, r.y + r.height),
CV_RGB(0,255,0)
);
}
但問題是我沒有得到正確的前景。無論如何,無論背景的複雜性和其他因素如何,我都可以改進前景生成,並始終使用一些矩形邊界框來限定前景對象。