adi1992
adi1992

Reputation: 95

How to average continues translation vectors

I am doing pose estimation with AruCo markers using OpenCV in C++ on live webcam feed. My fps is 30 so when I am printing translation vectors, I am getting continues values of translation vectors i.e. 30 values per second. These values are fluctuating, therefore to make it more stable I want to average first 30 values and then print it and then next 30 values and print it so on. How to do it?. My code is as follows

aruco::estimatePoseSingleMarkers(markerCorners, arucoSquareDimension, cameraMatrix, distanceCoefficients, rotationVectors, translationVectors);

    for (int i = 0; i < markerIds.size(); i++)
    {
        aruco::drawAxis(Croppedframe, cameraMatrix, distanceCoefficients, rotationVectors[i], translationVectors[i], 0.1f);

        cout << translationVectors[i] << "translation" << "vector" << markerIds[i] << endl;

    }

Upvotes: 0

Views: 243

Answers (1)

api55
api55

Reputation: 11420

If translationVectors is a vector with the 30 translations as a cv::Mat you can always try a simple add and divide to get the average:

cv::Mat accum(3,1,CV_64F, cv::Scalar::all(0.));
for( const auto& t: translationVectors)
{
  accum += t;
}
// avoid division by zero
if (!translationVectors.empty())
  accum /= translationVectors.size();

However I would recommend for you to use kalman filter, it helps to make pose estimation stable. If not, at least a running average is a more accurate solution than taking 30 poses and then the next 30.

I have not tested the code, but the idea is there. If you run into a problem please comment the answer and I will try to help you more.

Upvotes: 1

Related Questions