问题
I am using visual 2010 (C++) with opencv 2.3.1 to construct this code for background subtraction using MOG2. As shown in the full code here it works successfully but with much noise.
can anyone suggest on how to reduce this noise. some one (thanks for him) has told me to increase the size of the kernel instead of using this morphological function:
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);
}
But i really do not know how to do that because i am still a beginner. Another one (thanks for him) has suggested for solving the contours problem noticed if you run the code "Two tips. Seems you are just taking one point from many contours. Try averaging them. Another is to damp over time. Example damped_x=damped_x*0.9+real_x*0.1; also this part i do not know how to do it.
you can see the portion of the code containing the contours in the following section, i intentionally used this to have blobs on the other smaller objects moving because i already used another instruction to track the biggest object only ( this is not a good choice but i couldnt make it track all so i just bound them with blobs instead).
//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;
}
This is the instruction to select the biggest object to be tracked:
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";
}
could you guys help me . Thanks in advance.
来源:https://stackoverflow.com/questions/37116423/how-to-reduce-the-noise-and-enhance-the-appearance