Clearly separate perspective and radial distortion. What am I doing wrong ?
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_linesfrom an image -
with the help of
generate_source_target_perspective_points()andcalc_perspective_coefficients(), I am able to get 8 coefficients. With an additional 1, I make it a 3x3 homography matrixHsuited forcv2.warpPerspective()/cv2.perspectiveTransform() -
by analyzing how the corners of the input image are projected by
cv2.perspectiveTransform(), I can even add a translation toHso 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 samelist_hor_lines/list_ver_linesto 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_linesfrom the original image (not perspective warped) - I transform the points in
list_hor_lines/list_ver_linesby H, let's name themlist_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 byH, should match the COD fromlist_hor_lines_H/list_ver_lines_H. In practice there can be differences, and I am not sure why.
Did I miss something ?
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.
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.
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.
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).
I close the ticket for now. Feel free to reopen or create a new one if you find something new.