Skip to content

ROI returned by cvGetOptimalNewCameraMatrix has wrong width & height #24831

@devernay

Description

@devernay

System Information

OpenCV python version: 4.8.0.76
Operating System / Platform: macOS
Python version: 3.10.6

Detailed description

I noticed here that cvGetOptimalNewCameraMatrix returns (0,0,width-1,height-1) when the distortion parameters are zero.

Looking more closely at the code, I see that the internal function icvGetRectangles finds the rectangles by sampling points in the (0,width-1)x(0,hieght-1) range, and then computes the output ROI sizes as (maxX-minX,maxY-minY), which of course misses one pixel in each dimension!

I think the code in icvGetRectangles should add 1 pixel to both dimensions, because the max value should be included.

Steps to reproduce

Test code:

import cv2
import numpy as np
# zero distortion
distortion_params = np.zeros(8)
width=1920
height=1080
ffactor=1.6
# intrinsics with a centered principal point
K1 = np.array([[width*ffactor, 0, (width-1)*0.5],
              [0,width*ffactor, (height-1)*0.5],
              [0,0,1]])
# intrinsics with a non-centered principal point
K2 = np.array([[width*ffactor, 0, (width-1)*0.5+0.05],
              [0,width*ffactor, (height-1)*0.5-0.04],
              [0,0,1]])
for K in [K1,K2]:
    newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (width, height), 0)
    print(np.linalg.norm(K-newK))
    print(roi)

Output:

4.547473508864641e-13
(0, 0, 1919, 1079)
4.547473508864641e-13
(0, 0, 1919, 1079)

both rois should be (0, 0, 1920, 1080).

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

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions