valerio_sperati
valerio_sperati

Reputation: 823

opencv MeanShift: which is the area interested?

a very basic question about meanShift algorithm in opencv and c++! It's not clear to me which is the area within which the searching and the corresponding shift is done. I mean: I implemented a very basic example where I furnish to the meanShift function the following 3 parameters:

1) a backprojection Matrix A,

2) an initial area B where to start the searching, cv::Rect B(x,y,w,h);

3) a TermCriteria object C,
cv::TermCriteria C(cv::TermCriteria::MAX_ITER, 10, 0.01);

Then I pass these argument to the function: cv::meanShift(A, B, C);

Now, the algorithm works fine, in the sense that the Rectangle B is shifted from its initial position B to a new position correspondent to the searched histogram. Nevertheless the new position is always very close to the initial position. If the searched object is not very close to the starting position B, no shift is performed. I mean: it is as if the searching is performed only in the proximity of the initial position. Is it possible to tell the algorithm to search in a wider area? I tried to play a little bit with parameters in TermCriteria, but it seems not to influence the area within which the searching must be performed. Thanks a lot! Valerio

Upvotes: 1

Views: 703

Answers (1)

QED
QED

Reputation: 808

I think you are correct in "I tried to play a little bit with parameters in TermCriteria".

Assuming your working in the image domain, looking at the documentation, it appears as the the epsilon criteria refers to the minimum distance moved, you have set this to 0.01 pixels. Try changing that to larger number that represents the minimum amount of pixels that you want it to shift, for instance set it to "1" for 1 pixel. The MAX_ITER criteria will also play a role here, you have this set to "10" so the current maximum shift distance is 10*0.01 = 0.1 pixels. If you want larger shifts, set this to a lager number, i.e 100. Now with:

    cv::TermCriteria C(cv::TermCriteria::MAX_ITER | cv::TermCriteria::EPS, 100, 1.0);

the minimum shift is 1 pixel and the maximum is 100 pixels (edit: actually this is more like a lower bound on the maximum, since it could shift > 1 pixel in an iteration).

^^Please note that you had not added the cv::TermCriteria::EPS at all to the arg list also.

If this still doesn't work then you may have issues with noise in you data causing the algorithm to halt in the wrong place, so some pre-processing maybe required to remove some off the noise.

Upvotes: 2

Related Questions