Exemplo n.º 1
0
 def test_gridify_generates_correct_sequence_of_borders(self):
     # get the test image which is constructed as follows:
     grid_image_path = path.join(this_path,
                                 'grid (tlbr padding - 1, 2, 3, 4).png')
     grid_image = cv2.imread(grid_image_path)
     self.assertIsNotNone(grid_image)  # just confirm file loaded
     # 4 rows, 2 columns
     dimensions = (4, 2)
     # (top, left, bottom, right) padding:
     #   = (1px, 2px, 3px, 4px)
     #   = (1/5, 2/10, 3/5, 4/10)
     #   = (0.2, 0.2, 0.6, 0.4)
     cell_padding = 0.2, 0.2, 0.6, 0.4
     #   ==> remaining cell image shape is 1 row, 4 columns (3 channels)
     channels = 3
     cell_shape_spec = (1, 4, channels)  # 1 row, 4 columns, 3 channels
     # ==> content of each cell image should be exactly the colored parts
     #     in the image as follows:
     red = (0, 0, 255)
     green = (0, 255, 0)
     blue = (255, 0, 0)
     white = (255, 255, 255)
     colors_spec = [[red, white],
                    [green, red],
                    [blue, green],
                    [white, blue]]
     colors_spec = numpy.asarray(colors_spec)
     # gridify the image and confirm all sub images match the above specs
     grid = Grid(dimensions, cell_padding)
     for grid_position, borders in grid.borders_by_grid_position(grid_image):
         top, left, bottom, right = borders
         # confirm the size
         cell_shape = (bottom - top, right - left, channels)
         self.assertEqual(cell_shape, cell_shape_spec)
         spec_pixel = colors_spec[grid_position]
         # confirm the color for each pixel in the cell
         for pixel_row in range(top, bottom):
             for pixel_col in range(left, right):
                 p = pixel_row, pixel_col
                 image_pixel = grid_image[p]
                 self.assertTrue(numpy.all(image_pixel == spec_pixel),
                                 'Pixel in grid cell:'
                                 '\n{}: {}'
                                 '\nunexpectedly not equal to specification:'
                                 '\n{}'.format(grid_position, image_pixel,
                                               spec_pixel))