Skip to content

Commit

Permalink
[#51] Return four vertices and Rect object for rotated images
Browse files Browse the repository at this point in the history
  • Loading branch information
alycejenni authored Apr 25, 2024
2 parents 793d3f5 + 059ff82 commit fb21878
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
50 changes: 35 additions & 15 deletions pylibdmtx/pylibdmtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

# A rectangle
Rect = namedtuple('Rect', 'left top width height')
Rect_vertices = namedtuple('Rect_vertices', 'P0 P1 P2 P3')

# Results of reading a barcode
Decoded = namedtuple('Decoded', 'data rect')
Expand Down Expand Up @@ -146,7 +147,7 @@ def _decoded_matrix_region(decoder, region, corrections):
dmtxMessageDestroy(byref(message))


def _decode_region(decoder, region, corrections, shrink):
def _decode_region(decoder, region, corrections, shrink, return_vertices=False):
"""Decodes and returns the value in a region.
Args:
Expand All @@ -160,19 +161,37 @@ def _decode_region(decoder, region, corrections, shrink):
# Coordinates
p00 = DmtxVector2()
p11 = DmtxVector2(1.0, 1.0)
dmtxMatrix3VMultiplyBy(
p00,
region.contents.fit2raw
)
p10 = DmtxVector2(1.0, 0.0)
p01 = DmtxVector2(0.0, 1.0)
dmtxMatrix3VMultiplyBy(p00, region.contents.fit2raw)
dmtxMatrix3VMultiplyBy(p11, region.contents.fit2raw)
x0 = int((shrink * p00.X) + 0.5)
y0 = int((shrink * p00.Y) + 0.5)
x1 = int((shrink * p11.X) + 0.5)
y1 = int((shrink * p11.Y) + 0.5)
return Decoded(
string_at(msg.contents.output),
Rect(x0, y0, x1 - x0, y1 - y0)
)
dmtxMatrix3VMultiplyBy(p01, region.contents.fit2raw)
dmtxMatrix3VMultiplyBy(p10, region.contents.fit2raw)
x00 = int((shrink * p00.X) + 0.5)
y00 = int((shrink * p00.Y) + 0.5)
x11 = int((shrink * p11.X) + 0.5)
y11 = int((shrink * p11.Y) + 0.5)
x10 = int((shrink * p10.X) + 0.5)
y10 = int((shrink * p10.Y) + 0.5)
x01 = int((shrink * p01.X) + 0.5)
y01 = int((shrink * p01.Y) + 0.5)

if return_vertices:
return Decoded(
string_at(msg.contents.output),
Rect_vertices((x00,y00), (x01,y01), (x10,y10), (x11,y11))
)
else:
min_x = min(x00, x11, x10, x01)
max_x = max(x00, x11, x10, x01)
min_y = min(y00, y11, y10, y01)
max_y = max(y00, y11, y10, y01)

return Decoded(
string_at(msg.contents.output),
Rect(min_x, min_y, max_x - min_x, max_y - min_y)
)

else:
return None

Expand Down Expand Up @@ -229,7 +248,7 @@ def _pixel_data(image):

def decode(image, timeout=None, gap_size=None, shrink=1, shape=None,
deviation=None, threshold=None, min_edge=None, max_edge=None,
corrections=None, max_count=None):
corrections=None, max_count=None, return_vertices=False):
"""Decodes datamatrix barcodes in `image`.
Args:
Expand All @@ -245,6 +264,7 @@ def decode(image, timeout=None, gap_size=None, shrink=1, shape=None,
corrections (int):
max_count (int): stop after reading this many barcodes. `None` to read
as many as possible.
return_vertices: If to return the coordinates of the four vertices of the datamatrix or just one + width/height
Returns:
:obj:`list` of :obj:`Decoded`: The values decoded from barcodes.
Expand Down Expand Up @@ -289,7 +309,7 @@ def decode(image, timeout=None, gap_size=None, shrink=1, shape=None,
else:
# Decoded
res = _decode_region(
decoder, region, corrections, shrink
decoder, region, corrections, shrink, return_vertices
)
if res:
results.append(res)
Expand Down
8 changes: 4 additions & 4 deletions pylibdmtx/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
c_ulonglong, c_char_p, Structure, CFUNCTYPE, POINTER
)
from enum import IntEnum, unique
from distutils.version import LooseVersion
from packaging import version

from . import dmtx_library

Expand Down Expand Up @@ -196,7 +196,7 @@ class DmtxSymbolSize(IntEnum):


# Structs
if LooseVersion(dmtxVersion()) < LooseVersion('0.7.5'):
if version.parse(dmtxVersion()) < version.parse('0.7.5'):
class DmtxMessage(Structure):
_fields_ = [
('arraySize', c_size_t),
Expand Down Expand Up @@ -310,7 +310,7 @@ class DmtxScanGrid(Structure):
]


if LooseVersion(dmtxVersion()) < LooseVersion('0.7.5'):
if version.parse(dmtxVersion()) < version.parse('0.7.5'):
class DmtxDecode(Structure):
_fields_ = [
('edgeMin', c_int),
Expand Down Expand Up @@ -398,7 +398,7 @@ class DmtxRegion(Structure):
]


if LooseVersion(dmtxVersion()) < LooseVersion('0.7.5'):
if version.parse(dmtxVersion()) < version.parse('0.7.5'):
class DmtxEncode(Structure):
_fields_ = [
('method', c_int),
Expand Down

0 comments on commit fb21878

Please sign in to comment.