I'm making a custom virtual reality headset with code in python / opencv. I need to be able to distort the images to create the "barrel distortion" / "radial distortion" effect.
Some images to explain:
I already have the source_image
that I want to use and show to the user, and already have them side-by-side. Now I just need something like out = cv2.createBarrelDistortion(source_image, params)
. (and I wouldn't mind being able to tune a few params like center of distortion, magnitude of distortion, etc, so I can make it look right for whatever custom lenses I get.)
Any help much appreciated!
Here is how to do that in Python Wand 0.5.9
(http://docs.wand-py.org/en/0.5.9/index.html)
Input:
from wand.image import Image
import numpy as np
import cv2
with Image(filename='checks.png') as img:
print(img.size)
img.virtual_pixel = 'transparent'
img.distort('barrel', (0.2, 0.0, 0.0, 1.0))
img.save(filename='checks_barrel.png')
# convert to opencv/numpy array format
img_opencv = np.array(img)
# display result with opencv
cv2.imshow("BARREL", img_opencv)
cv2.waitKey(0)
Result:
See https://imagemagick.org/Usage/distorts/#barrel for the same example and discussion of arguments.
See https://hackaday.io/project/12384-autofan-automated-control-of-air-flow/log/41862-correcting-for-lens-distortions for Python/OpenCV approach.