Skip to content

Commit

Permalink
Merge pull request #36 from NaturalHistoryMuseum/feature/35-resize-mi…
Browse files Browse the repository at this point in the history
…nimum

Feature/35 resize minimum
  • Loading branch information
quicklizard99 authored Dec 21, 2016
2 parents aef03d0 + 765ff36 commit d748522
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v0.1.13
- #35 resize strategy minimum

# v0.1.12
- #33 decode_barcodes csv error

Expand Down
31 changes: 23 additions & 8 deletions gouda/strategies/resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,38 @@ def _unsharpmask(img):
return cv2.addWeighted(img, 3, blur, -2, 0)


def resize(img, engine):

def resize(img, engine, minimum_pixels=10):
# Entire image at different fractions of original size

# Minimum number of pixel along each edge.
if minimum_pixels < 3:
raise ValueError('Invalid value for minimum_pixels: [{0}]'.format(
minimum_pixels
))

# TODO LH try more sharpening, equalisation, other stuff?
for sharpening in (0, 1, 2):
if sharpening > 0:
img = _unsharpmask(img)
for f in [round(x * 0.01, 2) for x in range(100, 0, -5)]:
height, width = img.shape[1], img.shape[0]
for factor in [round(x * 0.01, 2) for x in range(100, 0, -5)]:
msg = 'resize: scaling factor [{0}] sharpening [{1}]'
msg = msg.format(f, sharpening)
msg = msg.format(factor, sharpening)
debug_print(msg)
if 1 == f:
i = img
if 1 == factor:
resized = img
else:
# Resize from the original image
i = cv2.resize(img, (0, 0), fx=f, fy=f)
barcodes = engine(i)
# Resize from the original image, only if resized height and
# width are greater than the minimum.
# cv2.resize raises an error if either dimension is zero.
dsize = (int(round(height * factor)), int(round(width * factor)))
if dsize[0] >= minimum_pixels and dsize[1] >= minimum_pixels:
resized = cv2.resize(img, dsize)
else:
# No point in continuing to shrink
break
barcodes = engine(resized)
if barcodes:
return msg, barcodes

Expand Down
21 changes: 19 additions & 2 deletions gouda/tests/test_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ def test_1d_roi(self):

@unittest.skipUnless(DM_ENGINE,
'No available engine for reading Data Matrix codes')
@unittest.skipUnless(hasattr(cv2, 'createCLAHE'),
'Older cv2 without createCLAHE')
def test_dm_resize(self):
self._test_dm(resize)

Expand All @@ -90,6 +88,25 @@ def test_no_barcode_roi(self):
img = cv2.imread(str(TESTDATA / 'nobarcode.png'))
self.assertIsNone(roi(img, self.ONED_ENGINE))

@unittest.skipUnless(DM_ENGINE or ONED_ENGINE,
'No available engine')
def test_resize_minimum_value(self):
img = cv2.imread(str(TESTDATA / 'nobarcode.png'))
engine = self.DM_ENGINE if self.DM_ENGINE else self.ONED_ENGINE
self.assertRaises(ValueError, resize, img, engine, minimum_pixels=0)

@unittest.skipUnless(DM_ENGINE or ONED_ENGINE,
'No available engine')
def test_resize_tiny_image(self):
"The resize strategy is given a very small image with no barcode"
# Test that the resize strategy can cope with very small images without
# cv2.resize raising an error
img = cv2.imread(str(TESTDATA / 'nobarcode.png'))
img = img[0:10, 0:10]
engine = self.DM_ENGINE if self.DM_ENGINE else self.ONED_ENGINE
self.assertIsNone(resize(img, engine))
self.assertIsNone(resize(img, engine, minimum_pixels=3))


if __name__ == '__main__':
unittest.main()

1 comment on commit d748522

@reitellepa
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I'm a novice using GitHub and Python and all.
I would like to test your package for my work at the natural history museum of La Chaux-de-Fonds.
What's the easiest way ?
I mean, i downloaded the latest release but i don't really know where i should put it and where i should use the commande decode_barcodes.

Thank you in advance for your answer.

Please sign in to comment.