finish morph

and add erode/dilate convenience functions to Vips.py
This commit is contained in:
John Cupitt 2015-01-28 21:58:08 +00:00
parent b2f23c853e
commit c384e2b664
3 changed files with 52 additions and 0 deletions

12
TODO
View File

@ -1,3 +1,15 @@
- add imageize from 2D arrays, eg. we should be able to write
im = im.erode([[128, 255, 128],
[255, 255, 255],
[128, 255, 128]])
see test_morphology.py
- add median convenience function, see test_morphology.py
- use vips_resize() in vipsthumbnail?
should the sharpening filter be selectable?

View File

@ -866,6 +866,14 @@ class Image(Vips.Image):
"""Return 10 ** pixel."""
return self.math(Vips.OperationMath.EXP10)
def erode(self, mask):
"""Erode with a structuring element."""
return self.morph(mask, Vips.OperationMorphology.ERODE)
def dilate(self, mask):
"""Dilate with a structuring element."""
return self.morph(mask, Vips.OperationMorphology.DILATE)
# we need different imageize rules for this operator ... we need to
# imageize th and el to match each other first
@add_doc(generate_docstring("ifthenelse"))

View File

@ -33,6 +33,11 @@ class TestMorphology(unittest.TestCase):
for x, y in zip_expand(a, b):
self.assertAlmostEqual(x, y, places = places, msg = msg)
def setUp(self):
self.mask = Vips.Image.new_from_array([[128, 255, 128],
[255, 255, 255],
[128, 255, 128]])
def test_countlines(self):
im = Vips.Image.black(100, 100)
im = im.draw_line(255, 0, 50, 100, 50)
@ -47,5 +52,32 @@ class TestMorphology(unittest.TestCase):
self.assertEqual(opts['segments'], 3)
self.assertEqual(mask.max(), 2)
def test_erode(self):
im = Vips.Image.black(100, 100)
im = im.draw_circle(255, 50, 50, 25, fill = True)
im2 = im.erode(self.mask)
self.assertEqual(im.width, im2.width)
self.assertEqual(im.height, im2.height)
self.assertEqual(im.bands, im2.bands)
self.assertTrue(im.avg() > im2.avg())
def test_dilate(self):
im = Vips.Image.black(100, 100)
im = im.draw_circle(255, 50, 50, 25, fill = True)
im2 = im.dilate(self.mask)
self.assertEqual(im.width, im2.width)
self.assertEqual(im.height, im2.height)
self.assertEqual(im.bands, im2.bands)
self.assertTrue(im2.avg() > im.avg())
def test_rank(self):
im = Vips.Image.black(100, 100)
im = im.draw_circle(255, 50, 50, 25, fill = True)
im2 = im.rank(3, 3, 8)
self.assertEqual(im.width, im2.width)
self.assertEqual(im.height, im2.height)
self.assertEqual(im.bands, im2.bands)
self.assertTrue(im2.avg() > im.avg())
if __name__ == '__main__':
unittest.main()