Skip to content

Use of fisheye::distortPoints always yields extremely distorted image #10947

@dustsigns

Description

@dustsigns
System information (version)
  • OpenCV => 3.4.0 (but also affects earlier and later versions)
  • Operating System / Platform => Ubuntu 16.04
  • Compiler => g++ 5.4.0
Detailed description

cv::fisheye::distortPoints, according to its documentation, can be used to distort undistorted points of an image using the fisheye model.

I am trying to find any further documentation on how to use this function to deliberately distort an image, i.e., to do the opposite of cv::fisheye::undistortImage. However, all attempts to get a distorted version of an image failed, even those with an all-zero distortion vector. Since there are no examples or further documentation, I suspect that this is either a bug or needs an example/more documentation.

Steps to reproduce

I wrote this program to try and get a slightly distorted version of the input image by passing a list of all pixel positions to distortPoints and then remapping them:

#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 }; //Even an all-zero vector yields an extremely distorted image
  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;
}

All my attempts lead to an extremely distorted image like this, even with an all-zero distortion vector:

15035778566029332

Note: I asked this question more than half a year ago at answers.opencv.org, but did even not get a single comment, let alone an answer.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions