In a perfect world… well, we don’t live in a perfect world. Errors will always creep in. Fortunately, the OpenCV software library has built-in routines for correcting these errors. In this post, we take a look at 2 types of distortion and how to correct for them.
Cameras and lenses have intrinsic distortion. Usually, the distortion is slight and not apparent in a casual view of the image. In some cases, like with a wide angle or fisheye lens, the distortion is quite obvious. For some computer vision tasks, this error is unacceptable and must be corrected
For calibration, OpenCV uses a reference checkboard pattern.
By measuring the distance between the squares in the calibration target across dozens of images in the field of view, OpenCV can detect and measure the distortion. From these measurements, a correction factor is generated that can then be applied to images to remove the distortion.
Camera/Lens distortion is pose independent. Once calibrated, the camera can be moved without impacting the calibration, as long as the lens is not adjusted on the camera.
Ideally, the plane of the camera sensor and the plane of the object being imaged are parallel. Regardless of how perfectly you attempt aligning the camera physically, you will have errors. In this example where the object is 11 feet away, slight errors in alignment are magnified in the image.
In this setup, Aruco markers were placed in the scene to define the region of interest, a rectangle 16 x 6 feet. OpenCV was used to locate the coordinates of the markers. These coordinates, along with OpenCV’s PerspectiveTransform function were used to correct for the camera’s tilt.
Obviously, this correction is pose dependent. If the camera is moved, tilt recalibration is required.
Computer Vision and Machine Learning have never been more accessible to small businesses. If you’re considering a computer vision project for your business, we’d love to hear about it. Maybe we can design something incredible together.
Not all computer vision tasks need to worry about image distortion. If you’re counting widgets, and your setup is reasonable, the distortion won’t impact your final results. If your computer vision project does require accuracy, like measuring widgets rather than counting them, OpenCV can help deal with the imperfections of reality.
Credit Where Credit is Due
I’ve not covered the specific Python code used for calibration. We’ve pulled together ideas from around the web to make this project possible. In this article, I’ve attempted to explain the problem, and in general, explain how it’s resolved. For a more in-depth discussion and code examples see the links below.
Without the published Python code of Adrian Rosebrock (https://www.pyimagesearch.com) and OpenCV (https://docs.opencv.org/4.1.0/dc/dbb/tutorial_py_calibration.html) several days work would have easily been several weeks of work.
Adrian’s website has probably hundreds of tutorials and Python code on OpenCV and Machine Learning and is highly recommended.