2

I try to determine the distortion of an imaging system. Due to technical reasons, I can only use a grid target. Sample Image of a grid target

I follow the method using cv2 as described here: https://opencv24-python-tutorials.readthedocs.io/en/stable/py_tutorials/py_calib3d/py_calibration/py_calibration.html

Using cv2.findChessboardCorners() does not work. Fair enough, I do not have a checkerboard. BUT I determined the grid points "manually", which works pretty well. Grid points are shown in orange

However, following the code example from cv2, I get this:

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera([objp], [corners], img.shape[::-1], None, None)

with

ret = 403.18434311612924 

mtx = [[2.15648501e+04 0.00000000e+00 6.60463129e+02]
      [0.00000000e+00 4.83041078e+05 5.08963085e+02]
      [0.00000000e+00 0.00000000e+00 1.00000000e+00]] 

dist = [[ 2.25904915e+03  6.07202692e-03  2.83113951e+00 -9.49974797e+00  1.69976610e-08]] 

rvecs = (array([[-0.59541192],
        [-1.49725611],
        [-0.56402402]]),) 

tvecs = (array([[ 1.2836315e+01],
        [-1.8945857e+01],
        [ 1.7703286e+04]]),)

Then I try to cv2.undistort() and get a very strange image Undistortion gone wrong

What's wrong here? I do not get any errors.

I tried to artificially distort the image using gimp. Maybe the distortion in the original image is too low to work with. However, the results are also not satisfactory.

beaker
  • 16,331
  • 3
  • 32
  • 49
kai90
  • 21
  • 5
  • Check the result parameter values based on the meaning of them, before using them to undistortion. For example, (cx,cy, fx, fy) is most checkable one. – fana Mar 28 '23 at 01:33
  • 1
    In your `mtx`, fx and fy are very different. Does this match your expectations? If not, your calibration process failed. – fana Mar 28 '23 at 02:15
  • Thank you very much. Yes, I am pretty sure that either the calibration process failed or the undistort function does something unexpected. However, apart from evaluating my grid points, I don't know how to make the calibration process work. Any ideas? – kai90 Mar 28 '23 at 07:24
  • At first, it is necessary to make sure that the main cause of bad calibration result parameter value is not mistake in input data. Confirm your input to calibration carefully and carefully in comparison with specification of the calibration function. – fana Mar 29 '23 at 01:00
  • 1
    Second, consider whether you can reduce the degree of freedom(:number of intrinsic parameters to estimate) to avoid the instability of optimization. When your image has only few distortion, few number of distortion parameter may be enough. Or if you know the aspect ration is enoughly 1.0, doing the calibration under the constraint `fx==fy` is good way. – fana Mar 29 '23 at 01:07
  • And/Or, consider to provide most possible good initial parameter value to optimizer to reduce the possibility of obviously bad results. – fana Mar 29 '23 at 01:13
  • @fana: Thanks for the advice. I tried that out by setting some flags. It does not work. I verified my code using a standard grid pattern I found on google images. The code there works perfectly fine. – kai90 Apr 13 '23 at 09:59
  • I also tried now to reduce the number of grid points. My thinking: Maybe the cv2 can't handle too much grid points correctly. That wasn't the problem either. – kai90 Apr 13 '23 at 10:00

1 Answers1

0

The problem is, that the image is too large. I wondered why the images I took from the internet were working and the image from the camera did not. After some playing around, I found that only difference were the sizes of the files.

I cropped my original image and then the code worked fine. I added some artificial distortion using gimp and this also worked fine with the cropped image.

TL;DR: CV2 does not like high pixel count images!

Is it a bug in cv2? Maybe... I will try to report this one.

kai90
  • 21
  • 5