Пример #1
0
def get_filling_ratio(rectangle: Rectangle, mask: np.array):
    # compute the area of bboxes
    bbox_area = rectangle.get_area()
    whites = count_whites(rectangle, mask)

    # return the filling ratio
    return whites / bbox_area
Пример #2
0
def get_mask(mask: np.array) -> (np.array, List[Rectangle]):
    regions = []
    side = SIDE
    for _ in range(INTERMEDIATE_STEPS):
        kernel = np.empty((side, side))
        kernel.fill(1 / 255)

        conv_res = cv2.filter2D(mask,
                                cv2.CV_32F,
                                kernel,
                                anchor=(0, 0),
                                borderType=cv2.BORDER_CONSTANT)

        positions = conv_iter(conv_res, side)
        for pos in positions:
            rec = Rectangle(top_left=(pos[0], pos[1]), width=side, height=side)
            regions.append(rec)

        side = int(side / SHRINK_MULTIPLIER)
        if side % 2 == 0:
            side += 1

    regions = combine_overlapped_regions(regions)
    mask = clear_non_region_mask(mask, regions)

    return mask, regions
Пример #3
0
    def get_mask(self, mask: np.array):
        a, contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        for contour in contours:
            min_point = np.full(2, np.iinfo(np.int).max)
            max_point = np.zeros(2).astype(int)
            for point in contour:
                min_point[0] = min(min_point[0], int(point[0][1]))
                min_point[1] = min(min_point[1], int(point[0][0]))
                max_point[0] = max(max_point[0], int(point[0][1]))
                max_point[1] = max(max_point[1], int(point[0][0]))

            rectangle = Rectangle()
            rectangle.top_left = min_point.astype(int).tolist()
            rectangle.height = int(max_point[0] - min_point[0])
            rectangle.width = int(max_point[1] - min_point[1])

            fr = get_filling_ratio(rectangle, mask)
            ff = rectangle.get_form_factor()
            if (rectangle.get_area() < self.min_area or
                    fr < self.min_fill_ratio or
                    fr > self.max_fill_ratio or
                    ff < self.min_form_factor or
                    ff > self.max_form_factor):
                cv2.rectangle(mask, (min_point[1], min_point[0]), (max_point[1], max_point[0]), 0, thickness=cv2.FILLED)

        return mask
def main():
    composite1 = GraphicComposite('Composite-1')
    composite1.add_graphic(Rectangle('Rectangle-1'))
    composite1.add_graphic(Rectangle('Rectangle-2'))
    composite1.add_graphic(Circle('Circle-1'))

    composite2 = GraphicComposite('Composite-2')
    composite2.add_graphic(Rectangle('Rectangle-3'))
    composite2.add_graphic(Circle('Circle-2'))

    composite = GraphicComposite('Overall Composite')
    composite.add_graphic(composite1)
    composite.add_graphic(composite2)

    composite.draw()
    print()
    composite1.translate(x=10, y=5)
    print()
    composite2.resize(times=1.4)
Пример #5
0
def get_cc_regions(mask: np.array) -> List[Rectangle]:
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    regions = []
    for contour in contours:
        min_point = np.full(2, np.iinfo(np.int).max)
        max_point = np.zeros(2).astype(int)
        for point in contour:
            min_point[0] = min(min_point[0], int(point[0][1]))
            min_point[1] = min(min_point[1], int(point[0][0]))
            max_point[0] = max(max_point[0], int(point[0][1]))
            max_point[1] = max(max_point[1], int(point[0][0]))

        rectangle = Rectangle()
        rectangle.top_left = min_point.astype(int).tolist()
        rectangle.width = int(max_point[0] - min_point[0]) + 1
        rectangle.height = int(max_point[1] - min_point[1]) + 1
        if rectangle.height < mask.shape[0] and \
                rectangle.width < mask.shape[1]:
            regions.append(rectangle)

    return regions
Пример #6
0
    def get_sizes(self, data: List[Data]) -> (Dict[str, 'Template'], int):
        sign_type = {}
        total = 0
        for sample in data:
            for gt in sample.gt:
                if gt.type not in sign_type.keys():
                    sign_type[gt.type] = Template()

                sign_type[gt.type].signs.append(
                    Rectangle(top_left=gt.top_left,
                              width=gt.width,
                              height=gt.height))
            total += 1

        return sign_type, total
Пример #7
0
 def __init__(self, directory: str, name: str):
     self.name = name
     self.gt = []
     self.img_path = '{}/{}.jpg'.format(directory, name)
     self.mask_path = '{}/mask/mask.{}.png'.format(directory, name)
     with open('{}/gt/gt.{}.txt'.format(directory, name)) as f:
         for line in f.readlines():
             parts = line.strip().split(' ')
             gt = GroundTruth()
             gt.type = parts[4]
             gt.rectangle = Rectangle()
             gt.rectangle.top_left = (float(parts[0]), float(parts[1]))
             gt.rectangle.width = float(parts[3]) - float(parts[1]) + 1
             gt.rectangle.height = float(parts[2]) - float(parts[0]) + 1
             self.gt.append(gt)
Пример #8
0
def sliding_window(mask: np.array) -> (np.array, List[Rectangle]):
    regions = []

    side = SIDE
    for _ in range(INTERMEDIATE_STEPS):
        positions = window_iter(mask, side)
        for pos in positions:
            regions.append(
                Rectangle(top_left=(pos[0], pos[1]), width=side, height=side))

        side = int(side / SHRINK_MULTIPLIER)
        if side % 2 == 0:
            side += 1

    regions = combine_overlapped_regions(regions)
    mask = clear_non_region_mask(mask, regions)
    return mask, regions
Пример #9
0
def get_mask(mask: np.array) -> (np.array, List[Rectangle]):
    integral = cv2.integral(mask / 255)

    positions = int_iter(integral)
    regions = []
    for pos in positions:
        regions.append(
            Rectangle(
                top_left=(pos[0], pos[1]),
                width=pos[2],
                height=pos[2]
            )
        )

    regions = combine_overlapped_regions(regions)
    mask = clear_non_region_mask(mask, regions)
    return mask, regions
Пример #10
0
 def train(self, images: List[Picture]) -> List[Rectangle]:
     self.compare_histograms.train(images)
     return [Rectangle() for _ in images]
Пример #11
0
def get_cropped(rectangle: Rectangle, img):
    img_cropped = img[
        int(rectangle.top_left[0]):int(rectangle.get_bottom_right()[0]) + 1,
        int(rectangle.top_left[1]):int(rectangle.get_bottom_right()[1]) + 1]
    return img_cropped
Пример #12
0
def detect_text_gray(img: np.ndarray) -> (np.ndarray, Rectangle):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    kernel = np.ones((3, 3))

    Ib = cv2.GaussianBlur(gray, (3, 3), 5)

    gradient = cv2.morphologyEx(Ib, cv2.MORPH_GRADIENT, kernel)

    re, th1 = cv2.threshold(gradient, 127, 255, cv2.THRESH_BINARY)
    th2 = cv2.adaptiveThreshold(gradient, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 11, 2)

    th3 = cv2.bitwise_and(th1, th2)

    edges = cv2.dilate(th3, kernel)

    edges1 = cv2.erode(edges, kernel)

    cnts = cv2.findContours(edges1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    # ret, labels = cv2.connectedComponents(cnts)

    text = []
    corner_left = gray.shape
    corner_right = (0, 0)
    for c in cnts:
        (x, y, w, h) = cv2.boundingRect(c)

        if (5 <= w <= 150) and (5 <= h <= 150):

            text.append(c)
            if cv2.norm((x, y)) < cv2.norm(corner_left):
                corner_left = (x, y)
            if cv2.norm((x + w, y + h)) > cv2.norm(corner_right):
                corner_right = (x + w, y + h)

        # cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2)

    padding = int(0.07 * (cv2.norm(np.subtract(corner_right, corner_left))))
    sub = np.subtract(corner_right, corner_left)
    width = sub[0]
    height = sub[1]

    bounding = Rectangle(corner_left, width, height)

    mask = np.ones(img.shape[:2], dtype=np.uint8) * 255

    # cv2.rectangle(im, (corner_left[0] - padding, corner_left[1] - padding),
    #              (bounding.get_bottom_right()[0] + padding, bounding.get_bottom_right()[1] + padding), (0, 0, 0), -1)

    corner_left = (corner_left[0] - padding, corner_left[1] - padding)
    corner_right = (bounding.get_bottom_right()[0] + padding,
                    bounding.get_bottom_right()[1] + padding)
    mask = cv2.rectangle(mask, corner_left, corner_right, (0, 0, 0), -1)

    sub = np.subtract(corner_right, corner_left)
    width = sub[0]
    height = sub[1]
    bounding = Rectangle(corner_left, width, height)
    # cv2.imshow('mask',mask)

    # plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    # plt.show()

    return mask, bounding
Пример #13
0
def detect_text(img: np.ndarray) -> (np.ndarray, Rectangle):
    im = img.copy()
    im_yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV)

    Y, U, V = cv2.split(im_yuv)

    kernel = np.ones((3, 3))

    Ib = cv2.GaussianBlur(Y, (3, 3), 5)

    gradient = cv2.morphologyEx(Ib, cv2.MORPH_GRADIENT, kernel)

    re, th1 = cv2.threshold(gradient, 126, 255, cv2.THRESH_BINARY)

    edges = cv2.dilate(th1, kernel)

    edges1 = cv2.erode(edges, kernel)

    cnts = cv2.findContours(edges1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    # ret, labels = cv2.connectedComponents(cnts)

    text = []
    corner_left = Y.shape
    corner_right = (0, 0)
    for c in cnts:
        (x, y, w, h) = cv2.boundingRect(c)

        if (5 <= w <= 300) and (6 <= h <= 300):

            text.append(c)
            if cv2.norm((x, y)) < cv2.norm(corner_left):
                corner_left = (x, y)
            if cv2.norm((x + w, y + h)) > cv2.norm(corner_right):
                corner_right = (x + w, y + h)

        # cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2)

    padding = int(0.07 * (cv2.norm(np.subtract(corner_right, corner_left))))
    sub = np.subtract(corner_right, corner_left)
    width = sub[0]
    height = sub[1]

    bounding = Rectangle(corner_left, width, height)

    mask = np.ones(img.shape[:2], dtype=np.uint8) * 255

    # cv2.rectangle(im, (corner_left[0] - padding, corner_left[1] - padding),
    #              (bounding.get_bottom_right()[0] + padding, bounding.get_bottom_right()[1] + padding), (0, 0, 0), -1)

    corner_left = (corner_left[0] - padding, corner_left[1] - padding)
    corner_right = (bounding.get_bottom_right()[0] + padding,
                    bounding.get_bottom_right()[1] + padding)
    mask = cv2.rectangle(mask, corner_left, corner_right, (0, 0, 0), -1)
    sub = np.subtract(corner_right, corner_left)
    width = sub[0]
    height = sub[1]
    bounding = Rectangle(corner_left, width, height)
    # cv2.imshow('mask',mask)

    # plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    # plt.show()

    return mask, bounding