I have the following image which is computer generated
It is fed as an input to an optical experiment results in the following image:
As you can tell the image has a double concave effect due to the lens system being used.
I need to be able to restore the image without distortion and compare it with the original image. I'm new to image processing and I came across two useful python packages:
https://pypi.org/project/defisheye/
The defisheye was quite straight forward for me to use (script below) but i'm not able to achieve the optimal result so far.
from defisheye import Defisheye
dtype = 'linear'
format = 'fullframe'
fov = 11
pfov = 10
img = "input_distorted.jpg"
img_out = "input_distorted_corrected.jpg"
obj = Defisheye(img, dtype=dtype, format=format, fov=fov, pfov=pfov)
# To save image locally
obj.convert(outfile=img_out)
Seconly from opencv: https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html The camera calibration tutorial is way out of my knowledge. If someone could assure me if that's the way to go i can start digging in deeper. Really appreciate any suggestions.












If you have access to camera simply calibrate it but if you have no choice, the best way is using a software to do it manually. here I used GIMP to remove the lens effect.
If you have to do it pragmatically you have 2 ways to do it.
First: you can automate GIMP and write a script to do the thing.
Second: find the best coefficients for your camera in OpenCV. here I wrote a script that helps you to find coefficients by test and trial.
this code draws the biggest contour in the image and shows the image before and after undistortion. you have to find the best values to make the contour look like a rectangle.
You just need to change focal length, optical center, k1, k2, k3, p1 and p2 to reach the best result.
When you find the best values, use them to make your final image.
this is the output that I got with the current coefficients.