Skip to content

Commit

Permalink
return None if distortion calibration missing instead of garbage coor…
Browse files Browse the repository at this point in the history
…dinates
  • Loading branch information
letmaik committed Oct 24, 2020
1 parent b05769c commit a16273a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
33 changes: 21 additions & 12 deletions lensfunpy/_lensfun.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -927,35 +927,44 @@ cdef class Modifier:
def apply_geometry_distortion(self, float xu = 0, float yu = 0, int width = -1, int height = -1):
"""
:return: coordinates for geometry distortion correction
:rtype: ndarray of shape (height, width, 2)
:return: coordinates for geometry distortion correction,
or None if calibration data missing
:rtype: ndarray of shape (height, width, 2) or None
"""
width, height = self._widthHeight(width, height)
cdef np.ndarray[DTYPE_t, ndim=3, mode='c'] res = np.empty((height, width, 2), dtype=DTYPE)
lf_modifier_apply_geometry_distortion(self.lf, xu, yu, width, height, &res[0,0,0])
return res
if lf_modifier_apply_geometry_distortion(self.lf, xu, yu, width, height, &res[0,0,0]):
return res
else:
return None

def apply_subpixel_distortion(self, float xu = 0, float yu = 0, int width = -1, int height = -1):
"""
:return: per-channel coordinates for subpixel distortion correction
:rtype: ndarray of shape (height, width, 2, 3)
:return: per-channel coordinates for subpixel distortion correction,
or None if calibration data missing
:rtype: ndarray of shape (height, width, 2, 3) or None
"""
width, height = self._widthHeight(width, height)
cdef np.ndarray[DTYPE_t, ndim=4, mode='c'] res = np.empty((height, width, 2, 3), dtype=DTYPE)
lf_modifier_apply_subpixel_distortion(self.lf, xu, yu, width, height, &res[0,0,0,0])
return res
if lf_modifier_apply_subpixel_distortion(self.lf, xu, yu, width, height, &res[0,0,0,0]):
return res
else:
return None

def apply_subpixel_geometry_distortion(self, float xu = 0, float yu = 0, int width = -1, int height = -1):
"""
:return: per-channel coordinates for combined distortion and subpixel distortion correction
:rtype: ndarray of shape (height, width, 2, 3)
:return: per-channel coordinates for combined distortion and subpixel distortion correction,
or None if calibration data missing
:rtype: ndarray of shape (height, width, 2, 3) or None
"""
width, height = self._widthHeight(width, height)
cdef np.ndarray[DTYPE_t, ndim=4, mode='c'] res = np.empty((height, width, 2, 3), dtype=DTYPE)
lf_modifier_apply_subpixel_geometry_distortion(self.lf, xu, yu, width, height, &res[0,0,0,0])
return res
if lf_modifier_apply_subpixel_geometry_distortion(self.lf, xu, yu, width, height, &res[0,0,0,0]):
return res
else:
return None

def apply_color_modification(self, img_dtypes[:,:,::1] img):
"""
Expand Down
32 changes: 12 additions & 20 deletions test/tests.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
from __future__ import division, print_function

import numpy as np
import lensfunpy as lensfun
import gc
from numpy.testing.utils import assert_equal

# the following strings were taken from the lensfun xml files
cam_maker = u'NIKON CORPORATION'
cam_model = u'NIKON D3S'
lens_maker = u'Nikon'
lens_model = u'Nikkor 28mm f/2.8D AF'
# Note regarding u-prefix:
# As lensfunpy returns unicode strings we declare the above
# as unicocd strings as well to make comparison in unit tests easier.
# (Python 2 would default to byte, Python 3 to unicode and comparing byte
# with unicode fails without en/decoding)
cam_maker = 'NIKON CORPORATION'
cam_model = 'NIKON D3S'
lens_maker = 'Nikon'
lens_model = 'Nikon AI-S Nikkor 28mm f/2.8'

def testDatabaseLoading():
db = lensfun.Database()
Expand All @@ -33,11 +26,7 @@ def testDatabaseLoading():
assert_equal(lens.maker.lower(), lens_maker.lower())
assert len(str(lens)) > 0

if lensfun.lensfun_version >= (0,3):
# lens names were "streamlined" in lensfun 0.3
assert_equal(lens.model.lower(), u'nikon af nikkor 28mm f/2.8d')
else:
assert_equal(lens.model.lower(), lens_model.lower())
assert_equal(lens.model.lower(), lens_model.lower())

def testDatabaseXMLLoading():
xml = """
Expand All @@ -61,11 +50,14 @@ def testDatabaseXMLLoading():
</camera>
<lens>
<maker>Nikon</maker>
<model>Nikkor 28mm f/2.8D AF</model>
<mount>Nikon F AF</mount>
<cropfactor>1.0</cropfactor>
<model>Nikon AI-S Nikkor 28mm f/2.8</model>
<model lang="en">Nikkor AI-S 28mm f/2.8</model>
<mount>Nikon F AI-S</mount>
<cropfactor>1</cropfactor>
<calibration>
<distortion model="ptlens" focal="28" a="0" b="0.025773" c="-0.085777" />
<!-- Taken with Nikon D600 -->
<distortion model="ptlens" focal="28" a="0.00929" b="-0.02155" c="0.0021"/>
<tca model="poly3" focal="28" br="-0.0002306" vr="1.0006860" bb="0.0002350" vb="0.9995614"/>
</calibration>
</lens>
</lensdatabase>
Expand Down

0 comments on commit a16273a

Please sign in to comment.