Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mono10g40 & Mono12g24 PixelFormats (IDS cameras) #474

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/harvesters/test/test_harvester_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ def _test_conversion(self, format_name: str, inputs, outputs):
for input, output in zip(inputs, outputs):
packed = np.frombuffer(input, dtype=np.uint8)
unpacked_elements = pf.expand(packed)
self.assertEqual(len(outputs), unpacked_elements.size)
self.assertEqual(len(output), unpacked_elements.size)
for i, element in enumerate(unpacked_elements):
self.assertEqual(output[i], element)

Expand Down Expand Up @@ -821,6 +821,42 @@ def _test_issue_222(self):
]
self._test_conversion('Mono10c3p32', inputs, outputs)

def test_mono10g40_pixel_format(self):
inputs = [
# V0 -> V4
bytes([0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000]),
bytes([0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111]),
bytes([0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b00000000]),
bytes([0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b11111111]),
bytes([0b10000000, 0b01000000, 0b00100000, 0b00010000, 0b00000000]),
]
outputs = [
[0, 0, 0, 0],
[0x3ff, 0x3ff, 0x3ff, 0x3ff],
[0x3fc, 0x3fc, 0x3fc, 0x3fc],
[0x003, 0x003, 0x003, 0x003],
[0x200, 0x100, 0x80, 0x40]
]
self._test_conversion('Mono10g40', inputs, outputs)

def test_mono12g24_pixel_format(self):
inputs = [
# V0 -> V2
bytes([0b00000000, 0b00000000, 0b00000000]),
bytes([0b11111111, 0b11111111, 0b11111111]),
bytes([0b11111111, 0b11111111, 0b00000000]),
bytes([0b00000000, 0b00000000, 0b11111111]),
bytes([0b10000000, 0b01000000, 0b00000000]),
]
outputs = [
[0, 0],
[0xfff, 0xfff],
[0xff0, 0xff0],
[0xf, 0xf],
[0x800, 0x400]
]
self._test_conversion('Mono12g24', inputs, outputs)

def _test_issue_146_mono_unpacked_multibytes(self):
names = ['Mono10', 'Mono12']
maximums = [0x4, 0x10]
Expand Down
9 changes: 8 additions & 1 deletion src/harvesters/util/_pfnc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# We create the following symbolics from the official PFNC table so the
# numeric values are all capitalized but this is an exception: We usually
# go with lower case in other Python files.
symbolics = {
_pfnc_symbolics = {
# As of 17-Feb-2017
0x01080001: 'Mono8',
0x01080002: 'Mono8s',
Expand Down Expand Up @@ -274,3 +274,10 @@
0x02180103: 'YCbCr2020_422_12p_CbYCrY',
}

# https://github.com/genicam/harvesters/issues/473
_extra_symbolics = {
0x4000000f: 'Mono10g40',
0x4000001f: 'Mono12g24',
}

symbolics = {**_extra_symbolics, **_pfnc_symbolics}
160 changes: 160 additions & 0 deletions src/harvesters/util/pfnc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ def get_bits_per_pixel(data_format):
'Mono4p',
'Mono10Packed',
'Mono10p',
'Mono10g40',
'Mono12Packed',
'Mono12p',
'Mono12g24',
'Coord3D_A10p',
'Coord3D_B10p',
'Coord3D_C10p',
Expand Down Expand Up @@ -1298,6 +1300,81 @@ def expand(self, array: numpy.ndarray) -> numpy.ndarray:
# Stack the four pixels as columns, i.e. one row per chunk, then
# flatten to 1D-array
return numpy.column_stack((v0, v1, v2, v3)).ravel()


class _10g40(_PixelFormat):
def __init__(
self, symbolic: str = None, nr_components: float = None,
location: _Location = None):
#
super().__init__(
symbolic=symbolic,
alignment=_Alignment(
unpacked=_DataSize.UINT16, packed=_DataSize.UINT8
),
nr_components=nr_components,
unit_depth_in_bit=10,
location=location
)

def expand(self, array: numpy.ndarray) -> numpy.ndarray:
"""Expand the Mono10g40 format (litte-endian order), where chunks of 5 bytes give 4 pixels.

Parameters
----------
array : numpy.ndarray
One-dimensional array of datatype uint8 representing the raw byte sequence.

Returns
-------
numpy.ndarray
One-dimensional array of datatype uint16 representing the unpacked
pixels with 10-bit data (values from 0 to 1023).
"""

assert array.dtype == numpy.uint8

bytes_packed = 5 # chunks of 5 bytes
# pixels_unpacked = 4 # give 4 pixels

# The .T-transpose allows receiving into five named variables v0--v4.
v0, v1, v2, v3, v4 = array.reshape(
array.size // bytes_packed,
bytes_packed
).astype(numpy.uint16).T

"""
See Figure
https://www.1stvision.com/cameras/IDS/IDS-manuals/en/basics-monochrome-pixel-formats.html

Input: v4 v3 v2 v1 v0

Byte: B4 B3 B2 B1 B0
|..+..+..+..|........|........|........|........|
Pixel: p3-0_LSB p3_MSB p2_MSB p1_MSB p0_MSB

|........+..|........+..|........+..|........+..|
Output: v3 v2 v1 v0

"""
v0 = numpy.bitwise_or(
v0 << 2,
numpy.bitwise_and(v4, 0b0000000011)
)
v1 = numpy.bitwise_or(
v1 << 2,
numpy.bitwise_and(v4 >> 2, 0b0000000011)
)
v2 = numpy.bitwise_or(
v2 << 2,
numpy.bitwise_and(v4 >> 4, 0b0000000011)
)
v3 = numpy.bitwise_or(
v3 << 2,
numpy.bitwise_and(v4 >> 6, 0b0000000011)
)

return numpy.column_stack((v0, v1, v2, v3)).ravel()


class _10p32(_PixelFormat):
Expand Down Expand Up @@ -1398,6 +1475,55 @@ def expand(self, array: numpy.ndarray) -> numpy.ndarray:
),
nr_unpacked * up1st.shape[0]
)


class _12g24(_PixelFormat):
def __init__(
self, symbolic: str = None, nr_components: float = None,
location: _Location = None):
#
super().__init__(
symbolic=symbolic,
alignment=_Alignment(
unpacked=_DataSize.UINT16, packed=_DataSize.UINT8
),
nr_components=nr_components,
unit_depth_in_bit=12,
location=location
)

def expand(self, array: numpy.ndarray) -> numpy.ndarray:
bytes_packed = 3 # chunks of 3 bytes
# pixels_unpacked = 2 # give 2 pixels

v0, v1, v2 = numpy.reshape(
array, (array.shape[0] // bytes_packed, bytes_packed)
).astype(numpy.uint16).T

"""
See Figure
https://www.1stvision.com/cameras/IDS/IDS-manuals/en/basics-monochrome-pixel-formats.html

Input: v2 v1 v0

Byte: B2 B1 B0
|....+....|........|........|
Pixel: p1-0_LSB p1_MSB p0_MSB

|........+....|........+....|
Output: v1 v0

"""
v0 = numpy.bitwise_or(
v0 << 4,
numpy.bitwise_and(v2, 0b000000001111)
)
v1 = numpy.bitwise_or(
v1 << 4,
numpy.bitwise_and(v2 >> 4, 0b000000001111)
)

return numpy.column_stack((v0, v1)).ravel()


class _14p(_PixelFormat):
Expand Down Expand Up @@ -1465,6 +1591,16 @@ def __init__(self, symbolic: str = None):
)


class _Mono_10g40(_10g40):
def __init__(self, symbolic: str = None):
#
super().__init__(
symbolic=symbolic,
nr_components=1.,
location=_Location.MONO
)


class _Mono_10p32(_10p32):
def __init__(self, symbolic: str = None):
#
Expand All @@ -1485,6 +1621,16 @@ def __init__(self, symbolic: str = None):
)


class _Mono_12g24(_12g24):
def __init__(self, symbolic: str = None):
#
super().__init__(
symbolic=symbolic,
nr_components=1.,
location = _Location.MONO
)


class _Mono_14p(_14p):
def __init__(self, symbolic: str = None):
#
Expand Down Expand Up @@ -1527,6 +1673,12 @@ def __init__(self):
super().__init__(symbolic='Mono10p')


class Mono10g40(_Mono_10g40):
def __init__(self):
#
super().__init__(symbolic='Mono10g40')


class Mono10Packed(_Mono_GroupPacked_10):
def __init__(self):
#
Expand All @@ -1551,6 +1703,12 @@ def __init__(self):
super().__init__(symbolic='Mono12p')


class Mono12g24(_Mono_12g24):
def __init__(self):
#
super().__init__(symbolic='Mono12g24')


class Mono14p(_Mono_14p):
def __init__(self):
#
Expand Down Expand Up @@ -3074,9 +3232,11 @@ class Dictionary:
# Mono4p(),
Mono10Packed(),
Mono10p(),
Mono10g40(),
Mono10c3p32(),
Mono12Packed(),
Mono12p(),
Mono12g24(),
Mono14p(),
Coord3D_A10p(),
Coord3D_B10p(),
Expand Down