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
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))
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()
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')
def max(self): """Return the max corner as a vector.""" return Vec3d(self._max)
def min(self): """Return the min corner as a vector.""" return Vec3d(self._min)
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)
def size(self): return Vec3d(self._size)
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))
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)))
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)))
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