def get_masked_images(img, contours):
    mask = np.zeros(img.shape[:2], dtype="uint8")

    filtered_contours = []
    for c in contours:
        ps = math.pow(cv2.arcLength(c, True), 2)
        circularity = 0
        area = 0
        if ps > 0:
            area = cv2.contourArea(c)
            circularity = (4 * math.pi * area) / ps
        if circularity > 0.08 and area > 30:
            filtered_contours.append(c)

    masks = []
    united_contours = features_util.combine_contours(filtered_contours, 20)

    for c in united_contours:
        sub_mask = mask.copy()
        cv2.drawContours(sub_mask, c, -1, (255, 255, 255), -1)
        x1, y1, w, h = features_util.largest_bounding_rect(c)
        cropped = sub_mask[y1:y1 + h, x1:x1 + w]
        masks.append(cropped)

    return masks
Exemple #2
0
def get_masked_images(img, x_dist):
    contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1]
    # Sort contours by x value.
    contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0])

    # Combine contours that are less than 'x_dist' apart from each on the x-axis (or 30 on the y).
    contours = features_util.combine_contours(contours, x_dist, 40)
    #contours = features_util.combine_contours_better(filtered_contours, x_dist, 30)
    masks = []
    for c in contours:
        mask = np.zeros(img.shape, "uint8")
        cv2.drawContours(mask, c, -1, (255, 255, 255), -1)
        min_y, max_y, min_x, max_x = features_util.crop_to_content(mask,
                                                                   padding=2)
        mask = mask[min_y:max_y, min_x:max_x]
        masks.append(mask)

    # Split masks into two, if they are over '24' pixels in width.
    # This is because some letters are too close to each other for the contours
    # to be seperated.
    filtered_masks = []
    for mask in masks:
        if mask.shape[1] > 24:
            mask1 = mask[:, 0:12]
            mask2 = mask[:, 12:]
            filtered_masks.append(mask1)
            filtered_masks.append(mask2)
        elif mask.shape[0] > 12 or mask.shape[1] > 12:
            filtered_masks.append(mask)

    return filtered_masks
Exemple #3
0
def segment_image(img):
    contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
    contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0])
    contours = features_util.combine_contours(contours, 3, 20)

    for c in contours:
        if 15 < cv2.contourArea(c[0]) < 35:
            return True
    return False
Exemple #4
0
def get_contour_positions(img):
    contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
    contours = features_util.combine_contours(contours, 3)
    bboxes = [features_util.largest_bounding_rect(c) for c in contours]

    bboxes.sort(key=lambda x: x[2],
                reverse=True)  # Sort by width of contour bbox.
    bboxes = [features_util.mid_bbox(bbox) for bbox in bboxes]
    return (bboxes[0], bboxes[1])
Exemple #5
0
def segment_image(img):
    contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
    filtered_contours = []
    for c in contours:
        area = cv2.contourArea(c)
        if area < 1000:
            filtered_contours.append(c)

    contours = filtered_contours
    contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0])

    contours = features_util.combine_contours(contours, 5, 30)
    masks = []
    for c in contours:
        mask = np.zeros(img.shape[:2], dtype="uint8")
        cv2.drawContours(mask, c, -1, (255, 255, 255), -1)
        masks.append(mask)
    return masks