我使用visual 2010(C ++)和opencv 2.3.1来构建使用MOG2进行背景扣除的代码。如完整代码here所示,它可以成功运行但噪音很大。
任何人都可以建议如何减少这种噪音。有人(感谢他)告诉我增加内核的大小而不是使用这种形态函数:
void morphOps(Mat &thresh){
Mat erodeElement = getStructuringElement( MORPH_RECT,Size(2,2)); //3x3
Mat dilateElement = getStructuringElement( MORPH_RECT,Size(1,1)); //8x8
erode(thresh,thresh,erodeElement);
erode(thresh,thresh,erodeElement);
dilate(thresh,thresh,dilateElement);
dilate(thresh,thresh,dilateElement);
}
但我真的不知道该怎么做,因为我还是初学者。另一个(感谢他)建议解决轮廓问题,如果你运行代码“两个提示。似乎你只是从许多轮廓中取一个点。尝试平均它们。另一个是随着时间的推移而潮湿。 damped_x = damped_x * 0.9 + real_x * 0.1; 此部分我不知道该怎么做。
你可以在下一节中看到包含轮廓的代码部分,我有意使用它来移动其他较小的对象,因为我已经使用了另一条指令来跟踪最大的对象(这不是一个好的选择,但我不能让它跟踪所有,所以我只是用blob绑定它们。)
//Find contour
ContourImg = binaryImage.clone();
//less blob delete
vector< vector< Point> > contours;
findContours(ContourImg,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // all pixels of each contours
vector< Rect > output;
vector< vector< Point> >::iterator itc= contours.begin();
while (itc!=contours.end()) {
//Create bounding rect of object
//rect draw on origin image
Rect mr= boundingRect(Mat(*itc));
rectangle(frame, mr, CV_RGB(255,0,0));
++itc;
}
这是选择要跟踪的最大对象的指令:
void searchForMovement(Mat binaryImage, Mat &framein){
bool objectDetected = false;
Mat temp;
binaryImage.copyTo(temp);
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(temp,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE );// retrieves external contours
//if contours vector is not empty, we have found some objects
if(contours.size()>0)objectDetected=true;
else objectDetected = false;
if(objectDetected){
//the largest contour is found at the end of the contours vector
//we will simply assume that the biggest contour is the object we are looking for.
vector< vector<Point> > largestContourVec;
largestContourVec.push_back(contours.at(contours.size()-1));
//make a bounding rectangle around the largest contour then find its centroid
objectBoundingRectangle = boundingRect(largestContourVec.at(0));
int xpos = objectBoundingRectangle.x+objectBoundingRectangle.width/2;
int ypos = objectBoundingRectangle.y+objectBoundingRectangle.height/2;
//update the objects positions by changing the 'theObject' array values
theObject[0] = xpos , theObject[1] = ypos;
}
//make some temp x and y variables so we dont have to type out so much
int x = theObject[0];
int y = theObject[1];
//draw some crosshairs around the object
circle(framein,Point(x,y),20,Scalar(0,255,0),2);
line(framein,Point(x,y),Point(x,y-25),Scalar(0,255,0),2);
line(framein,Point(x,y),Point(x,y+25),Scalar(0,255,0),2);
line(framein,Point(x,y),Point(x-25,y),Scalar(0,255,0),2);
line(framein,Point(x,y),Point(x+25,y),Scalar(0,255,0),2);
//write the position of the object to the screen
putText(framein,"Tracking object at (" + intToString(x)+","+intToString(y)+")",Point(x,y),1,1,Scalar(255,0,0),2);
file_ <<"X:"<<intToString(x)<<" "<<"Y:"<<intToString(y)<<"\n";
}
你们可以帮助我吗?提前致谢。