Ask Your Question

dustsigns's profile - activity

2017-08-24 07:32:07 -0500 asked a question Correct use of fisheye::distortPoints()

I am trying to figure out how to distort (not undistort!) an image. This is the code that I am using:

#include <iostream>

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>

using namespace std;

using namespace cv;
using namespace cv::fisheye;

static Mat_<Point2f> GetDistortionMapping(const Size2i &image_size, const array<float, 4> &distortion_vector)
{
  Mat_<float> empty_camera_matrix = Mat::eye(3, 3, CV_32F);
  empty_camera_matrix(2, 0) = image_size.width / 2.f;
  empty_camera_matrix(2, 1) = image_size.height / 2.f;
  Mat_<Point2f> image_points(image_size);
  for (int y = 0; y < image_size.height; y++)
    for (int x = 0; x < image_size.width; x++)
      image_points(x, y) = Point2f(y, x);
  Mat_<Point2f> distorted_points(image_size);
  distortPoints(image_points, distorted_points, empty_camera_matrix, distortion_vector);
  return distorted_points;
}

static void ShowImage(const Mat &image)
{
  namedWindow("Distorted");
  const array<float, 4> distortion_vector { 1.f, 0.f, 0.f, 0.f };
  const Mat distortion_mapping = GetDistortionMapping(image.size(), distortion_vector);
  Mat distorted_image;
  remap(image, distorted_image, distortion_mapping, noArray(), INTER_LANCZOS4);
  imshow("Distorted", distorted_image);
}

int main(const int argc, const char * const argv[])
{
  if (argc != 2)
  {
    cout << "Illustrates the effect of the distortion vector on a camera." << endl;
    cout << "Usage: " << argv[0] << " <camera image>" << endl;
    return 1;
  }
  const auto filename = argv[1];
  const Mat image = imread(filename);
  if (image.empty())
  {
    cerr << "Could not read input image '" << filename << "'" << endl;
    return 2;
  }
  ShowImage(image);
  waitKey(0);
  return 0;
}

I am using cv::fisheye::distortPoints and pass it a matrix with all image coordinates (0, 0), (0, 1), ... (h - 1, w - 1). Then I use remap to perform the actual remapping of the points. I have worked with initUndistortRectifyMap successfully, but I can't seem to get any meaningful result out of distortPoint (see below for an example with the Lenna image).

image description

I tried different distortion vector values as well as a identity matrix as empty_camera_matrix(i.e., without setting the principal point). Am I using the function incorrectly or is this approach wrong altogether?

2017-08-11 04:04:17 -0500 received badge  Editor (source)
2017-08-11 03:17:22 -0500 asked a question Setting pose of Viz3d changes camera parameters

When I call Viz3d::setViewerPose, the camera parameters of the Viz3d window change. In particular, it seems to zoom out significantly, even when the pose that I set is unchanged, i.e., when I set the one I can obtain with Viz3d::getViewerPose. The following code illustrates the issue:

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

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

int main()
{
  Viz3d visualization("Test");
  visualization.showWidget("Test object", WCone(1, 0.5, 100));
  /*const auto pose = visualization.getViewerPose();
  visualization.setViewerPose(pose);*/ //Uncomment this line and the one above it to see the described effect

  //cout << visualization.getCamera().getClip() << endl; //Debugging (uncomment this for an even stranger effect)

  visualization.spinOnce(1, true);
  while (!visualization.wasStopped())
    visualization.spinOnce(1, true);
  return 0;
}

When the two lines of code above are commented, the window looks like this: Code commented (no effect)

When the two lines of code above are uncommented, the window looks like this: Code uncommented (effect visible)

To pinpoint the issue, I added a line that outputs the changing camera parameter (which I found after some debugging). Printing this camera parameter gives different results, depending on whether setViewerPose is called, i.e., whether the two lines above it are commented. The strange thing, however, is, that just calling Viz3d::getCamera() (here, for printing) makes the "zoom" effect I described above disappear.

I don't know whether either of the two effects are identended and I don't know how to make them go away. In a more complex example, I tried to adjust the pose in the same way, but I could not do so because the camera kept "zooming" when I just read some parameters, making it impossible to set or change values in a meaningful way. Is this a bug, perhaps?

2017-08-11 00:30:50 -0500 asked a question Adjusting Viz camera parameters yields stuttering effect

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

2015-04-29 03:51:56 -0500 received badge  Enthusiast
2015-04-22 04:43:26 -0500 commented question pkg-config file for 3.0.0 beta incomplete?

@mshabunin: Thank you very much. There is nothing to add at the moment (since you provided the link to this question with the examples), but I'll provide any necessary information if required.

2015-04-22 01:59:34 -0500 commented question pkg-config file for 3.0.0 beta incomplete?

Unfortunately, I cannot use cmake in my use case. Would it make send to file a bug report by reporting this issue?

2015-04-21 08:18:33 -0500 commented question pkg-config file for 3.0.0 beta incomplete?

@StevenPuttemans: Thanks for the hint, but this does not solve the problem with the linker errors, unfortunately, since the pkg-config file is still incomplete.

2015-04-21 08:00:20 -0500 received badge  Student (source)
2015-04-21 07:59:20 -0500 asked a question pkg-config file for 3.0.0 beta incomplete?

Hello,

I am currently switching from OpenCV 2.4.9 to 3.0.0 beta on my 64-bit Ubuntu 14.04 system. I built OpenCV from source, installed it and tried to link against it using a Makefile that worked fine with OpenCV 2.4.9. It basically looks like this:

CXX ?= g++

CXXFLAGS += -c -Wall `pkg-config --cflags opencv`
LDFLAGS += -static `pkg-config --libs --static opencv`

all: test.exe

test.exe: test.o
    $(CXX) $< -o $@ $(LDFLAGS)

%.o: %.cpp
    $(CXX) $< -o $@ $(CXXFLAGS)

clean:
    rm -f test.o test.exe

I am using the following minimal example source file (test.cpp):

#include <iostream>
#include "opencv2/core/core.hpp"

using namespace std;
using namespace cv;

int main(const int argc, const char * const argv[])
{
  cout << getBuildInformation() << endl;
  return 0;
}

Linking fails due to several unresolved references. Although it was not hard to figure out that I had to add -lpthread -lz -ldlto LDFLAGS, it seemed strange to me that pkg-config did not do that already. When I looked at the .pc file of OpenCV in the pkg-config directory, I noticed that no other libraries than the OpenCV libraries themselves were listed there. In version 2.4.9., the pkg-config file included -lphread -lz -ldl and many other in its "Libs:" line, but 3.0.0 does not do so any longer. What is the reason for this? Is the pkg-config file incomplete on purpose or is this an artifact of the beta version? If so, is there any way to get a complete pkg-config file like in 2.4.9 before the final release?

Best regards Andreas