but it does not seem to be for half equiretangular I think and it seems like you need to put in camera parameters?
but its not well documented so I cant tell.
so do you know how I would just do a simple function that just deals with the case of a square equirectangular image so width == height representing a 180 degree view horozontaly and vertically and I want it to output a image of the same size that is a fish eye projection of 180 degrees so making it as simple as possible.
so do you know how I would just do a simple function that just deals with the case of a square equirectangular image so width == height representing a 180 degree view horozontaly and vertically and I want it to output a image of the same size that is a fish eye projection of 180 degrees so making it as simple as possible. I have loaded the half equirectagular image into a numpy array named at "hequirect" where hequirect has shape (1600, 1600, 3) and hequirect dtype is uint8.
Assistant
Here's a simple function that converts a half equirectangular image to a fisheye projection. This function assumes that the input image is a square, representing a 180 degree view horizontally and vertically.
python
import numpy as np
import cv2
def equirect2fisheye(hequirect):
"""
Convert a half equirectangular image to a fisheye projection.
Parameters:
hequirect (numpy array): The input half equirectangular image.
Returns:
fisheye (numpy array): The output fisheye projection image.
"""
height, width, _ = hequirect.shape
assert height == width, "Input image must be a square"
# Create a grid of coordinates for the output image
x = np.linspace(-1, 1, width)
y = np.linspace(-1, 1, height)
x, y = np.meshgrid(x, y)
# Calculate the radius of each point from the center
r = np.sqrt(x**2 + y**2)
# Calculate the angle of each point from the center
theta = np.arctan2(y, x)
# Calculate the corresponding equirectangular coordinates
lon = theta + np.pi
lat = np.arcsin(r)
# Convert the equirectangular coordinates to pixel coordinates
x_eq = (lon / (2 * np.pi)) * width
y_eq = (1 - lat / np.pi) * height
# Create the output image
fisheye = np.zeros_like(hequirect)
# Map the equirectangular image to the fisheye image
for i in range(height):
for j in range(width):
x_idx = int(np.round(x_eq[i, j]))
y_idx = int(np.round(y_eq[i, j]))
if 0 <= x_idx < width and 0 <= y_idx < height:
fisheye[i, j] = hequirect[y_idx, x_idx]
return fisheye
hequirect = np.random.randint(0, 256, size=(1600, 1600, 3), dtype=np.uint8) # Replace with your image
fisheye = equirect2fisheye(hequirect)
cv2.imshow('Fisheye', fisheye)
cv2.waitKey(0)
cv2.destroyAllWindows()
However, the above code does not use interpolation and may produce a pixelated image. To improve the quality of the output image, you can use interpolation to map the equirectangular image to the fisheye image. Here's an updated version of the function that uses interpolation:
python
import numpy as np
import cv2
def equirect2fisheye(hequirect):
"""
Convert a half equirectangular image to a fisheye projection.
Parameters:
hequirect (numpy array): The input half equirectangular image.
Returns:
fisheye (numpy array): The output fisheye projection image.
"""
height, width, _ = hequirect.shape
assert height == width, "Input image must be a square"
# Create a grid of coordinates for the output image
x = np.linspace(-1, 1, width)
y = np.linspace(-1, 1, height)
x, y = np.meshgrid(x, y)
# Calculate the radius of each point from the center
r = np.sqrt(x**2 + y**2)
# Calculate the angle of each point from the center
theta = np.arctan2(y, x)
# Calculate the corresponding equirectangular coordinates
lon = theta + np.pi
lat = np.arcsin(r)
# Convert the equirectangular coordinates to pixel coordinates
x_eq = (lon / (2 * np.pi)) * width
y_eq = (1 - lat / np.pi) * height
# Create the output image
fisheye = np.zeros_like(hequirect)
# Map the equirectangular image to the fisheye image using interpolation
for i in range(height):
for j in range(width):
x_idx = x_eq[i, j]
y_idx = y_eq[i, j]
if 0 <= x_idx < width and 0 <= y_idx < height:
x1 = int(np.floor(x_idx))
x2 = int(np.ceil(x_idx))
y1 = int(np.floor(y_idx))
y2 = int(np.ceil(y_idx))
dx = x_idx - x1
dy = y_idx - y1
if x2 < width and y2 < height:
q11 = hequirect[y1, x1]
q12 = hequirect[y2, x1]
q21 = hequirect[y1, x2]
q22 = hequirect[y2, x2]
fisheye[i, j] = (1 - dx) * (1 - dy) * q11 + dx * (1 - dy) * q21 + (1 - dx) * dy * q12 + dx * dy * q22
elif x2 < width:
q11 = hequirect[y1, x1]
q21 = hequirect[y1, x2]
fisheye[i, j] = (1 - dx) * q11 + dx * q21
elif y2 < height:
q11 = hequirect[y1, x1]
q12 = hequirect[y2, x1]
fisheye[i, j] = (1 - dy) * q11 + dy * q12
else:
fisheye[i, j] = hequirect[y1, x1]
return fisheye.astype(np.uint8)
hequirect = np.random.randint(0, 256, size=(1600, 1600, 3), dtype=np.uint8) # Replace with your image
fisheye = equirect2fisheye(hequirect)
cv2.imshow('Fisheye', fisheye)
cv2.waitKey(0)
cv2.destroyAllWindows()
This updated function uses bilinear interpolation to map the equirectangular image to the fisheye image, which should produce a smoother output image.