Giter VIP home page Giter VIP logo

lensfunpy's Introduction

lensfunpy

lensfunpy is an easy-to-use Python wrapper for the lensfun library.

API Documentation

Sample code

How to find cameras and lenses

import lensfunpy

cam_maker = 'NIKON CORPORATION'
cam_model = 'NIKON D3S'
lens_maker = 'Nikon'
lens_model = 'Nikkor 28mm f/2.8D AF'

db = lensfunpy.Database()
cam = db.find_cameras(cam_maker, cam_model)[0]
lens = db.find_lenses(cam, lens_maker, lens_model)[0]

print(cam)
# Camera(Maker: NIKON CORPORATION; Model: NIKON D3S; Variant: ; 
#        Mount: Nikon F AF; Crop Factor: 1.0; Score: 0)

print(lens)
# Lens(Maker: Nikon; Model: Nikkor 28mm f/2.8D AF; Type: RECTILINEAR;
#      Focal: 28.0-28.0; Aperture: 2.79999995232-2.79999995232; 
#      Crop factor: 1.0; Score: 110)

How to correct lens distortion

import cv2 # OpenCV library

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]

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_LANCZOS4)
cv2.imwrite(undistorted_image_path, img_undistorted)

It is also possible to apply the correction via SciPy instead of OpenCV. The lensfunpy.util module contains convenience functions for RGB images which handle both OpenCV and SciPy.

How to correct lens vignetting

Note that the assumption is that the image is in a linear state, i.e., it is not gamma corrected.

import lensfunpy
import imageio

db = lensfun.Database()
cam = db.find_cameras('NIKON CORPORATION', 'NIKON D3S')[0]
lens = db.find_lenses(cam, 'Nikon', 'Nikkor AF 20mm f/2.8D')[0]

# The image is assumed to be in a linearly state.
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?')

How to correct lens vignetting and TCA

Note that the assumption is that the image is in a linear state, i.e., it is not gamma corrected. Vignetting should always be corrected first before applying the TCA correction.

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]

# The image is assumed to be in a linearly state.
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)

# Vignette Correction
mod.apply_color_modification(img)

# TCA Correction
undist_coords = mod.apply_subpixel_distortion()
img[..., 0] = cv2.remap(img[..., 0], undist_coords[..., 0, :], None, cv2.INTER_LANCZOS4)
img[..., 1] = cv2.remap(img[..., 1], undist_coords[..., 1, :], None, cv2.INTER_LANCZOS4)
img[..., 2] = cv2.remap(img[..., 2], undist_coords[..., 2, :], None, cv2.INTER_LANCZOS4)

imageio.imwrite('/path/to/image_corrected.tiff', img)

Installation

Install lensfunpy by running:

pip install lensfunpy

64-bit binary wheels are provided for Linux, macOS, and Windows.

Installation from source on Linux/macOS

If you have the need to use a specific lensfun version or you can't use the provided binary wheels then follow the steps in this section to build lensfunpy from source.

First, install the lensfun library on your system.

On Ubuntu, you can get (an outdated) version with:

sudo apt-get install liblensfun-dev

Or install the latest developer version from the Git repository:

git clone https://github.com/lensfun/lensfun
cd lensfun
cmake .
sudo make install

After that, install lensfunpy using:

git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
pip install numpy cython
pip install .

On Linux, if you get the error "ImportError: liblensfun.so.0: cannot open shared object file: No such file or directory" when trying to use lensfunpy, then do the following:

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

The lensfun library is installed in /usr/local/lib when compiled from source, and apparently this folder is not searched for libraries by default in some Linux distributions. Note that on some systems the installation path may be slightly different, such as /usr/local/lib/x86_64-linux-gnu or /usr/local/lib64.

Installation from source on Windows

These instructions are experimental and support is not provided for them. Typically, there should be no need to build manually since wheels are hosted on PyPI.

You need to have Visual Studio installed to build lensfunpy.

In a PowerShell window:

$env:USE_CONDA = '1'
$env:PYTHON_VERSION = '3.7'
$env:PYTHON_ARCH = 'x86_64'
$env:NUMPY_VERSION = '1.14.*'
git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
.github/scripts/build-windows.ps1

The above will download all build dependencies (including a Python installation) and is fully configured through the four environment variables. Set USE_CONDA = '0' to build within an existing Python environment.

lensfunpy's People

Contributors

jia-kai avatar kelsolaar avatar koykl avatar letmaik avatar tvwerkhoven avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

lensfunpy's Issues

Lens not found, may be a lensfun issue

>>> list(filter(lambda l: l.model == 'Nikon AF-S Zoom-Nikkor 17-35mm f/2.8D IF-ED', db.lenses))
[Lens(Maker: Nikon; Model: Nikon AF-S Zoom-Nikkor 17-35mm f/2.8D IF-ED; Type: RECTILINEAR; Focal: 17.0-35.0; Aperture: 2.79999995232-2.79999995232; Crop factor: 1.52799999714; Score: 0)]
>>>  db.find_lenses(db.find_cameras('Nikon Corporation', 'Nikon D3S')[0], None, 'Nikon AF-S Zoom-Nikkor 17-35mm f/2.8D IF-ED', True)
[Lens(Maker: Tamron; Model: Tamron 17-35mm f/2.8-4 Di LD; Type: RECTILINEAR; Focal: 17.0-35.0; Aperture: 2.79999995232-4.0; Crop factor: 1.0; Score: 65)]

Question about linear images

Hi, I'm using lensfunpy to correct my raw footage made with my Canon DSLRs (and MagicLantern). I export the geometry distortion as a UV map to un-distort the images, this is working well so far.
But I've got some issues when it comes to vignetting correction, it seems that apply_color_modification doesn't accept floating points data as input, I get either an error or a strange image as output.
I decided to try with 8 bits images, but I'm wondering if the output is gamma corrected.
Could you help me ?

Install Develop Database

Dear all,

Thanks for the great tool and the clear documentation. I installed lensfunpy on my 64-bit machine (Linux Mint 19.3 - Ubuntu Tricia) using pip install and everything is working fine.
I cloned the database from the lensfun GitHub repository as suggested. However, apparently, it is loading the latest stable version 0.3.2., but I need the develop version (since it already has the lens information of the Mavic Pro from DJI). The lensfun Github repository is not very helpful regarding that matter. Do you know a way how I can install the develop version?
Any help is highly appreciated.

Best,
Soraya

Not all lenses show up? -- HD Pentax-D FA 24-70mm f/2.8 ED SDM WR

I'm trying to get corrections for the HD Pentax-D FA 24-70mm f/2.8 ED SDM WR but somehow not all lenses show up, for Pentax specifically most HD lenses don't show up. Am I doing something wrong?

The below code returns no lens:

import lensfunpy

cam_maker = 'Pentax'
cam_model = '35mm film: full frame'

lens_maker = 'Pentax'
lens_model = 'HD Pentax-D FA 24-70mm f/2.8 ED SDM WR'

db = lensfunpy.Database()
cam = db.find_cameras(cam_maker, cam_model)[0]
lens = db.find_lenses(cam, lens_maker, lens_model)[0]

Checking the database for Pentax lenses gives a list of lenses:

for l in db.lenses:
    print(l.maker,l.model)

gives

Pentax 01 Standard Prime 8.5mm f/1.9 AL [IF]
Pentax HD Pentax-DA 20-40mm f/2.8-4 ED Limited DC WR
Pentax Pentax Optio 230GS & compatibles (Standard)
Pentax Pentax Optio 430 & compatibles (Standard)
Pentax Pentax Optio 43WR & compatibles (Standard)
Pentax Pentax Optio 750Z & compatibles (Standard)
Pentax smc Pentax K 30mm f/2.8
Pentax smc Pentax-A 28mm 1:2.8
Pentax smc Pentax-A 50mm f/1.7
Pentax smc Pentax-D FA Macro 100mm f/2.8 WR
Pentax smc Pentax-DA 12-24mm f/4 ED AL IF
Pentax smc Pentax-DA 15mm f/4 ED AL Limited
Pentax smc Pentax-DA 16-45mm f/4 ED AL
Pentax smc Pentax-DA 18-135mm f/3.5-5.6 ED AL IF DC WR
Pentax smc Pentax-DA 18-55mm f/3.5-5.6 AL
Pentax smc Pentax-DA 18-55mm f/3.5-5.6 AL II L WR
Pentax smc Pentax-DA 21mm f/3.2 AL Limited
Pentax smc Pentax-DA 35mm f/2.4 AL
Pentax smc Pentax-DA 40mm f/2.8 Limited
Pentax smc Pentax-DA 50-200mm f/4-5.6 DA ED
Pentax smc Pentax-DA 50mm f/1.8
Pentax smc Pentax-DA 55-300mm f/4-5.8 ED
Pentax smc Pentax-DA Fish-Eye 10-17mm f/3.5-4.5 ED IF
Pentax smc Pentax-DA L 55-300mm f/4-5.8 ED
Pentax smc Pentax-DA* 16-50mm f/2.8 ED AL IF SDM
Pentax smc Pentax-DA* 50-135mm f/2.8 ED IF SDM
Pentax smc Pentax-FA 28mm f/2.8 AL
Pentax smc Pentax-FA 31mm f/1.8 AL Limited
Pentax smc Pentax-FA 43mm f/1.9 Limited
Pentax smc Pentax-FA 50mm f/1.4
Pentax smc Pentax-M 150mm f/3.5
Pentax smc Pentax-M 28mm 1:3.5
Pentax smc Pentax-M 35mm 1:2
Pentax smc Pentax-M 50mm f/2
Pentax smc Pentax-M Macro 1:4 50mm
Pentax Takumar 135mm f/2.5 Bayonet

However running grep "<model>" /opt/local/share/lensfun/version_1/slr-pentax.xml gives many more lenses (skipping mounts):

        <model>smc Pentax-DA 12-24mm f/4 ED AL IF</model>
        <model>smc Pentax-DA 16-45mm f/4 ED AL</model>
        <model>HD Pentax-D FA 15-30mm f/2.8 ED SDM WR</model>
        <model>HD Pentax-DA 16-85mm f/3.5-5.6 ED DC WR</model>
        <model>smc Pentax-DA 18-55mm f/3.5-5.6 AL</model>
        <model>smc Pentax-DA 18-55mm f/3.5-5.6 AL II L WR</model>
        <model>smc Pentax-DA 18-135mm f/3.5-5.6 ED AL IF DC WR</model>
        <model>smc Pentax-DA 18-250mm f/3.5-6.3 ED AL [IF]</model>
        <model>Pentax-F 28-80mm f/3.5-4.5</model>
        <model>HD Pentax-D FA 28-105mm f/3.5-5.6 ED DC WR</model>
        <model>HD Pentax-D FA 70-200mm f/2.8 ED DC AW</model>
        <model>smc Pentax-DA 50-200mm f/4-5.6 DA ED</model>
        <model>HD Pentax-DA 55-300mm f/4-5.8 ED WR</model>
        <model>smc Pentax-DA 55-300mm f/4-5.8 ED</model>
        <model>smc Pentax-DA L 55-300mm f/4-5.8 ED</model>
        <model>smc Pentax-DA 55-300mm f/4-5.8 ED</model>
        <model>smc Pentax-DA 15mm f/4 ED AL Limited</model>
        <model>smc Pentax K 30mm f/2.8</model>
        <model>smc Pentax-DA 40mm f/2.8 Limited</model>
        <model>smc Pentax-DA 40mm f/2.8 XS</model>
        <model>smc Pentax-FA 43mm f/1.9 Limited</model>
        <model>smc Pentax-FA 77mm f/1.8 Limited</model>
        <model>Takumar 135mm f/2.5 Bayonet</model>
        <model>smc Pentax-M 50mm f/1.7</model>
        <model>smc Pentax-M 50mm f/2</model>
        <model>smc Pentax-D FA Macro 100mm f/2.8 WR</model>
        <model>smc Pentax-D FA Macro 100mm f/2.8 WR</model>
        <model>smc Pentax-M 150mm f/3.5</model>
        <model>smc Pentax-DA 35mm f/2.4 AL</model>
        <model>smc Pentax-DA 35mm f/2.4 AL</model>
        <model>HD Pentax-DA 70mm f/2.4 Limited</model>
        <model>smc Pentax-DA 70mm f/2.4 Limited</model>
        <model>smc Pentax-DA* 50-135mm f/2.8 ED IF SDM</model>
        <model>smc Pentax-FA 50mm f/1.4</model>
        <model>smc Pentax-FA 50mm f/1.4</model>
        <model>HD Pentax-DA 21mm f/3.2 ED AL Limited</model>
        <model>smc Pentax-DA 21mm f/3.2 AL Limited</model>
        <model>smc Pentax-FA 28mm f/2.8 AL</model>
        <model>smc Pentax-FA 28mm f/2.8 AL</model>
        <model>smc Pentax-DA* 16-50mm f/2.8 ED AL IF SDM</model>
        <model>smc Pentax-DA Fish-Eye 10-17mm f/3.5-4.5 ED IF</model>
        <model>smc Pentax-A 50mm f/1.7</model>
        <model>smc Pentax-A 50mm f/1.7</model>
        <model>smc Pentax-A 28mm 1:2.8</model>
        <model>smc Pentax-M 28mm 1:3.5</model>
        <model>smc Pentax-M 35mm 1:2</model>
        <model>smc Pentax-FA 31mm f/1.8 AL Limited</model>
        <model>smc Pentax-DA 50mm f/1.8</model>
        <model>smc Pentax-DA 50mm f/1.8</model>
        <model>HD Pentax-DA 20-40mm f/2.8-4 ED Limited DC WR</model>
        <model>smc Pentax-M Macro 1:4 50mm</model>
        <model>smc Pentax-M Macro 1:4 100mm</model>
        <model>smc Pentax-DA L 18-50mm f/4-5.6 DC WR RE</model>
        <model>smc Pentax-DA L 18-50mm f/4-5.6 DC WR RE</model>
        <model>HD Pentax-DA 18-50mm f/4-5.6 DC WR RE</model>
        <model>HD Pentax-D FA 150-450mm f/4.5-5.6 ED DC AW</model>
        <model>smc Pentax-DA L 50-200mm f/4-5.6 ED WR</model>
        <model>smc Pentax-DA L 50-200mm f/4-5.6 ED WR</model>
        <model>smc Pentax-DA 17-70mm f/4 AL [IF] SDM</model>
        <model>Super-Takumar 55mm f/1.8</model>
        <model>Super-Takumar 50mm f/1.4</model>
        <model>HD Pentax-D FA 24-70mm f/2.8 ED SDM WR</model>
        <model>smc Pentax-DA 35mm f/2.8 Macro Limited</model>
        <model>Pentax SMC Takumar 50mm f/1.4</model>
        <model>smc Pentax-FA 28-70mm f/4 AL</model>
        <model>smc Pentax-A 50mm f/1.4</model>
        <model>smc Pentax-F 28mm f/2.8</model>
        <model>smc PENTAX DA* 60-250mm F4 [IF] SDM</model>
        <model>HD Pentax-DA 55-300mm f/4.5-6.3 ED PLM WR RE</model>

check type in lensfunpy.Modifier().initialize()

a small usability improvement suggestion. I was consistently getting a segfault error at initialize with the code below because I forgot to include the [0] indexing for lens. It would make sense to check that lens is of type lensfunpy._lensfun.Lens and cam is of type lensfunpy._lensfun.Camera

import lensfunpy
import cv2
import piexif

image_path = 'swirf16.tif'
img_png = image_path+'.png'
undistorted_image_path = image_path+'-corrected.png'

im = cv2.imread(image_path)
cv2.imwrite(img_png, im)
height, width = im.shape[0], im.shape[1]

meta = piexif.load(image_path)
focal_length=meta['Exif'][37386]
focal_length = focal_length[0]/focal_length[1]
aperture = meta['Exif'][33437]
aperture = aperture[0]/aperture[1]
distance = .3

cam_make = meta['0th'][271]
cam_model = meta['0th'][272]
lens_make = meta['Exif'][42035]
lens_model=meta['Exif'][42036]
db = lensfunpy.Database()
cam = db.find_cameras(cam_make, cam_model)[0]
lens = db.find_lenses(cam, lens_make, lens_model)
mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(focal_length, aperture, distance)

source files missing on pypi

There is no sdist (.tar.gz) uploaded on pypi, which is why the most recent version is not being picked up by the conda-forge autoupdater bot.

Raise exception on DB loading errors/warnings

E.g. I used new database files in /home/.../.local/share/lensfun/ with an old (Ubuntu repo) lensfun version. This caused the following type of warning and had the effect that these home-folder db files were not loaded, just the system-level ones. This is confusing as it is really an error instead of a warning.

 ** (process:22844): WARNING **: /home/.../.local/share/lensfun/generic.xml:7:1: The <lensdatabase> element cannot have any attributes!

Calculate new point after remapping of points

I looked through all the code and did't find formulas that calculate new coordinates after using method apply_geometry_distortion of Modifier class.

After undistorting the image, some pixels are lost, and i would like to know its location behind the image (e.g. point (-20; 20)).

Can I somehow match the pixels coordinates on the original frame and undistorted?

Database version is 2, but supported is only 1!

Hi, I am getting following error:

** (process:3908): WARNING **: 22:28:25.364: [Lensfun] mil-sony.xml:3:1: Database version is 2, but supported is only 1!

Traceback (most recent call last):
File "correction.py", line 15, in
db = lensfunpy.Database(paths = xml)
File "lensfunpy/_lensfun.pyx", line 230, in lensfunpy._lensfun.Database.init
File "lensfunpy/_lensfun.pyx", line 966, in lensfunpy._lensfun.handleError
lensfunpy._lensfun.XMLFormatError

Installation failed on Jetson Xavier

I am installing lensfunpy on Jetson Xavier. I got the following error.
sudo apt-get install libffi-dev Reading package lists... Done Building dependency tree Reading state information... Done libffi-dev is already the newest version (3.2.1-8). libffi-dev set to manually installed. The following packages were automatically installed and are no longer required: python3-click python3-colorama Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded. xavier@xavier-desktop:~/Downloads/lensfunpy-master$ sudo python3 setup.py install Package lensfun was not found in the pkg-config search path. Perhaps you should add the directory containing lensfun.pc'
to the PKG_CONFIG_PATH environment variable
No package 'lensfun' found
Package lensfun was not found in the pkg-config search path.
Perhaps you should add the directory containing lensfun.pc' to the PKG_CONFIG_PATH environment variable No package 'lensfun' found Package lensfun was not found in the pkg-config search path. Perhaps you should add the directory containing lensfun.pc'
to the PKG_CONFIG_PATH environment variable
No package 'lensfun' found
Package lensfun was not found in the pkg-config search path.
Perhaps you should add the directory containing lensfun.pc' to the PKG_CONFIG_PATH environment variable No package 'lensfun' found Package lensfun was not found in the pkg-config search path. Perhaps you should add the directory containing lensfun.pc'
to the PKG_CONFIG_PATH environment variable
No package 'lensfun' found
running install
running bdist_egg
running egg_info
writing lensfunpy.egg-info/PKG-INFO
writing dependency_links to lensfunpy.egg-info/dependency_links.txt
writing requirements to lensfunpy.egg-info/requires.txt
writing top-level names to lensfunpy.egg-info/top_level.txt
reading manifest file 'lensfunpy.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'lensfunpy/version_helper.h'
writing manifest file 'lensfunpy.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-aarch64/egg
running install_lib
running build_py
running build_ext
building 'lensfunpy._lensfun' extension
aarch64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Ilensfunpy -I/usr/local/lib/python3.6/dist-packages/numpy/core/include -I/home/xavier/Downloads/lensfunpy-master/lensfunpy -I/usr/include/python3.6m -c lensfunpy/_lensfun.c -o build/temp.linux-aarch64-3.6/lensfunpy/_lensfun.o
In file included from /usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1832:0,
from /usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:12,
from /usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
from lensfunpy/_lensfun.c:636:
/usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it with "
^~~~~~~
lensfunpy/_lensfun.c:638:10: fatal error: lensfun.h: No such file or directory
#include "lensfun.h"
^~~~~~~~~~~
compilation terminated.
error: command 'aarch64-linux-gnu-gcc' failed with exit status 1
`
I have tried with both i.e with pip and with setup.py but the same error occurs (fatal error: lensfun.h: No such file or directory)

Chromatic aberrations fix example ?

Hello,

Those python bindings to lensfun look pretty cool!

What is the workflow for fixing chromatic aberration / vignetting? (I need to fix chromatic aberrations, but not large distortion). It's not really clear in the API docs.

Thanks.

Paul.

String memory corruption with xml strings

On Travis, the Mac builds fail randomly for Python 3.3 and 3.4 (2.7 not observed yet) with:

tests.testDatabaseXMLLoading ... 
** (process:89054): WARNING **: [lensfun] XML:25:72: Error on line 25 char 72: Odd character 'l', expected a '=' after attribute name 'c' of element 'distortion'

or

tests.testDatabaseXMLLoading ... 
** (process:89032): WARNING **: [lensfun] XML:1:1: Error on line 1 char 1: Document must begin with an element (e.g. <book>)

Why is this not happening on Linux or Windows? Could it be a problem with the gettext install?

This happened only recently and may be due to Travis updating the Mac workers.

Differences between Hugin and Lensfunpy

I'm attempting to recreate a lens distortion correction that I've achieved in Hugin Lens calibration GUI whilst using lensfunpy, there seems to be a slight discrepancy between the two approaches that I can't account for.

The raw image:
image

The lens distortion correction parameters and output in Hugin Lens calibration GUI:
Screenshot from 2019-05-09 15-39-29

focal length 8
focal length multiplier 3.97

a 0.00199
b -0.04563
c -0.02328

The lens distortion correction output from lensfunpy:
undistorted_image

My python script:

import lensfunpy
import lensfunpy.util

import cv2

IMAGE = "/home/gsi/Downloads/image.jpg"
SHOW_IMAGE = False
XML = """
<lensdatabase>
    <camera>
        <maker>MAKER</maker>
        <model>MODEL</model>
        <mount>djiFC6310</mount>
        <cropfactor>3.97</cropfactor>
    </camera>
    
    <lens>
        <maker>MAKER</maker>
        <model>MODEL</model>
        <mount>djiFC6310</mount>
        <cropfactor>3.97</cropfactor>
        <calibration>
            <distortion model="ptlens" focal="8.0" a="0.00199" b="-0.04563" c="-0.02328"/>
        </calibration>
    </lens>
</lensdatabase>

"""

image = cv2.imread(IMAGE)

db = lensfunpy.Database(xml=XML)
cam = db.find_cameras("MAKER", "MODEL")[0]
lens = db.find_lenses(cam, "MAKER", "MODEL")[0]

print(cam)
print(lens)

height, width = image.shape[0], image.shape[1]

mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(
    8.0, 
    1.4
)

undistorted_coords = mod.apply_geometry_distortion()   
undistorted_image = lensfunpy.util.remap(image, undistorted_coords)

cv2.imwrite("undistorted_image.jpg", undistorted_image)

The differences are small, but seem significant to my eye!

Any help greatly appreciated.

ImportError: liblensfun.so.1 on Mint 17.2

Hi,

the fix:

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

does not work for me.

When installed with "apt-get" the libraries are located in a subdirectory of /usr/local/lib:

/usr/local/lib
โ”œโ”€โ”€ x86_64-linux-gnu
โ”‚ย ย  โ”œโ”€โ”€ pkgconfig
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ lensfun.pc
โ”‚ย ย  โ”œโ”€โ”€ liblensfun.so.1 -> liblensfun.so.0.3.2
โ”‚ย ย  โ”œโ”€โ”€ liblensfun.so.0.3.2
โ”‚ย ย  โ””โ”€โ”€ liblensfun.so -> liblensfun.so.1

Changing the location to the subdirectory works for me:

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

lensfunpy does not install on Debian unstable

hello, I cannot get lensfunpy to install on Debian unstable. I think that I'm following the instructions

$ sudo apt-get install liblensfun0 liblensfun-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
liblensfun-dev is already the newest version (0.3.2-3).
liblensfun0 is already the newest version (0.2.8-2).
The following packages were automatically installed and are no longer required:
  firebird2.5-common firebird2.5-common-doc firebird2.5-server-common firebird3.0-common
  firebird3.0-common-doc imagemagick-common libchromaprint0 libfbclient2 libfbembed2.5 libglew1.13 libgsoap9
  liblouis10 libmagick++-6.q16-5v5 libperl5.22 libtommath1 libvpx3 libx265-79 libxapian22v5
  linux-headers-4.4.0-1-amd64 linux-headers-4.4.0-1-common linux-image-3.16.0-4-amd64
  linux-image-4.4.0-1-amd64 linux-kbuild-4.4 perl-modules-5.22
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ pip install lensfunpy
Collecting lensfunpy
  Using cached lensfunpy-1.4.0.tar.gz
Collecting enum34 (from lensfunpy)
  Using cached enum34-1.1.6-py2-none-any.whl
Building wheels for collected packages: lensfunpy
  Running setup.py bdist_wheel for lensfunpy ... error
  Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-H4Q98Z/lensfunpy/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /tmp/tmpv9OygApip-wheel- --python-tag cp27:
  Package lensfun was not found in the pkg-config search path.
  Perhaps you should add the directory containing `lensfun.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'lensfun' found
  Package lensfun was not found in the pkg-config search path.
  Perhaps you should add the directory containing `lensfun.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'lensfun' found
  Package lensfun was not found in the pkg-config search path.
  Perhaps you should add the directory containing `lensfun.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'lensfun' found
  Package lensfun was not found in the pkg-config search path.
  Perhaps you should add the directory containing `lensfun.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'lensfun' found
  Package lensfun was not found in the pkg-config search path.
  Perhaps you should add the directory containing `lensfun.pc'
  to the PKG_CONFIG_PATH environment variable
  No package 'lensfun' found
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-2.7
  creating build/lib.linux-x86_64-2.7/lensfunpy
  copying lensfunpy/__init__.py -> build/lib.linux-x86_64-2.7/lensfunpy
  copying lensfunpy/util.py -> build/lib.linux-x86_64-2.7/lensfunpy
  copying lensfunpy/_version.py -> build/lib.linux-x86_64-2.7/lensfunpy
  running build_ext
  building 'lensfunpy._lensfun' extension
  creating build/temp.linux-x86_64-2.7
  creating build/temp.linux-x86_64-2.7/lensfunpy
  x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-EkQe1J/python2.7-2.7.12=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include -I/tmp/pip-build-H4Q98Z/lensfunpy/lensfunpy -I/usr/include/python2.7 -c lensfunpy/_lensfun.c -o build/temp.linux-x86_64-2.7/lensfunpy/_lensfun.o
  In file included from /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1777:0,
                   from /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:18,
                   from /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                   from lensfunpy/_lensfun.c:274:
  /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
   #warning "Using deprecated NumPy API, disable it by " \
    ^~~~~~~
  In file included from lensfunpy/_lensfun.c:276:0:
  lensfunpy/version_helper.h:1:21: fatal error: lensfun.h: No such file or directory
   #include "lensfun.h"
                       ^
  compilation terminated.
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for lensfunpy
  Running setup.py clean for lensfunpy
Failed to build lensfunpy
Installing collected packages: enum34, lensfunpy
  Running setup.py install for lensfunpy ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-H4Q98Z/lensfunpy/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-KOjtOv-record/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    Package lensfun was not found in the pkg-config search path.
    Perhaps you should add the directory containing `lensfun.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'lensfun' found
    Package lensfun was not found in the pkg-config search path.
    Perhaps you should add the directory containing `lensfun.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'lensfun' found
    Package lensfun was not found in the pkg-config search path.
    Perhaps you should add the directory containing `lensfun.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'lensfun' found
    Package lensfun was not found in the pkg-config search path.
    Perhaps you should add the directory containing `lensfun.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'lensfun' found
    Package lensfun was not found in the pkg-config search path.
    Perhaps you should add the directory containing `lensfun.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'lensfun' found
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/lensfunpy
    copying lensfunpy/__init__.py -> build/lib.linux-x86_64-2.7/lensfunpy
    copying lensfunpy/util.py -> build/lib.linux-x86_64-2.7/lensfunpy
    copying lensfunpy/_version.py -> build/lib.linux-x86_64-2.7/lensfunpy
    running build_ext
    building 'lensfunpy._lensfun' extension
    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/lensfunpy
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-EkQe1J/python2.7-2.7.12=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include -I/tmp/pip-build-H4Q98Z/lensfunpy/lensfunpy -I/usr/include/python2.7 -c lensfunpy/_lensfun.c -o build/temp.linux-x86_64-2.7/lensfunpy/_lensfun.o
    In file included from /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1777:0,
                     from /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:18,
                     from /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                     from lensfunpy/_lensfun.c:274:
    /home/s.pook/.local/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
     #warning "Using deprecated NumPy API, disable it by " \
      ^~~~~~~
    In file included from lensfunpy/_lensfun.c:276:0:
    lensfunpy/version_helper.h:1:21: fatal error: lensfun.h: No such file or directory
     #include "lensfun.h"
                         ^
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-H4Q98Z/lensfunpy/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-KOjtOv-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-H4Q98Z/lensfunpy/

Adapt to lensfun API backwards incompatible changes

The next lensfun version (after 0.3.2) has some backwards incompatible changes and other things that should be adapted in lensfunpy: https://sourceforge.net/p/lensfun/code/ci/master/tree/ChangeLog

As lensfunpy exposes some of the breaking functions (Lens.interpolate_*) it will be backwards incompatible as well, meaning the next version will be 2.0.0. For people building lensfunpy from source, this would also raise the minimum supported lensfun version to the next released lensfun version.

The functions with breaking API in lensfunpy are more advanced and probably not used in the majority of cases, which is good.

Given that this is more work than usual and the next lensfun version is still in development and may introduce more breaking changes, I estimate that it will be a good while until the next lensfunpy version.

submodules not reachable

whenever submodules are needed the build fails due to: https://github.com/letmaik/lensfunpy/blob/master/.gitmodules#L3

git clone git://git.code.sf.net/p/lensfun/code
Cloning into 'code'...
fatal: remote error: access denied or repository not exported: /p/lensfun/code

is this situation known or just temporary outage?

PS.: i found it when working with python 3.8 where the install process could not find precompiled binaries and started cloning submodules

EDIT: ah very related to: #21

Hand over contiguous arrays to OpenCV

Older OpenCV versions need contiguous arrays. Make sure this is true for lensfunpy.util.remapOpenCv, by adding np.require(rgb, rgb.dtype, 'C').

OpenCV Error: Assertion failed (src.dims <= 2 && esz <= (size_t)32) in transpose, file /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp, line 1680
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp:1680: error: (-215) src.dims <= 2 && esz <= (size_t)32 in function transpose

Help needed; not getting results

I've run the code in the README on an image, but it doesn't seem to change anything.

This is what I have right now (I tweaked the parameters to extreme values to see what would happen; nothing did):
Screen Shot 2023-09-30 at 3 42 41 PM

However the results seemed to be exactly the same.

Empty database on Linux

Hi,
Using linux Mint, I followed your installation protocol with apt-get for lensfun, and then install the lensfunpy library with pip. The python library loads just fine but its database is empty when querying for a camera such as the one in the Readme example.

This is for instance my input and output in ipython:

import lensfunpy
 
cam_maker = 'NIKON CORPORATION'
cam_model = 'NIKON D3S'
lens_maker = 'Nikon'
lens_model = 'Nikkor 28mm f/2.8D AF'
   
db = lensfunpy.Database()
cam = db.find_cameras(cam_maker, cam_model)[0]
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2883, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-3cb54c3ab96e>", line 9, in <module>
    cam = db.find_cameras(cam_maker, cam_model)[0]
IndexError: list index out of range

Any idea where should I look to solve the problem?
Thanks

find_lenses() on Mac seems to have trouble with certain lenses

Hello, I am trying to load the lens profile for a Sigma Fisheye lens. However, I get a StopIteration error. Also it is not possible to print a list of all lenses. I have tried this on Mac Sierra and El Capitan, Python 2.7 and lensfunpy installed through PIP. This is what I input:

import lensfunpy
cam_maker = 'NIKON CORPORATION'
cam_model = 'NIKON D3S'

lens_maker = 'Sigma'
lens_model = "Sigma 8mm f/3.5 EX DG circular fisheye"

db = lensfunpy.Database()
cam = db.find_cameras(cam_maker, cam_model)[0]
lens = db.find_lenses(cam, lens_maker, lens_model)[0]
print(lens)

This is what I get:

StopIteration Traceback (most recent call last)
in ()
----> 1 print(lens)

lensfunpy/_lensfun.pyx in lensfunpy._lensfun.Lens.repr (lensfunpy/_lensfun.c:7480)()

lensfunpy/_lensfun.pyx in lensfunpy._lensfun.Lens.type.get (lensfunpy/_lensfun.c:5771)()

StopIteration:

Is this a problem with lensfunpy?

undefined symbol: lf_db_load_data

I am currently following the build steps in the README to build lensfunpy for ARM on Amazon Lambda but I get this error message when using the library despite the library correctly loading.

These are the Dockerfile commands I am using

RUN cd /tmp && \
    curl -o lensfun.tar.gz -L "https://github.com/lensfun/lensfun/tarball/v0.3.95" && \
    mkdir lensfun && \
    tar xvzf lensfun.tar.gz -C lensfun --strip-components 1 && \
    cd lensfun && \
    cmake . && \
    sudo make install && \
    echo "/usr/local/lib64" | sudo tee /etc/ld.so.conf.d/99local.conf && \
    sudo ldconfig
RUN PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig/" pip install git+https://github.com/letmaik/lensfunpy --global-option="build_ext" --global-option="-I/usr/local/include/lensfun/"

I pass in the custom build arguments to the python package as otherwise it can't find the header files.

Wheel for Python 3.7

Dear Maik,

if you still support lensfunpy - could you please extend the provided wheels to Python 3.7? I tried to compile it myself but failed miserably :)

Greetings

Stefan

core dump on gopro lens.

import cv2
import lensfunpy

db = lensfunpy.Database()
cam = db.find_cameras('gopro',"HERO4",True)[0]
print (cam)
#Camera(Maker: GoPro; Model: HERO4 Black;
#Mount: goProHero4black; Crop Factor: 5.0; Score: 180)

lens = db.find_lenses(cam)
print(lens)
#[Lens(Maker: GoPro; Model: HERO4 black; Type: FISHEYE_STEREOGRAPHIC; Focal: 3.0-3.0; Aperture: None-None; Crop factor: 5.0; Score: 15)]

focal_length = 3.0
aperture = 3
distance = 100
image_path = 'testframe.png'
undistorted_image_path = 'out.png'

im = cv2.imread(image_path)
height, width = im.shape[0], im.shape[1]
print (height, width)
#1124 1998

mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
#Segmentation fault (core dumped)

apply_color_modification: input data type and range?

What data type (uint8/uint16/float32/float64) is the input image expected to be in? In particular for apply_color_modification. Does the vignette correction happen in linear space, or after gamma / tone mapping?

My lens, Nikkor 80-200mm f/2.8 ED (w/ crop sensor), seems to be in the lensfun database as far as I can tell, but when I pass in float64 0-1 normalized linear data to apply_color_modification, I get nonsensical output range (max value gets set to 2.7e+155). When I pass in uint16 linear data, I get *** TypeError: No matching signature found.

make remap work on grayscale images as well

Currently lensfunpy.util.remapScipy assumes RGB and it would make sense to support grayscale images (h,w) but also arbitrary n-channel images (h,w,n).

lensfunpy.util.remapOpenCv probably supports this already but that should be tested.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.