Adjusting Viz camera parameters yields stuttering effect

asked 2017-08-11 00:30:50 -0600

I am trying to make an animation with OpenCV's viz module (version 3.3; but 3.2 seems to have the same issue). More precisely, I am adjusting the intrinsic camera parameters programmatically to illustrate their effect. So far, everything works fine, except for the fact that the animation yields a strange stuttering effect. For example, when adjusting the focal length, there is a slight up-and-down movement in every second step of the animation. The movement is reproducible (albeit unwanted) and always between the same two (relative) positions. I reduced my animation code to the following minimum:

#include <iostream>
#include <opencv2/viz.hpp>

using namespace std;
using namespace cv;
using namespace cv::viz;

int main()
{
  Viz3d window("Test");
  window.showWidget("Test object", WCone(1, 0.5, 100));
  window.spinOnce(1, true); //If this is not called here, getCamera returns different values and setCamera below throws a stack underflow exception!?
  const auto camera = window.getCamera();
  const auto focal_length = camera.getFocalLength();
  const int fx = focal_length[0]; //Deliberate round to int
  const int fy = focal_length[1];
  const auto principal_point = camera.getPrincipalPoint();
  const int px = principal_point[0];
  const int py = principal_point[1];

  for (int i = 1; i <= 30; i++) 
  {
    cout << "Iteration " << i << endl;
    const auto old_camera = window.getCamera();
    Matx44d matrix;
    old_camera.computeProjectionMatrix(matrix);
    cout << "Old proj. matrix: " << matrix << endl;
    Camera new_camera(fx - 10 * i, fy, px, py, old_camera.getWindowSize());
    new_camera.computeProjectionMatrix(matrix);
    cout << "New proj. matrix: " << matrix << endl;
    window.setCamera(new_camera);
    window.spinOnce(500, true);
  }
  return 0;
}

I am aware that numerical inaccuracies could be to blame for this, but the effect is far to severe for it only being that. The effect becomes even stronger for larger deviations from the initial parameters and increases when I change multiple parameters at once.

How can I reduce this stuttering effect? It remains (to the same extent) even when I change 10 * i to i for very small animation steps.

Remark: I also posted this here a while ago, but did not get any replies

edit retag flag offensive close merge delete