Bad results when undistorting points using OpenCV in Python

2024/9/30 23:28:03

I'm having trouble undistorting points on an image taken with a calibrated camera using the Python bindings for OpenCV. The undistorted points have entirely different coordinates than the original points detected in the image.

Here's the offending call:

undistorted = cv2.undistortPoints(image_points,camera_matrix,distortion_coefficients)

where image_points is a numpy array of detected chessboard corners returned by cv2.findChessboardCorners and reshaped to match the dimensional requirements of cv2.undistortPoints, and camera_matrix and distortion_coefficients were returned by cv2.calibrateCamera.

camera_matrix and distortion_coefficients seem to me to be okay, and so do image_points. Nevertheless, distorted seems to have no relationship to image_points. Here's a summary of the values:

>>> image_points
array([[[ 186.95303345,  163.25502014]],[[ 209.54478455,  164.62690735]],[[ 232.26443481,  166.10734558]],..., [[ 339.03695679,  385.97784424]],[[ 339.20108032,  400.38635254]],[[ 339.13067627,  415.30780029]]], dtype=float32)
>>> undistorted
array([[[-0.19536583, -0.07900728]],[[-0.16608481, -0.0772614 ]],[[-0.13660771, -0.07537176]],..., [[ 0.00228534,  0.21044853]],[[ 0.00249786,  0.22910291]],[[ 0.00240568,  0.24841554]]], dtype=float32)
>>> camera_matrix
array([[ 767.56947802,    0.        ,  337.27849576],[   0.        ,  767.56947802,  224.04766824],[   0.        ,    0.        ,    1.        ]])
>>> distortion_coefficients
array([[ 0.06993424, -0.32645465,  0.        ,  0.        , -0.04310827]])

I'm working with reference C code and everything matches up until I make that call. What's going wrong?

Answer

I think you forgot to specify the new camera matrix in your call to undistortPoints. If you look at the documentation of the function, it says that the signature is:

Python: cv.UndistortPoints(src, dst, cameraMatrix, distCoeffs, R=None, P=None) → None

where dst is the array of points after undistortion and "if P is identity or omitted, then it contains normalized point coordinates", meaning before projection in the image using the calibration matrix.

The function should do what you expect if you set P to your cameraMatrix.

https://en.xdnf.cn/q/71027.html

Related Q&A

Pandas - Groupby and create new DataFrame?

This is my situation - In[1]: data Out[1]: Item Type 0 Orange Edible, Fruit 1 Banana Edible, Fruit 2 Tomato Edible, Vegetable 3 Laptop Non Edible, Elec…

Can pytest fixtures and confest.py modules be shared across packages?

Suppose I have packageA which provides a class usefulClass, pytest fixtures in a test_stuff.py module, and test configurations in a conftest.py module. Moreover, suppose that I have packageBand package…

Sort a list by presence of items in another list

Suppose I have two lists:a = [30, 10, 90, 1111, 17] b = [60, 1201, 30, 17, 900]How would you sort this most efficiently, such that:list b is sorted with respect to a. Unique elements in b should be pla…

Sample from a multivariate t distribution python

I am wondering if there is a function for sampling from a multivariate student t-distribution in Python. I have the mean vector with 14 elements, the 14x14 covariance matrix and the degrees of freedom …

Why is this Jinja nl2br filter escaping brs but not ps?

I am attempting to implement this Jinja nl2br filter. It is working correctly except that the <br>s it adds are being escaped. This is weird to me because the <p>s are not being escaped and…

select certain monitor for going fullscreen with gtk

I intend to change the monitor where I show a fullscreen window. This is especially interesting when having a projector hooked up.Ive tried to use fullscreen_on_monitor but that doesnt produce any visi…

Load Excel add-in using win32com from Python

Ive seen from various questions on here that if an instance of Excel is opened from Python using:xl = win32com.client.gencache.EnsureDispatch(Excel.Application) xl.Visible = True wb = xl.Workbooks.Open…

iterating through a list removing items, some items are not removed

Im trying to transfer the contents of one list to another, but its not working and I dont know why not. My code looks like this:list1 = [1, 2, 3, 4, 5, 6] list2 = []for item in list1:list2.append(item)…

Apply function to create string with multiple columns as argument

I have a dataframe like this:name . size . type . av_size_type 0 John . 23 . Qapra . 22 1 Dan . 21 . nukneH . 12 2 Monica . 12 . kahless . 15I wa…

Popping items from a list using a loop in Python [duplicate]

This question already has answers here:Strange result when removing item from a list while iterating over it in Python(12 answers)Closed 3 months ago.Im trying to write a for loop in python to pop out …