lensfunpy

repository·master·Indexed 41 days ago

https://github.com/letmaik/lensfunpy

A Python wrapper for the lensfun_ library providing tools to correct lens-induced artifacts, including geometric distortion, vignetting, and transverse chromatic aberration (TCA). It provides a Database class to access lens and camera profiles and a Modifier class to apply corrections to images.

Tokens
3.6K
Snippets
9
Records
22
Agent score
51%

What's inside lensfunpy

  1. Core classes in lensfunpy

    master

    The lensfunpy library is organized around several core classes that represent the components of the Lensfun database and the operations performed on images.

    • Database: The entry point for accessing the Lensfun database. It is used to search for cameras, lenses, and mounts.
    • Camera: Represents a specific camera model. It contains metadata such as the maker, model, and crop factor.
    • Mount: Represents a lens mount type (e.g., Nikon F AF).
    • Lens: Represents a specific lens model. It contains optical parameters like focal length, aperture, and type.
    • Modifier: Used to apply corrections to an image, such as Vignette or TCA (Transverse Chromatic Aberration) correction.
  2. Correct lens vignetting and TCA (Transverse Chromatic Aberration)

    master

    To correct both vignetting and TCA, you must follow a specific order: Vignetting must be corrected first.

    Steps:

    1. Initialize lensfunpy.Modifier with the flags parameter set to lensfunpy.ModifyFlags.VIGNETTING | lensfunpy.ModifyFlags.TCA.
    2. Call mod.apply_color_modification(img) to handle vignetting.
    3. Call mod.apply_subpixel_distortion() to get the subpixel distortion coordinates.
    4. Use cv2.remap on each color channel (R, G, B) separately using the corresponding channel from the distortion coordinates.

    Note: The input image must be in a linear state.

    import imageio
    import cv2
    import lensfunpy
    
    db = lensfunpy.Database()
    cam = db.find_cameras('Canon', 'Canon EOS 5D Mark IV')[0]
    lens = db.find_lenses(cam, 'Sigma', 'Sigma 8mm f/3.5 EX DG circular fisheye')[0]
    
    img = imageio.imread('/path/to/image.tiff')
    
    focal_length = 8.0
    aperture = 11
    distance = 10
    width = img.shape[1]
    height = img.shape[0]
    
    mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
    mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype, flags=lensfunpy.ModifyFlags.VIGNETTING | lensfunpy.ModifyFlags.TCA)
    
    # 1. Vignette Correction
    mod.apply_color_modification(img)
    
    # 2. TCA Correction
    undist_coords = mod.apply_subpixel_distortion()
    img[..., 0] = cv2.remap(img[..., 0], undist_coords[..., 0, :], None, cv2.INTER_LINEAR)
    img[..., 1] = cv2.remap(img[..., 1], undist_coords[..., 1, :], None, cv2.INTER_LINEAR)
    img[..., 2] = cv2.remap(img[..., 2], undist_coords[..., 2, :], None, cv2.INTER_LINEAR)
    
    imageio.imwrite('/path/to/image_corrected.tiff', img)
  3. Correct lens distortion

    master

    To correct geometric distortion, use lensfunpy.Modifier. This process typically involves generating undistorted coordinates and remapping the image using a library like OpenCV.

    Steps:

    1. Create a lensfunpy.Modifier instance with the lens, camera.crop_factor, width, and height.
    2. Initialize the modifier with focal_length, aperture, distance, and the image's pixel_format.
    3. Call mod.apply_geometry_distortion() to get the undistorted coordinates.
    4. Use cv2.remap (OpenCV) or SciPy to apply the transformation to your image.

    Note: The lensfunpy.util module provides convenience functions for RGB images that handle both OpenCV and SciPy automatically.

    import cv2
    import lensfunpy
    
    focal_length = 28.0
    aperture = 1.4
    distance = 10
    image_path = '/path/to/image.tiff'
    undistorted_image_path = '/path/to/image_undist.tiff'
    
    img = cv2.imread(image_path)
    height, width = img.shape[0], img.shape[1]
    
    # Assuming 'lens' and 'cam' were found via Database()
    mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
    mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype)
    
    undist_coords = mod.apply_geometry_distortion()
    img_undistorted = cv2.remap(img, undist_coords, None, cv2.INTER_LINEAR)
    cv2.imwrite(undistorted_image_path, img_undistorted)
  4. Build lensfunpy from source on Windows (Experimental)

    master

    Building from source on Windows is experimental and requires Visual Studio. Use the provided PowerShell script with environment variables to configure the build.

    Environment Variables:

    • USE_CONDA: Set to '1' to use Conda, or '0' to build within an existing Python environment.
    • PYTHON_VERSION: e.g., '3.10'
    • PYTHON_ARCH: e.g., 'x86_64'
    • NUMPY_VERSION: e.g., '2.0.*'

    Execution: Run the following in a PowerShell window:

    $env:USE_CONDA = '1'
    $env:PYTHON_VERSION = '3.10'
    $env:PYTHON_ARCH = 'x86_64'
    $env:NUMPY_VERSION = '2.0.*'
    git clone https://github.com/letmaik/lensfunpy
    cd lensfunpy
    .github/scripts/build-windows.ps1
  5. Build lensfunpy from source on Linux/macOS

    master

    If you need a specific lensfun version or cannot use binary wheels, follow these steps:

    1. Install liblensfun:
      • Ubuntu: sudo apt-get install liblensfun-dev
      • Or build from source:
        git clone https://github.com/lensfun/lensfun
        cd lensfun
        cmake .
        sudo make install
    2. Install lensfunpy:
      git clone https://github.com/letmaik/lensfunpy
      cd lensfunpy
      pip install numpy cython
      pip install .

    Troubleshooting ImportError: liblensfun.so.0 on Linux: If the library is not found, add /usr/local/lib to your dynamic linker configuration:

    echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/99local.conf
    sudo ldconfig

    Note: The installation path might vary (e.g., /usr/local/lib/x86_64-linux-gnu or /usr/local/lib64).

    # Install liblensfun from source
    git clone https://github.com/lensfun/lensfun
    cd lensfun
    cmake .
    sudo make install
    
    # Install lensfunpy
    git clone https://github.com/letmaik/lensfunpy
    cd lensfunpy
    pip install numpy cython
    pip install .
  6. Correct lens vignetting

    master

    To correct vignetting, use lensfunpy.Modifier.apply_color_modification(img).

    Important Requirements:

    • The input image must be in a linear state (not gamma corrected).
    • The method returns a boolean indicating if the correction was applied. If it returns False, calibration data for that lens/camera combination might be missing.

    Steps:

    1. Initialize lensfunpy.Modifier with lens, crop factor, width, and height.
    2. Call mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype).
    3. Call mod.apply_color_modification(img) to modify the image in-place.
    import lensfunpy
    import imageio
    
    db = lensfunpy.Database()
    cam = db.find_cameras('NIKON CORPORATION', 'NIKON D3S')[0]
    lens = db.find_lenses(cam, 'Nikon', 'Nikkor AF 20mm f/2.8D')[0]
    
    img = imageio.imread('/path/to/image.tiff')
    
    focal_length = 20
    aperture = 4
    distance = 10
    width = img.shape[1]
    height = img.shape[0]
    
    mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
    mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype)
    
    did_apply = mod.apply_color_modification(img)
    if did_apply:
        imageio.imwrite('/path/to/image_corrected.tiff', img)
    else:
        print('vignetting not corrected, calibration data missing?')
  7. Use ModifyFlags to control lens correction operations

    master

    The lensfunpy.ModifyFlags enumeration is used to specify which lens correction operations should be applied during processing. This allows you to selectively enable or disable effects like vignette correction or TCA (Transverse Chromatic Aberration) correction.

    import lensfunpy
    # Example usage of ModifyFlags (actual flags depend on implementation)
    # flags = lensfunpy.ModifyFlags.VIGNETTE_CORRECTION | lensfunpy.ModifyFlags.TCA_CORRECTION
  8. Use the Lens class in lensfunpy

    master
    The lensfunpy.Lens class is the primary interface for interacting with lens profiles provided by the Lensfun database. It allows you to access lens-specific parameters such as focal length, aperture, and crop factor, and serves as the basis for applying lens corrections (like vignette or TCA correction) to images.