Ejemplo n.º 1
0
 def _treat_border(self, config):
     """
     TODO(kisuk): Documentation.
     """
     border_func = self.params.get('border', None)
     if border_func is not None:
         if border_func['type'] is 'mirror_border':
             for _, data in config.items('dataset'):
                 # Apply only to images.
                 if not 'image' in data:
                     continue
                 assert config.has_section(data)
                 assert config.has_option(data, 'offset')
                 offset = config.get(data, 'offset')
                 # Append border mirroring to the preprocessing list.
                 if config.has_option(data, 'preprocess'):
                     pp = config.get(data, 'Preprocess') + '\n'
                 else:
                     pp = ''
                 pp += str(border_func)
                 config.set(data, 'preprocess', pp)
                 # Update offset.
                 fov = border_func['fov']
                 offset = Vec3d(offset) - Vec3d(fov)/2
                 config.set(data, 'offset', tuple(offset))
         else:
             msg = 'unknown border mode [%s].' % border_func['type']
             raise RuntimeError(msg)
Ejemplo n.º 2
0
 def testIntersect(self):
     b1 = Box(Vec3d(1, 1, 1), [2, 2, 2])
     b2 = Box(Vec3d(3, 3, 3), [2, 2, 2])
     self.assertTrue(b1.intersect(b2) == None)
     b2.translate((-0.5, -0.5, -0.5))
     b3 = b1.intersect(b2)
     self.assertTrue(b3 == Box((1.5, 1.5, 1.5), (2, 2, 2)))
Ejemplo n.º 3
0
 def testMerge(self):
     b1 = Box(Vec3d(1, 1, 1), [2, 2, 2])
     b2 = Box(Vec3d(3, 3, 3), [2, 2, 2])
     b3 = b1.merge(b2)
     self.assertTrue(b3 == Box((1, 1, 1), (3, 3, 3)))
     b4 = b3.merge(Box((0, 0, 0), (-1, -1, -1)))
     self.assertTrue(b4 == Box((-1, -1, -1), (3, 3, 3)))
Ejemplo n.º 4
0
 def testOverlaps(self):
     b1 = Box(Vec3d(1, 1, 1), [2, 2, 2])
     b2 = Box(Vec3d(3, 3, 3), [2, 2, 2])
     self.assertTrue(b1.overlaps(b1) and b2.overlaps(b2))
     self.assertTrue((not b1.overlaps(b2)) and (not b2.overlaps(b1)))
     b2.translate((-0.5, -0.5, -0.5))
     self.assertTrue(b1.overlaps(b2) and b2.overlaps(b1))
Ejemplo n.º 5
0
 def __call__(self, data, offset=(0, 0, 0), size=None):
     data = check_tensor(data)
     if size is None:
         size = tuple(Vec3d(data.shape[-3:]) - Vec3d(offset))
     ret = np.zeros((data.shape[-4], ) + size, data.dtype)
     v1 = Vec3d(offset)
     v2 = v1 + Vec3d(size)
     ret[...] = data[..., v1[0]:v2[0], v1[1]:v2[1], v1[2]:v2[2]]
     return ret
Ejemplo n.º 6
0
def centered_box(c, s):
    """Return a box of size s centered on c."""
    center = Vec3d(c)
    size = Vec3d(s)
    half = size / 2
    assert size.x >= 0 and size.y >= 0 and size.z >= 0
    v1 = center - half
    v2 = v1 + size
    return Box(v1, v2)
Ejemplo n.º 7
0
def crop(img, offset=(0, 0, 0), size=None):
    """
    TODO(kisuk): Documentation.
    """
    img = check_volume(img)
    if size is None:
        size = tuple(Vec3d(img.shape) - Vec3d(offset))
    ret = np.zeros(size, dtype=img.dtype)
    v1 = Vec3d(offset)
    v2 = v1 + Vec3d(size)
    ret[:] = img[v1[0]:v2[0], v1[1]:v2[1], v1[2]:v2[2]]
    return ret
Ejemplo n.º 8
0
 def testCreationAndAccess(self):
     b = Box((1, 1, 1), [2, 2, 2])
     self.assertTrue(b.min() == Vec3d(1, 1, 1))
     self.assertTrue(b.max() == Vec3d(2, 2, 2))
     self.assertTrue(b.size() == Vec3d(1, 1, 1))
     b = Box((1, 0, 1), [0, 1, 0])
     self.assertTrue(b.min() == Vec3d(0, 0, 0))
     self.assertTrue(b.max() == Vec3d(1, 1, 1))
     self.assertTrue(b.size() == Vec3d(1, 1, 1))
     b1 = Box(b)
     self.assertTrue(b1.min() == Vec3d(0, 0, 0))
     self.assertTrue(b1.max() == Vec3d(1, 1, 1))
     self.assertTrue(b1.size() == Vec3d(1, 1, 1))
Ejemplo n.º 9
0
 def _random_location(self):
     """Return one of the valid locations randomly."""
     s = self._range.size()
     z = np.random.randint(0, s[0])
     y = np.random.randint(0, s[1])
     x = np.random.randint(0, s[2])
     # Global coordinate system.
     return Vec3d(z, y, x) + self._range.min()
Ejemplo n.º 10
0
def mirror_border(img, fov):
    """
    TODO(kisuk) Documentation.
    """
    img = check_volume(img)

    # Validate FoV
    fov = np.asarray(fov)
    fov = Vec3d(fov.astype('uint32'))

    # Pad size
    top = fov / 2
    btm = fov - top - (1, 1, 1)
    pad_with = [(top[0], btm[0]), (top[1], btm[1]), (top[2], btm[2])]
    # TODO(kisuk): Should we force an odd-sized fov?

    # TODO(kisuk): 'symmetric' or 'reflect'?
    return np.pad(img, pad_with, mode='reflect')
Ejemplo n.º 11
0
 def max(self):
     """Return the max corner as a vector."""
     return Vec3d(self._max)
Ejemplo n.º 12
0
 def min(self):
     """Return the min corner as a vector."""
     return Vec3d(self._min)
Ejemplo n.º 13
0
 def contains(self, v):
     """Return true if a point is inside the box."""
     (x, y, z) = Vec3d(v)
     return (self._min.x <= x < self._max.x
             and self._min.y <= y < self._max.y
             and self._min.z <= z < self._max.z)
Ejemplo n.º 14
0
 def size(self):
     return Vec3d(self._size)
Ejemplo n.º 15
0
 def __init__(self, v1_or_box, v2=None):
     """Initialize a box from another box or two vectors."""
     if v2 == None:
         self.set_coords(v1_or_box.min(), v1_or_box.max())
     else:
         self.set_coords(Vec3d(v1_or_box), Vec3d(v2))
Ejemplo n.º 16
0
 def testExpand(self):
     b1 = Box(Vec3d(1, 1, 1), [2, 2, 2])
     b1.expand_by(1)
     self.assertTrue(b1 == Box((0, 0, 0), (3, 3, 3)))
     b1.expand_by((-1, 0, 0))
     self.assertTrue(b1 == Box((1, 0, 0), (2, 3, 3)))
Ejemplo n.º 17
0
                    self.rot, self.shear, self.scale, self.stretch, self.twist)
            sample[k] = np.transpose(v, (1,0,2,3))
        return sample


if __name__ == "__main__":

    # Fov.
    spec = dict()
    spec['input/p3'] = (5,109,109)
    spec['input/p2'] = (7,73,73)
    spec['input/p1'] = (9,45,45)
    spec['label']    = (3,1,1,1)

    # In/out size.
    outsz = Vec3d(5,100,100)
    for k, v in spec.iteritems():
        newv = tuple(Vec3d(v[-3:]) + outsz - Vec3d(1,1,1))
        spec[k] = v[:-3] + newv

    # Augmentation.
    aug = WarpAugment()

    # Test.
    ret = aug.prepare(spec, imgs=['input/p3','input/p2','input/p1'])
    print ret
    print aug.spec
    print aug.rot
    print aug.shear
    print aug.scale
    print aug.stretch
Ejemplo n.º 18
0
 def testContains(self):
     b = Box(Vec3d(1, 1, 1), [3, 3, 3])
     self.assertTrue(b.contains(b.min()))
     self.assertTrue(not b.contains(b.max()))
     self.assertTrue(b.contains((2, 2, 2)))
     self.assertTrue(not b.contains((-2, -2, -2)))