bluthen / fish2pano

Convert fisheye image to panoramic
Creative Commons Zero v1.0 Universal
7 stars 1 forks source link

OpenCV equivalent to fish2pano #4

Open aaronwmorris opened 1 week ago

aaronwmorris commented 1 week ago

I really appreciate this module, but I have discovered that OpenCV actually provides a transformation that is able to produce the exact output as the fish2pano module. The OpenCV transformation is 50-100% faster depending on the scale of the output.

The caveat is the panorama output is reversed and in a different orientation. Once you apply the rotation and flip actions, the final execution time is only ~30% slower.

One interesting aspect of the OpenCV implementation is it also allows you to reverse the transformation, ie you can convert a panorama to a fisheye perspective.

https://docs.opencv.org/4.x/da/d54/group__imgproc__transform.html#ga49481ab24fdaa0ffa4d3e63d14c0d5e4

The following code will produce the same image using both workflows:

import math
import cv2
import fish2pano

image = cv2.imread('image.jpg', cv2.IMREAD_UNCHANGED)

width, height = image.shape[:2]
center_x = int(width / 2)
center_y = int(height / 2)
r = int(height / 2)

# fish2pano
x = fish2pano.fish2pano(image, r, [center_y, center_x], 1.0)

cv2.imwrite('fish2pano.jpg', x)

# opencv
y1 = cv2.warpPolar(image, (r, int(2 * math.pi * r)), (center_y, center_x), r, cv2.WARP_POLAR_LINEAR)
y2 = cv2.rotate(y1, cv2.ROTATE_90_CLOCKWISE)
y = cv2.flip(y2, 1)

cv2.imwrite('cv2_warpPolar.jpg', y)
bluthen commented 1 week ago

That is awesome, I thought there might be a way, but didn't know about warpPolar. One less dependency!