Skip to content

Incorrect pixel grid generation in icvGetRectangles #23304

@bhavitp

Description

@bhavitp

System Information

OpenCV version: 4.2.0
Operating System / Platform: Ubuntu 20.04
Compiler & compiler version: GCC 9.3.0

Detailed description

icvGetRectangles estimates the inscribed (inner) and circumscribed (outer) rectangles for an undistorted image by generating a uniform grid of pixel coordinates and undistorting each coordinate. However, the grid includes a row and column of pixels outside of the image. Here it considers the maximum horizontal and vertical pixel coordinates to be imgSize.width and imgSize.height, respectively, but they should be imgSize.width-1 and imgSize.height-1.

This method is used in getOptimalNewCameraMatrix and stereoRectify so fixing it will slightly improve undistortion and rectification.

I discovered this when using getOptimalNewCameraMatrix: passing an ideal camera matrix with zero distortion returns a slightly different camera matrix (see below).

Steps to reproduce

    // Simple camera matrix with no distortion
    const cv::Size img_size = {101, 101};
    const cv::Matx33d K = {1, 0, 50, 0, 1, 50, 0, 0, 1};
    const auto dist_coeffs = cv::Matx14d::zeros();

    const bool center_principal_point = false;
    const double alpha = 0;
    cv::Rect roi;

    // K_new should be the same as K
    const cv::Matx33d K_new = cv::getOptimalNewCameraMatrix(K, dist_coeffs, img_size, alpha, img_size, &roi, center_principal_point)

The new 'optimal' camera matrix should be the same as the original but it is not:

Optimal new K:
[0.9900990128517151, 0, 49.50495064258575;
 0, 0.9900990128517151, 49.50495064258575;
 0, 0, 1]
ROI:
[100 x 100 from (0, 0)]

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions