Camera distortion

142 views
Skip to first unread message

Dorian Battivelli

unread,
Oct 15, 2020, 6:23:58 AM10/15/20
to Bonsai Users
Dear Bonsai users,

I tried to correct the fisheye distortion, using the python_opencv_calibration.py script (as explained here https://medium.com/@aliyasineser/opencv-camera-calibration-e9a48bdd1844). I used a 6.9 chessboard and took 100 pictures.
I entered the values obtained in that way in the "undistort" node, but it does not work, the picture is still distorted and significantly cropped.

Then I tried to build than Nils, but I have an error message when I click run:
I am actually not sur to understand how to use the nodes "findchessbordcorners" and "calibratecamerachessboard" can you please explain me how to set them? Or how to create a functional workflow that allow me to correct live the distortion during video acquisition?


Thank you very much,
Best,
Dorian


Dorian Battivelli

unread,
Oct 15, 2020, 6:25:50 AM10/15/20
to Bonsai Users
On the first screenshot, you can visualize the values calculated by Opencv, and the bonsai workflow I use.

On the second you can see the second workflow inspired by Nils.
image1.png
image2.png

nils.n...@gmail.com

unread,
Oct 15, 2020, 6:58:24 AM10/15/20
to Bonsai Users
Hi Dorian!

I basically got it to work by having my whole checkerboard/chessboard fill up the camera image (i.e., I had to put my printed checkerboard very close to the lens of the camera). Once this is done, the "Calibrate Camera Chessboard" node will continuously calculate different undistortion parameters (each row of text is one calculation), and these have different projection error scores, which are seen on the far right. I usually let it run until the whole screen is filled up with text, and then I pick the row associated with the least projection error score. You can then put these parameters into an "Undistort" node, which should undistort your video in real-time. 

To get this error score to go down, you really need to put the checkerboard as close to the camera as possible - I managed to get the projection error down to ~2, while having the checkerboard just a few cm lower down while make the projection error increase to ~500 or more for me. For me, It was also even fine if some of the outer checkerboard corners was not in the frame, I guess because only the internal corners are counted; i.e., I have a 10x7 checkerboard pattern, but in the 'PatternSize' property of the "Find Chessboard Corners" and 'Calibrate Camera Chessboard' nodes I put in 9 for width and 6 for height, since those are the number of internal corners. Likewise, in the 'CheckerSize' property of the  'Calibrate Camera Chessboard' node I put in 0.1, since my checkerboards have a size of 10 cm (since I assumed this node is using the SI unit of length, which is the meter, but perhaps someone can correct me if this is not the metric used).

I got the same error message as you showed when the "Find Chessboard Corners" node couldn't find all the corners. This was either due to the checkerboard either being placed too far away from the camera, due to the wrong parameters in the node, or due to the illumination not being uniform. The latter was usually the cause of my problems, and I had to put a piece of paper over some of my lights to make the illumination uniform, after which the node would correctly find all the internal corners.

Let me know if you get it to work, otherwise I'm happy to help more if I can. If you still don't get it to work, do feel free to send a photo of how you are positioning the checkerboard in the camera frame, what the size is of each checkerboard square, and what parameters you are using in the "Find Chessboard Corners" and the "Calibrate Camera Chessboard" nodes.

Dorian Battivelli

unread,
Oct 15, 2020, 8:22:32 AM10/15/20
to Bonsai Users
Hi Nils,

Thank so much for your help, I have been able to go ahead and to get a pretty good correction.
I have still 2 issues:
1) You talk about  'CheckerSize' property of the  'Calibrate Camera Chessboard' nod, but I don't see this parameter (see on the screenshot attached to my answer)
2) As you can see on my screenshot, the field that is corrected is very small compare to the original image captured by the camera. How can I increase the frame within which the correction applies?

Thank you!
Untitled.png

nils.n...@gmail.com

unread,
Oct 15, 2020, 10:19:42 AM10/15/20
to Bonsai Users
1. Sorry I meant the 'PatternSize' and you seem to have that inputted correctly already.
2. Yes you will get the distortion around the edges with some of the parameters, this can be fixed by testing different outputted parameters until you find one where this is not present. You can also place the checkerboard even closer to the camera than you currently have it, and get new parameters and test these. Those two things eventually solved it for me.

Best,
Nils

Dorian Battivelli

unread,
Oct 16, 2020, 7:44:41 AM10/16/20
to Bonsai Users
Hi Nils, 
thank you for the answer! 

 I finally used MATLAB to correct the distortion: 

I worked very well and without that much cropping issue, 
Thanks again, 
Best,
Dorian 

nils.n...@gmail.com

unread,
Oct 16, 2020, 8:14:48 AM10/16/20
to Bonsai Users
Great! Glad you got it to work.

Best,
Nils

Reply all
Reply to author
Forward
0 new messages