Commit 19e4980d authored by André Anjos's avatar André Anjos 💬

Test cropping with annotated images

parent d3f2e266
...@@ -43,7 +43,9 @@ class FixedCrop(Cropper): ...@@ -43,7 +43,9 @@ class FixedCrop(Cropper):
"""Implements cropping using a fixed suppression of border pixels """Implements cropping using a fixed suppression of border pixels
The defaults supress no lines from the image and returns an image like the 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:: .. note::
...@@ -97,7 +99,22 @@ class FixedCrop(Cropper): ...@@ -97,7 +99,22 @@ class FixedCrop(Cropper):
# this should work even if limits are zeros # this should work even if limits are zeros
h, w = image.shape 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): class NoCrop(FixedCrop):
......
...@@ -51,6 +51,26 @@ def test_cropping(): ...@@ -51,6 +51,26 @@ def test_cropping():
nose.tools.eq_(cropped.shape, (shape[0]-(top+bottom), shape[1]-(left+right))) 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) 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(): def test_masking():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment