Пример #1
0
 def testMultipleImages(self):
   images = [[[1, 1, 1, 1],
              [1, 0, 0, 1],
              [1, 0, 0, 1],
              [1, 1, 1, 1]],
             [[1, 0, 0, 1],
              [0, 0, 0, 0],
              [0, 0, 0, 0],
              [1, 0, 0, 1]],
             [[1, 1, 0, 1],
              [0, 1, 1, 0],
              [1, 0, 1, 0],
              [0, 0, 1, 1]]]  # pyformat: disable
   expected = [[[1, 1, 1, 1],
                [1, 0, 0, 1],
                [1, 0, 0, 1],
                [1, 1, 1, 1]],
               [[2, 0, 0, 3],
                [0, 0, 0, 0],
                [0, 0, 0, 0],
                [4, 0, 0, 5]],
               [[6, 6, 0, 7],
                [0, 6, 6, 0],
                [8, 0, 6, 0],
                [0, 0, 6, 6]]]  # pyformat: disable
   with self.test_session():
     self.assertAllEqual(
         image_ops.connected_components(math_ops.cast(
             images, dtypes.bool)).eval(), expected)
Пример #2
0
 def testMultipleImages(self):
   images = [[[1, 1, 1, 1],
              [1, 0, 0, 1],
              [1, 0, 0, 1],
              [1, 1, 1, 1]],
             [[1, 0, 0, 1],
              [0, 0, 0, 0],
              [0, 0, 0, 0],
              [1, 0, 0, 1]],
             [[1, 1, 0, 1],
              [0, 1, 1, 0],
              [1, 0, 1, 0],
              [0, 0, 1, 1]]]  # pyformat: disable
   expected = [[[1, 1, 1, 1],
                [1, 0, 0, 1],
                [1, 0, 0, 1],
                [1, 1, 1, 1]],
               [[2, 0, 0, 3],
                [0, 0, 0, 0],
                [0, 0, 0, 0],
                [4, 0, 0, 5]],
               [[6, 6, 0, 7],
                [0, 6, 6, 0],
                [8, 0, 6, 0],
                [0, 0, 6, 6]]]  # pyformat: disable
   with self.cached_session():
     self.assertAllEqual(
         image_ops.connected_components(math_ops.cast(
             images, dtypes.bool)).eval(), expected)
Пример #3
0
 def testSimple(self):
   arr = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
   with self.test_session():
     # Single component with id 1.
     self.assertAllEqual(
         image_ops.connected_components(math_ops.cast(
             arr, dtypes.bool)).eval(), arr)
Пример #4
0
 def testSimple(self):
   arr = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
   with self.cached_session():
     # Single component with id 1.
     self.assertAllEqual(
         image_ops.connected_components(math_ops.cast(
             arr, dtypes.bool)).eval(), arr)
Пример #5
0
 def testRandom_scipy(self):
   np.random.seed(42)
   images = np.random.randint(0, 2, size=(10, 100, 200)).astype(np.bool)
   expected = connected_components_reference_implementation(images)
   if expected is None:
     return
   with self.cached_session():
     self.assertAllEqual(
         image_ops.connected_components(images).eval(), expected)
Пример #6
0
 def testRandom_scipy(self):
   np.random.seed(42)
   images = np.random.randint(0, 2, size=(10, 100, 200)).astype(np.bool)
   expected = connected_components_reference_implementation(images)
   if expected is None:
     return
   with self.test_session():
     self.assertAllEqual(
         image_ops.connected_components(images).eval(), expected)
 def testDisconnected(self):
     arr = math_ops.cast(
         [[1, 0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0, 1, 0],
          [1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0],
          [0, 0, 1, 0, 0, 0, 0, 0, 0]], dtypes.bool)  # pyformat: disable
     expected = ([[1, 0, 0, 2, 0, 0, 0, 0, 3], [0, 4, 0, 0, 0, 5, 0, 6, 0],
                  [7, 0, 8, 0, 0, 0, 9, 0, 0], [0, 0, 0, 0, 10, 0, 0, 0, 0],
                  [0, 0, 11, 0, 0, 0, 0, 0, 0]])  # pyformat: disable
     with self.test_session():
         self.assertAllEqual(
             image_ops.connected_components(arr).eval(), expected)
Пример #8
0
 def testDisconnected(self):
   arr = math_ops.cast(
       [[1, 0, 0, 1, 0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0, 1, 0, 1, 0],
        [1, 0, 1, 0, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0]],
       dtypes.bool)  # pyformat: disable
   expected = (
       [[1, 0, 0, 2, 0, 0, 0, 0, 3],
        [0, 4, 0, 0, 0, 5, 0, 6, 0],
        [7, 0, 8, 0, 0, 0, 9, 0, 0],
        [0, 0, 0, 0, 10, 0, 0, 0, 0],
        [0, 0, 11, 0, 0, 0, 0, 0, 0]])  # pyformat: disable
   with self.test_session():
     self.assertAllEqual(image_ops.connected_components(arr).eval(), expected)
Пример #9
0
 def testSnake_disconnected(self):
   for i in range(SNAKE.shape[0]):
     for j in range(SNAKE.shape[1]):
       with self.cached_session():
         # If we disconnect any part of the snake except for the endpoints,
         # there will be 2 components.
         if SNAKE[i, j] and (i, j) not in [(1, 1), (6, 3)]:
           disconnected_snake = SNAKE.copy()
           disconnected_snake[i, j] = 0
           components = image_ops.connected_components(
               math_ops.cast(disconnected_snake, dtypes.bool)).eval()
           self.assertEqual(components.max(), 2, 'disconnect (%d, %d)' % (i,
                                                                          j))
           bins = np.bincount(components.ravel())
           # Nonzero number of pixels labeled 0, 1, or 2.
           self.assertGreater(bins[0], 0)
           self.assertGreater(bins[1], 0)
           self.assertGreater(bins[2], 0)
Пример #10
0
 def testSnake_disconnected(self):
   for i in range(SNAKE.shape[0]):
     for j in range(SNAKE.shape[1]):
       with self.test_session():
         # If we disconnect any part of the snake except for the endpoints,
         # there will be 2 components.
         if SNAKE[i, j] and (i, j) not in [(1, 1), (6, 3)]:
           disconnected_snake = SNAKE.copy()
           disconnected_snake[i, j] = 0
           components = image_ops.connected_components(
               math_ops.cast(disconnected_snake, dtypes.bool)).eval()
           self.assertEqual(components.max(), 2, 'disconnect (%d, %d)' % (i,
                                                                          j))
           bins = np.bincount(components.ravel())
           # Nonzero number of pixels labeled 0, 1, or 2.
           self.assertGreater(bins[0], 0)
           self.assertGreater(bins[1], 0)
           self.assertGreater(bins[2], 0)
Пример #11
0
 def testSnake(self):
   with self.cached_session():
     # Single component with id 1.
     self.assertAllEqual(
         image_ops.connected_components(math_ops.cast(
             SNAKE, dtypes.bool)).eval(), SNAKE)
Пример #12
0
 def testOnes_small(self):
   with self.cached_session():
     self.assertAllEqual(
         image_ops.connected_components(array_ops.ones((3, 5),
                                                       dtypes.bool)).eval(),
         np.ones((3, 5)))
Пример #13
0
 def testOnes(self):
   with self.cached_session():
     self.assertAllEqual(
         image_ops.connected_components(
             array_ops.ones((100, 20, 50), dtypes.bool)).eval(),
         np.tile(np.arange(100)[:, None, None] + 1, [1, 20, 50]))
Пример #14
0
 def testZeros(self):
   with self.cached_session():
     self.assertAllEqual(
         image_ops.connected_components(
             array_ops.zeros((100, 20, 50), dtypes.bool)).eval(),
         np.zeros((100, 20, 50)))
Пример #15
0
 def testZeros(self):
   with self.test_session():
     self.assertAllEqual(
         image_ops.connected_components(
             array_ops.zeros((100, 20, 50), dtypes.bool)).eval(),
         np.zeros((100, 20, 50)))
Пример #16
0
 def testSnake(self):
   with self.test_session():
     # Single component with id 1.
     self.assertAllEqual(
         image_ops.connected_components(math_ops.cast(
             SNAKE, dtypes.bool)).eval(), SNAKE)
Пример #17
0
 def testOnes_small(self):
   with self.test_session():
     self.assertAllEqual(
         image_ops.connected_components(array_ops.ones((3, 5),
                                                       dtypes.bool)).eval(),
         np.ones((3, 5)))
Пример #18
0
 def testOnes(self):
   with self.test_session():
     self.assertAllEqual(
         image_ops.connected_components(
             array_ops.ones((100, 20, 50), dtypes.bool)).eval(),
         np.tile(np.arange(100)[:, None, None] + 1, [1, 20, 50]))