discorpy icon indicating copy to clipboard operation
discorpy copied to clipboard

Clearly separate perspective and radial distortion. What am I doing wrong ?

Open PierreChatelier opened this issue 1 year ago • 5 comments

I am investigating if I can use discorpy to create correction models (for perspective and radial distortion) and apply the correction with OpenCV. I successfully handled both problems independently, but I have a hard time combining them. I wonder if I misuse something.

[the context]

  • I get list_hor_lines/list_ver_lines from an image

  • with the help of generate_source_target_perspective_points() and calc_perspective_coefficients(), I am able to get 8 coefficients. With an additional 1, I make it a 3x3 homography matrix H suited for cv2.warpPerspective()/cv2.perspectiveTransform()

  • by analyzing how the corners of the input image are projected by cv2.perspectiveTransform(), I can even add a translation to H so that I am able to warp the full image into a full image space (usually larger than the original input)

  • with find_cod_coarse()/calc_coef_forward()/calc_coef_backward(), I can use the same list_hor_lines/list_ver_lines to get a COD and a polynomial model for the radial distortion (I don't need the image there)

  • this model is easily converted into a mapping for cv2.remap()

  • in the same way as I did for the perspective, I can tune the remapping to get the full image space and not a cropped view.

[what I think should work] I assume that the perpsective homography H could have been computed by another mean, and I want to perform a radial distortion analysis with discorpy. I think the following scenario should be correct :

  • I get list_hor_lines/list_ver_lines from the original image (not perspective warped)
  • I transform the points in list_hor_lines/list_ver_lines by H, let's name them list_hor_lines_H/list_ver_lines_H
  • with those updated list_hor_lines_H/list_ver_lines_H, I compute the radial distortion as above (I don't need the image there)
  • I apply that distortion, not to the original image, but to the perspective warped image

[what I observe]

  • In practice, it doesn't really work. The remapped image is usually not centered at all in the dst space, as if the remap was not the good one.
  • I have observed something strange with the COD. I though that the COD from list_hor_lines/list_ver_lines , then transformed by H, should match the COD from list_hor_lines_H/list_ver_lines_H. In practice there can be differences, and I am not sure why.

Did I miss something ?

PierreChatelier avatar Oct 08 '25 08:10 PierreChatelier

Hi, I got the idea of your question, but it would be good to have some background. Have you checked the latest documentation and API on how to correct strong perspective distortion and radial distortion? https://discorpy.readthedocs.io/en/latest/technical_notes/fisheye_correction.html

For using cv2.remap while keeping the original view, we wrote a demo here: https://discorpy.readthedocs.io/en/latest/usage/tips.html

and a wrapper here: https://discorpy.readthedocs.io/en/latest/api/discorpy.util.utility.html#discorpy.util.utility.mapping_cv2

I'll have a go at performing perspective correction in OpenCV to reproduce the issue you observed. But one thing I can already see is incorrect: you’re applying radial distortion to the perspective-corrected image instead of the original one. You have to apply radial distortion to the original image first, and then apply perspective distortion.

Note that perspective distortion can vary depending on the target angle, while radial distortion is a lens-fixed parameter.

nghia-vo avatar Oct 08 '25 18:10 nghia-vo

You have to apply radial distortion to the original image first, and then apply perspective distortion.

That's very interesting. I assumed that radial distortion had to be done on a rectified image, because using a COD and a radius to apply the polynomial model implies that the image has no perspective.

I'll re-read the docs.

PierreChatelier avatar Oct 09 '25 06:10 PierreChatelier

Note that the main purpose of Discorpy is to calibrate radial distortion. However, in reality, there are also perspective effects and offset COD, so Discorpy provides methods to calculate the COD and correct for the perspective effect before applying radial distortion correction. The radial coefficients refer to the original image, not to the perspective-corrected image (this is the main purpose of radial lens calibration).

Let’s say you have an image with both perspective distortion and radial distortion. You can use different methods to obtain the perspective coefficients (note that this set of parameters is not unique, different sets of parameters can produce similar results). You might assume that you can use Discorpy on the perspective-corrected image to calculate radial distortion and expect it to work. However, this will not work as expected, because the way Discorpy calculates perspective coefficients may differ from the methods you used (as mentioned, perspective coefficients are not unique). As a result, you may end up translating the original image during perspective correction, which changes the COD. Discorpy’s methods, on the other hand, correct the perspective effect while ensuring that the COD does not change.

nghia-vo avatar Oct 10 '25 14:10 nghia-vo

I come back to acknowledge my stupidity ! In order to test perspective+radial distortion, I just peeked such an image from your docs and applied an exaggerated perspective effect with an image editor. But this is plain wrong ! In the real world, the lens is deforming the slanted planed, and I just stupidely created the opposite. Thats explains why I did not understand how your model, which is radial-isotropic, would work due to my added perspective effect that was defeating it. Of course you are right and the radial undistortion must be applied first.

I still let the issue open to conclude after some more tests (correct, this time).

PierreChatelier avatar Oct 11 '25 10:10 PierreChatelier

I close the ticket for now. Feel free to reopen or create a new one if you find something new.

nghia-vo avatar Nov 06 '25 16:11 nghia-vo