-
Notifications
You must be signed in to change notification settings - Fork 59
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
BUG: Rect object returned by decode
can be trivially wrong
#51
Comments
Jeez, why they didn't just return the rectangles like in the lib? That would have been so much easier. I think now I'll have to make my own implementation of the lib... |
yes you can do by easily adding two more vectors e.g I did its in my lib and it works well |
Thanks a ton, this works. Couldn't this be implemented with an optional activation in the decode function? |
hi, |
Is there a reason why this bug is still present, 2-3 years after this issue was created? @gauti1311's solution works for me. @rehman922 you have to alter the pylibdmtx/pylibdmtx.py
|
Maybe because there's no pull request on this issue(?) |
Solved! |
How did you solve it? |
@GenericArt Not sure what @Gennadynemchin 's actual issue was, but I found that the when using @gauti1311 's solution to return 4 corners from the
|
Ah, I thought an update to the code had been implemented. Nevermind. |
You are right - I flipped the image. That's the only solution I've found `while camera.IsGrabbing(): do somethingcv2.imshow('dm', img_flip)` |
@Gennadynemchin Usually this happens if two libraries define the origin differently, e.g. OpenCV sets it in top-left and perhaps this library returns coordinates with origin in bottom-left. You then need to invert the Y-axis (subtracting the coordinate from the height of the image) to get the same coordinates. |
Few months late to this but I recently needed this functionality, strange how they haven't merged it in yet. I didn't want to modify the actual library so here is my implementation, thanks to @jepperaskdk and @amackp: from pylibdmtx.pylibdmtx import _region, _decoder, _image, _pixel_data, _decoded_matrix_region
from pylibdmtx.wrapper import c_ubyte_p, DmtxPackOrder, DmtxVector2, dmtxMatrix3VMultiplyBy, DmtxUndefined
from ctypes import cast, string_at
from collections import namedtuple
import numpy
_pack_order = {
8: DmtxPackOrder.DmtxPack8bppK,
16: DmtxPackOrder.DmtxPack16bppRGB,
24: DmtxPackOrder.DmtxPack24bppRGB,
32: DmtxPackOrder.DmtxPack32bppRGBX,
}
Decoded = namedtuple('Decoded', 'data rect')
def decode_with_region(image):
results = []
pixels, width, height, bpp = _pixel_data(image)
with _image(cast(pixels, c_ubyte_p), width, height, _pack_order[bpp]) as img:
with _decoder(img, 1) as decoder:
while True:
with _region(decoder, None) as region:
if not region:
break
else:
res = _decode_region(decoder, region)
if res:
open_cv_image = numpy.array(image)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
height, width, _ = open_cv_image.shape
topLeft = (res.rect['01']['x'], height - res.rect['01']['y'])
topRight = (res.rect['11']['x'], height - res.rect['11']['y'])
bottomRight = (res.rect['10']['x'], height - res.rect['10']['y'])
bottomLeft = (res.rect['00']['x'], height - res.rect['00']['y'])
results.append(Decoded(res.data, (topLeft, topRight, bottomRight, bottomLeft)))
return results
def _decode_region(decoder, region):
with _decoded_matrix_region(decoder, region, DmtxUndefined) as msg:
if msg:
vector00 = DmtxVector2()
vector11 = DmtxVector2(1.0, 1.0)
vector10 = DmtxVector2(1.0, 0.0)
vector01 = DmtxVector2(0.0, 1.0)
dmtxMatrix3VMultiplyBy(vector00, region.contents.fit2raw)
dmtxMatrix3VMultiplyBy(vector11, region.contents.fit2raw)
dmtxMatrix3VMultiplyBy(vector01, region.contents.fit2raw)
dmtxMatrix3VMultiplyBy(vector10, region.contents.fit2raw)
return Decoded(
string_at(msg.contents.output),
{
'00': {
'x': int((vector00.X) + 0.5),
'y': int((vector00.Y) + 0.5)
},
'01': {
'x': int((vector01.X) + 0.5),
'y': int((vector01.Y) + 0.5)
},
'10': {
'x': int((vector10.X) + 0.5),
'y': int((vector10.Y) + 0.5)
},
'11': {
'x': int((vector11.X) + 0.5),
'y': int((vector11.Y) + 0.5)
}
}
)
else:
return None |
The rectangle object returned by the
decode
function is (completely) wrong whenever the found DataMatrix code is rotated. The problem is that the returned rectangle is always supposed to be upright when, in fact, libdtmx supports also rotated codes.Examples with one upright and one rotated DataMatrix code:
dmtxread
on the command linepylibdtmx
(Note how also
width
andheight
changed to totally incorrect values.)The obvious solution would be to not return a tuple of
left
,top
,width
, andheight
coordinates, since they apply only to upright rectangles, but to return the coordinates of the four corners---as is done by the command line program.So in these lines of the code all four points should be captured instead of only two ones.
Of course, such a change would break the interface as it is now.
If you insist on returning an upright rectangle, it should at least be the upright bounding box of the DataMatrix code crop. But I would much prefer if the python version would just return the four corner points as the command line version does.
The text was updated successfully, but these errors were encountered: