diff --git a/bob/bio/vein/preprocessor/crop.py b/bob/bio/vein/preprocessor/crop.py index 8b529e008bbfc4af7ec203cd33d2e4230c1adcfd..28b29b3271d8c61beb3ad3ba81987936be415b8d 100644 --- a/bob/bio/vein/preprocessor/crop.py +++ b/bob/bio/vein/preprocessor/crop.py @@ -43,7 +43,9 @@ class FixedCrop(Cropper): """Implements cropping using a fixed suppression of border pixels The defaults supress no lines from the image and returns an image like the - original. + original. If an :py:class:`..database.AnnotatedArray` is passed, then we also + check for its ``.metadata['roi']`` component and correct it so that annotated + RoI points are consistent on the cropped image. .. note:: @@ -97,7 +99,22 @@ class FixedCrop(Cropper): # this should work even if limits are zeros h, w = image.shape - return image[self.top:h-self.bottom, self.left:w-self.right] + retval = image[self.top:h-self.bottom, self.left:w-self.right] + + if hasattr(retval, 'metadata') and 'roi' in retval.metadata: + # adjust roi points to new cropping + retval = retval.copy() #don't override original + h, w = retval.shape + points = [] + for y, x in retval.metadata['roi']: + y = max(y-self.top, 0) #adjust + y = min(y, h-1) #verify it is not over the limits + x = max(x-self.left, 0) #adjust + x = min(x, w-1) #verify it is not over the limits + points.append((y,x)) + retval.metadata['roi'] = points + + return retval class NoCrop(FixedCrop): diff --git a/bob/bio/vein/tests/test.py b/bob/bio/vein/tests/test.py index 9703075304a1c1b57b1f7135035bda62b3b11781..40e28d8d99bba1a644be696987de45c7c62b4b6a 100644 --- a/bob/bio/vein/tests/test.py +++ b/bob/bio/vein/tests/test.py @@ -51,6 +51,26 @@ def test_cropping(): nose.tools.eq_(cropped.shape, (shape[0]-(top+bottom), shape[1]-(left+right))) nose.tools.eq_((test_image[top:-bottom,left:-right]-cropped).sum(), 0) + # tests metadata survives after cropping (and it is corrected) + from ..database import AnnotatedArray + annotations = [ + (top-2, left+2), #slightly above and to the right + (top+3, shape[1]-(right+1)+3), #slightly down and to the right + (shape[0]-(bottom+1)+4, shape[1]-(right+1)-2), + (shape[0]-(bottom+1)+1, left), + ] + annotated_image = AnnotatedArray(test_image, metadata=dict(roi=annotations)) + assert hasattr(annotated_image, 'metadata') + cropped = fixed_crop(annotated_image) + assert hasattr(cropped, 'metadata') + import ipdb; ipdb.set_trace() + assert numpy.allclose(cropped.metadata['roi'], [ + (0, 2), + (3, cropped.shape[1]-1), + (cropped.shape[0]-1, 4), + (cropped.shape[0]-1, 0), + ]) + def test_masking():