Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create this barrel / radial distortion with Python OpenCV?

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:

enter image description here

enter image description here

enter image description here

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!

like image 530
JDS Avatar asked Oct 16 '25 13:10

JDS


1 Answers

Here is how to do that in Python Wand 0.5.9

(http://docs.wand-py.org/en/0.5.9/index.html)


Input:

enter image description here

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:

enter image description here

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.

like image 54
fmw42 Avatar answered Oct 19 '25 08:10

fmw42