Пример #1
0
class TestRectangleMerger(TestCase):
    rect_a = Rectangle.from_2_pos(1, 1, 10, 5)
    rect_b = Rectangle.from_2_pos(11, 1.2, 13, 5.2)
    rect_c = Rectangle.from_2_pos(2, 3, 20, 10)
    merger = Merger()

    def test__merge_rectangle_to_pool(self):
        self.assertTrue(
            self.merger._is_rectangles_overlapped(self.rect_a, self.rect_c))
        self.assertFalse(
            self.merger._is_rectangles_overlapped(self.rect_a, self.rect_b))

    def test__merge_2_rectangles(self):
        rect = self.merger._merge_2_rectangles(self.rect_a, self.rect_c)
        expect_rect = Rectangle.from_2_pos(1, 1, 20, 10)
        self.assertEqual(rect, expect_rect,
                         "%s and %s not equal!" % (rect, expect_rect))

    def test__merge_all_rectangles(self):
        rects = [self.rect_a, self.rect_b, self.rect_c]
        merged_rects = self.merger.merge_rectangle_list(rects)
        expect_rects = [
            Rectangle.from_2_pos(1, 1, 13, 5.2),
            Rectangle.from_2_pos(2, 3, 20, 10)
        ]
        self.assertListEqual(merged_rects, expect_rects,
                             " ".join([r.__str__() for r in merged_rects]))
 def test__select_most_merged_rectangles(self):
     top_rects = self.selector.select_according_to_merged_times(self.rects)
     self.assertEquals(top_rects[0], Rectangle(0, 0, 3, 2),
                       "index %d should not be %s" % (0, top_rects[0]))
     self.assertEquals(top_rects[1], Rectangle(0, 0, 6, 5),
                       "index %d should not be %s" % (1, top_rects[1]))
     self.assertEquals(top_rects[2], Rectangle(0, 0, 4, 3),
                       "index %d should not be %s" % (2, top_rects[2]))
Пример #3
0
 def test__merge_all_rectangles(self):
     rects = [self.rect_a, self.rect_b, self.rect_c]
     merged_rects = self.merger.merge_rectangle_list(rects)
     expect_rects = [
         Rectangle.from_2_pos(1, 1, 13, 5.2),
         Rectangle.from_2_pos(2, 3, 20, 10)
     ]
     self.assertListEqual(merged_rects, expect_rects,
                          " ".join([r.__str__() for r in merged_rects]))
 def test__select_fat_and_large_rectangles(self):
     top_rects = self.selector.select_fat_and_large_rectangles(self.rects)
     self.assertEquals(top_rects[0], Rectangle(0, 0, 9, 1),
                       "index %d should not be %s" % (0, top_rects[0]))
     self.assertEquals(top_rects[1], Rectangle(0, 0, 8, 2),
                       "index %d should not be %s" % (1, top_rects[1]))
     self.assertEquals(top_rects[2], Rectangle(0, 0, 5, 4),
                       "index %d should not be %s" % (2, top_rects[2]))
     self.assertEquals(top_rects[3], Rectangle(0, 0, 6, 5),
                       "index %d should not be %s" % (3, top_rects[3]))
Пример #5
0
 def _merge_2_rectangles(self, rect_1, rect_2):
     return Rectangle.from_2_pos(
         rect_1.x1 < rect_2.x1 and rect_1.x1 or rect_2.x1,
         rect_1.y1 < rect_2.y1 and rect_1.y1 or rect_2.y1,
         rect_1.x2 > rect_2.x2 and rect_1.x2 or rect_2.x2,
         rect_1.y2 > rect_2.y2 and rect_1.y2 or rect_2.y2,
         rect_1.merged_times + rect_2.merged_times + 1)
Пример #6
0
 def _save_region_of_interest_with_margin(self, index, img, rect):
     max_x = len(img[0]) - 1
     max_y = len(img) - 1
     margin_x = int((rect.x2 - rect.x1) * self.width_margin_percent)
     margin_y = int((rect.y2 - rect.y1) * self.height_margin_percent)
     r = Rectangle.from_2_pos(
         rect.x1 - margin_x >= 0 and (rect.x1 - margin_x) or 0,
         rect.y1 - margin_y >= 0 and (rect.y1 - margin_y) or 0,
         rect.x2 + margin_x <= max_x and (rect.x2 + margin_x) or max_x,
         rect.y2 + margin_y <= max_y and (rect.y2 + margin_y) or max_y)
     roi = img[r.y1:r.y2, r.x1:r.x2]
     cv2.imwrite("%spart-%02d.jpg" % (self.output_path, index), roi)
Пример #7
0
 def find_all_text_rectangles(self, img):
     # Extract channels to be processed individually
     channels = cv2.text.computeNMChannels(img)
     # Append negative channels to detect ER- (bright regions over dark background)
     cn = len(channels) - 1
     for c in range(0, cn):
         channels.append((255 - channels[c]))
     # Apply the default cascade classifier to each independent channel (could be done in parallel)
     rectangles = []
     for channel in channels:
         for s in range(0, len(self.detection_parameter_list)):
             rects = self._text_detect(img, channel, s)
             for r in range(0, np.shape(rects)[0]):
                 rectangles.append(Rectangle(rects[r]).set_area_size_index(s))
     return rectangles
class TestRectangleSelector(TestCase):
    selector = RectangleSelector()
    rects = [
        Rectangle(0, 0, 6, 9, 0),
        Rectangle(0, 0, 4, 3, 1),
        Rectangle(0, 0, 9, 1, 0),
        Rectangle(0, 0, 1, 9, 1),
        Rectangle(0, 0, 5, 4, 0),
        Rectangle(0, 0, 8, 2, 0),
        Rectangle(0, 0, 1, 9, 3),
        Rectangle(0, 0, 7, 2, 0),
        Rectangle(0, 0, 6, 5, 2),
        Rectangle(0, 0, 6, 2, 0),
        Rectangle(0, 0, 3, 2, 3)
    ]

    def test__select_fat_and_large_rectangles(self):
        top_rects = self.selector.select_fat_and_large_rectangles(self.rects)
        self.assertEquals(top_rects[0], Rectangle(0, 0, 9, 1),
                          "index %d should not be %s" % (0, top_rects[0]))
        self.assertEquals(top_rects[1], Rectangle(0, 0, 8, 2),
                          "index %d should not be %s" % (1, top_rects[1]))
        self.assertEquals(top_rects[2], Rectangle(0, 0, 5, 4),
                          "index %d should not be %s" % (2, top_rects[2]))
        self.assertEquals(top_rects[3], Rectangle(0, 0, 6, 5),
                          "index %d should not be %s" % (3, top_rects[3]))

    def test__select_most_merged_rectangles(self):
        top_rects = self.selector.select_according_to_merged_times(self.rects)
        self.assertEquals(top_rects[0], Rectangle(0, 0, 3, 2),
                          "index %d should not be %s" % (0, top_rects[0]))
        self.assertEquals(top_rects[1], Rectangle(0, 0, 6, 5),
                          "index %d should not be %s" % (1, top_rects[1]))
        self.assertEquals(top_rects[2], Rectangle(0, 0, 4, 3),
                          "index %d should not be %s" % (2, top_rects[2]))
Пример #9
0
 def test__merge_2_rectangles(self):
     rect = self.merger._merge_2_rectangles(self.rect_a, self.rect_c)
     expect_rect = Rectangle.from_2_pos(1, 1, 20, 10)
     self.assertEqual(rect, expect_rect,
                      "%s and %s not equal!" % (rect, expect_rect))