コード例 #1
0
    def remove_intersected_regions(self, img_text, ccs_non_text):
        img_text = img_text.copy()
        ccs_non_text = ccs_non_text.copy()
        kernel = np.ones((3, 1), np.uint8)
        # dilation = cv.dilate(img_text, kernel)
        closing = cv.morphologyEx(img_text, cv.MORPH_CLOSE, kernel, iterations=4)
        ccs_text = get_connected_components(closing, external=True)
        img_text = np.zeros(img_text.shape, np.uint8)
        img_non_text = np.zeros(img_text.shape, np.uint8)
        cv.drawContours(img_text, [cc.get_contour() for cc in ccs_text], -1, 255, -1)
        cv.drawContours(img_non_text, [cc.get_contour() for cc in ccs_non_text], -1, 255, -1)
        ccs_non_text = get_connected_components(img_non_text, external=True)
        ccs_text_new = []
        for cc_non_text in ccs_non_text.copy():
            for cc_text in ccs_text:
                # if cc_text.contains(cc_non_text) or does_intersect(self.__img_shape, cc_text, cc_non_text):
                # if includes(img_shape, cc_text, cc_non_text):
                # if cc_text.contains(cc_non_text):
                if cc_text.get_rect_area() > cc_non_text.get_rect_area() and \
                        intersection_percentage(cc_text, cc_non_text) >= 0.9:
                    x, y, w, h = cc_non_text.get_rect()
                    cv.rectangle(img_text, (x, y), (x + w, y + h), 255, -1)
                    ccs_text_new.append(cc_non_text)
                    ccs_non_text.remove(cc_non_text)
                    break

        ccs_non_text_new = []
        for cc_text in ccs_text:
            for cc_non_text in ccs_non_text:
                # if cc_non_text.contains(cc_text) and cc_non_text.get_dens() > 0.02:
                if cc_non_text.get_rect_area() > cc_text.get_rect_area() and \
                        intersection_percentage(cc_non_text, cc_text) >= 0.9 and cc_non_text.get_dens() > 0.02:
                    x, y, w, h = cc_text.get_rect()
                    cv.rectangle(img_text, (x, y), (x + w, y + h), 0, -1)
                    ccs_non_text_new.append(cc_text)
                    break

        ccs_non_text.extend(ccs_non_text_new)

        if self.__debug:
            iu.show_and_wait('Intersections Grouping', img_text)
        kernel = np.ones((3, 3), np.uint8)
        img_text = cv.morphologyEx(img_text, cv.MORPH_CLOSE, kernel, iterations=4)
        if self.__debug:
            iu.show_and_wait('Intersections Grouping (Morph-Close)', img_text)
        ccs_text = get_connected_components(img_text, external=True)
        return ccs_text, ccs_non_text, ccs_text_new, ccs_non_text_new
コード例 #2
0
 def __init__(self, rect, img, t_var=T_VAR, debug=False):
     super().__init__()
     self.__rect = rect
     self.__root_img = img.copy()
     self.__img = self.__crop_image(img.copy())
     self.__t_var = t_var
     self.__debug = debug
     self.__set_all_attrs()
     self.__ccs = get_connected_components(self.__img, (rect[0], rect[1]))
     self.__hcs = set_ccs_neighbors(self.__ccs)
     self.__ccs = [cc for hc in self.__hcs for cc in hc]
コード例 #3
0
 def refine_non_text_regions(self, img_shape, ccs_non_text):
     rect_ccs = []
     img_non_text = np.zeros(img_shape, np.uint8)
     img_rect_non_text = np.zeros(img_shape, np.uint8)
     cv.drawContours(img_non_text, [cc.get_contour() for cc in ccs_non_text], -1, 255, -1)
     for cc1 in ccs_non_text:
         if cc1.get_dens() <= 0.02:
             continue
         intersections_sum = 0
         areas_sum = 0
         for cc2 in ccs_non_text:
             if cc1 is cc2:
                 continue
             rects_intersection = intersection(cc1.get_rect(), cc2.get_rect())
             if len(rects_intersection) > 0:
                 areas_sum += cc2.get_rect_area()
                 intersections_sum += rects_intersection[2] * rects_intersection[3]
         if areas_sum > 0 and intersections_sum / areas_sum >= 0.9:
             x, y, w, h = cc1.get_rect()
             cv.rectangle(img_non_text, (x, y), (x + w, y + h), 0, -1)
             cv.rectangle(img_rect_non_text, (x, y), (x + w, y + h), 255, -1)
             rect_ccs.append(cc1)
     return get_connected_components(img_non_text), get_connected_components(img_rect_non_text, external=True)
コード例 #4
0
    def __smooth_region(hr: Region):
        img = hr.get_img()

        kernel_height = 3
        wl = [wl_i[3] for wl_i in hr.get_wl_h()]
        if len(wl) > 0 and np.max(wl) > 0:
            kernel_height = round(2 * np.percentile(wl, 75))

        kernel_width = 1
        # ws = hr.set_features().get_features()['ws']
        # if len(ws) > 0 and np.max(ws) > 0:
        #     kernel_width = round(2 * np.percentile(ws, 75))

        kernel = np.ones((kernel_height, kernel_width), np.uint8)
        # kernel = np.ones((5, 1), np.uint8)
        closing = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel, iterations=4)

        return get_connected_components(closing,
                                        (hr.get_rect()[0], hr.get_rect()[1]))
コード例 #5
0
 def __get_ccs(self):
     return get_connected_components(self.__img)