Meanshift - Cleaning up back projection

asked 2016-07-05 11:49:02 -0500

logidelic gravatar image

In the OpenCV meanshift documentation, it states that:

You can simply pass the output of calcBackProject to this function. But better results can be obtained if you pre-filter the back projection and remove the noise. For example, you can do this by retrieving connected components with findContours , throwing away contours with small area ( contourArea ), and rendering the remaining contours with drawContours.

Since I love "better results", I've been trying to do exacly what has been suggested, but so far the reults are not useable. Here is my code (where backProjection is the original back projection created by calcBackProject():

// Clean up back projection by selecting for well-sized contour areas
vector< vector<Point> > contours;
vector<Vec4i>           hierarchy;
findContours(backProjection, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
backProjection = Mat::zeros(backProjection.rows, backProjection.cols, backProjection.type());
for(unsigned i = 0; i < contours.size(); ++i) {
  double area = contourArea(contours[i]);
  if(area >= MIN_AREA && area <= MAX_AREA)
    drawContours(backProjection, contours, i, Scalar(255,255,255), CV_FILLED, 8);
}

Sometimes the result is useable: the ROI is white and most of the rest of the image is black. However, for other situations, the ROI is completely black (no matter the values of MIN_AREA/MAX_AREA above).

What am I doing wrong? Any code out there for cleaning up a back-projection in a generic way?

Thanks!

edit retag flag offensive close merge delete