Skip to content
The Chat Archive
qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K

numpy if I have a half equirectangular image how do I create a fish eye image? so…

  1. User

    numpy if I have a half equirectangular image how do I create a fish eye image?

    so examples are in (https://blog.mikeswanson.com/apples-mysterious-fisheye-projection/) not the apple propritery fisheye projection just where it talks about standard fisheye and half equirectuagular projection for just aa 180 degree hemesphere view

    I looked at the code in omnicv:

    def equirect2Fisheye(self, img, outShape, f=50, xi=1.2, angles=[0, 0, 0] ):

    self.Hd = outShape[0]
    self.Wd = outShape[1]
    self.f = f
    self.xi = xi
    
    Hs, Ws = img.shape[:2]
    
    self.Cx = self.Wd / 2.0
    self.Cy = self.Hd / 2.0
    
    x = np.linspace(0, self.Wd - 1, num=self.Wd, dtype=np.float32)
    y = np.linspace(0, self.Hd - 1, num=self.Hd, dtype=np.float32)
    
    x, y = np.meshgrid(range(self.Wd), range(self.Hd))
    xref = 1
    yref = 1
    
    self.fmin = (
        np.sqrt(
            -(1 - self.xi ** 2) * ((xref - self.Cx) ** 2 +
                                   (yref - self.Cy) ** 2)
        )
        * 1.0001
    )
    
    x_hat = (x - self.Cx) / self.f
    y_hat = (y - self.Cy) / self.f
    
    x2_y2_hat = x_hat ** 2 + y_hat ** 2
    
    omega = np.real(
        self.xi + np.lib.scimath.sqrt(1 + (1 - self.xi ** 2) * x2_y2_hat)
    ) / (x2_y2_hat + 1)
    # print(np.max(x2_y2_hat))
    
    Ps_x = omega * x_hat
    Ps_y = omega * y_hat
    Ps_z = omega - self.xi
    
    self.alpha = angles[0]
    self.beta = angles[1]
    self.gamma = angles[2]
    
    R = np.matmul(
        rmat(self.alpha, self.beta, self.gamma),
        np.matmul(rmat(0, -90, 45), rmat(0, 90, 90)),
    )
    
    Ps = np.stack((Ps_x, Ps_y, Ps_z), -1)
    Ps = np.matmul(Ps, R.T)
    
    Ps_x, Ps_y, Ps_z = np.split(Ps, 3, axis=-1)
    Ps_x = Ps_x[:, :, 0]
    Ps_y = Ps_y[:, :, 0]
    Ps_z = Ps_z[:, :, 0]
    
    theta = np.arctan2(Ps_y, Ps_x)
    phi = np.arctan2(Ps_z, np.sqrt(Ps_x ** 2 + Ps_y ** 2))
    
    a = 2 * np.pi / (Ws - 1)
    b = np.pi - a * (Ws - 1)
    self.map_x = (1.0 / a) * (theta - b)
    
    a = -np.pi / (Hs - 1)
    b = np.pi / 2
    self.map_y = (1.0 / a) * (phi - b)
    
    output = cv2.remap(
        img,
        self.map_x.astype(np.float32),
        self.map_y.astype(np.float32),
        cv2.INTER_CUBIC,
        borderMode=cv2.BORDER_WRAP,
    )
    
    if self.f < self.fmin:
        r = np.sqrt(-(self.f ** 2) / (1 - self.xi ** 2))
        mask = np.zeros_like(output[:, :, 0])
        mask = cv2.circle(
            mask, (int(self.Cx), int(self.Cy)), int(r), (255, 255, 255), -1
        )
        output = cv2.bitwise_and(output, output, mask=mask)
    
    return output
    

    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.

  2. Assistant

    You're right in wanting to convert a half equirectangular image (which shows a 180°×180° hemisphere) into a fisheye image, also covering a 180° field of view. The process involves remapping pixels from the spherical coordinate system of the equirectangular image to the fisheye projection model.

    To keep things simple, we'll avoid camera parameters like xi and f for now and focus on the basic geometric transformation from equirectangular to fisheye, assuming both images are of the same size (e.g., 1600×1600).


    🧭 Understanding the Projections

    1. Equirectangular (half):

    • Covers a hemisphere: 180° horizontal (azimuth) × 90° vertical (elevation).
    • Pixel coordinates map directly to spherical coordinates:
      • θ (theta) = longitude = x / width * 2π → but for half, it's x / width * π
      • φ (phi) = latitude = y / height * π/2 → since it's half sphere

    2. Fisheye (stereographic):

    • A circular image showing a 180° FOV.
    • Each pixel maps to a direction in the hemisphere.
    • Fisheye uses polar coordinates:
      • Radius r = 2 * f * tan(θ / 2)
      • Where θ is the angle from the optical axis (zenith), and f is focal length (we'll normalize it to image size)

    ✅ Goal

    Given:

    • Input: hequirect of shape (1600, 1600, 3) representing a 180°×90° half equirectangular image.
    • Output: Same shape, but as a fisheye image of a 180° FOV (circular).

    🧮 Approach

    We'll:

    1. Generate a grid of output pixel coordinates.
    2. Convert them to spherical directions (θ, φ) for a fisheye projection.
    3. Map those directions to input equirectangular image coordinates.
    4. Use cv2.remap to warp the image.

    ✅ Simple Implementation

    Here's a minimal and well-documented implementation:

    import numpy as np
    import cv2
    
    def equirect_to_fisheye_simple(img):
        H, W = img.shape[:2]
        assert H == W, "Image must be square"
        
        # Output image size
        out = np.zeros_like(img)
    
        # Create coordinate grids
        x = np.arange(W)
        y = np.arange(H)
        x, y = np.meshgrid(x, y)
    
        # Normalize coordinates to [-1, 1]
        x = (x - W / 2) / (W / 2)
        y = (y - H / 2) / (H / 2)
    
        # Compute radius and angle in fisheye space
        r = np.sqrt(x**2 + y**2)
        theta = np.arctan2(y, x)
    
        # Mask for circular fisheye (discard pixels outside the circle)
        mask = (r <= 1.0)
    
        # Map fisheye radius to zenith angle θ (0 to π/2 for 180° FOV)
        zenith_angle = np.pi * r / 2
        zenith_angle = np.clip(zenith_angle, 0, np.pi / 2)
    
        # Spherical coordinates: θ (longitude), φ (latitude)
        phi = zenith_angle
        theta = theta
    
        # Convert to equirectangular coordinates
        # Equirectangular: θ ∈ [0, π] (horizontal), φ ∈ [0, π/2] (vertical)
        eq_x = (theta + np.pi) % (2 * np.pi) * (W / (2 * np.pi))
        eq_y = phi * (H / np.pi)
    
        # Remap using OpenCV
        remap_x = eq_x.astype(np.float32)
        remap_y = eq_y.astype(np.float32)
    
        out = cv2.remap(img, remap_x, remap_y, cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
    
        # Apply circular mask
        center = (W // 2, H // 2)
        radius = W // 2
        mask = np.zeros((H, W), dtype=np.uint8)
        cv2.circle(mask, center, radius, (255, 255, 255), -1)
        out = cv2.bitwise_and(out, out, mask=mask)
    
        return out
    

    🧪 Usage Example

    import cv2
    
    # Load half equirectangular image (1600x1600)
    hequirect = cv2.imread("half_equirect.jpg")
    
    # Convert to fisheye
    fisheye_img = equirect_to_fisheye_simple(hequirect)
    
    # Save or display
    cv2.imwrite("fisheye_output.jpg", fisheye_img)
    

    🔍 Notes

    • This assumes the fisheye uses stereographic projection (linear with tan(θ/2)), but simplified.
    • The output is circular and fits in the same square image.
    • It uses cv2.remap, which is fast and accurate.
    • If your equirectangular image covers the full 360°×180°, you can adjust the mapping range accordingly.

    🧠 Want More Control?

    If you want to:

    • Adjust FOV
    • Use different fisheye models (equidistant, orthographic, stereographic)
    • Rotate the view

    Then you can extend this with:

    • More complex spherical coordinate transformations
    • Rotation matrices (like in the OmniCV code)
    • More accurate fisheye equations

    Let me know if you'd like to expand on this!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.