Ejemplo n.º 1
0
def detect_text_area(img,
                     area_ratio_thresh=0.25,
                     char_min_pxl=10,
                     box_img_=False,
                     debug_=False):

    # convert to gray scale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # smooth the image to avoid noises
    filtered = cv2.medianBlur(gray, 3)

    # Apply adaptive threshold
    # thresh = cv2.adaptiveThreshold(gray, 255, 1, 1, 11, 2)
    _, bin = cv2.threshold(filtered,
                           thresh=128,
                           maxval=255,
                           type=cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

    kernel = np.ones((5, 5), np.uint8)
    """
    kernel[0:2,0:2] = 0
    kernel[0:2,3:5] = 0
    kernel[3:5,3:5] = 0
    kernel[3:5,0:2] = 0
    kernel[1,1] = 1
    kernel[1,3] = 1
    kernel[3,1] = 1
    kernel[3,3] = 1
    """

    morph = np.copy(bin)
    for _ in range(5):
        morph = cv2.dilate(morph, kernel, iterations=3)
        morph = cv2.erode(morph, kernel, iterations=3)

    # Find the contours
    image, contours, hierarchy = cv2.findContours(morph, cv2.RETR_TREE,
                                                  cv2.CHAIN_APPROX_SIMPLE)

    # For each contour, find the bounding rectangle and draw it
    boxes = []
    for contour in contours:
        x, y, w, h = cv2.boundingRect(contour)
        contour_area = cv2.contourArea(contour)
        rect_area = w * h
        if (contour_area / rect_area >
                area_ratio_thresh) and w > char_min_pxl and h > char_min_pxl:
            boxes.append([x, y, x + w, y + h])

    boxes_img = []
    if debug_:
        boxes_img = hp.draw_boxes_on_img(np.copy(img),
                                         boxes,
                                         color=hp.RED,
                                         thickness=4)
        hp.imshow(morph)
        hp.imshow(boxes_img)

    return boxes, boxes_img
Ejemplo n.º 2
0
def make_window_masked_image(self,
                             img,
                             win_pos,
                             color=utils.BLACK,
                             show_sec=-1):
    img = utils.draw_box_on_img(img,
                                win_pos,
                                color=color,
                                thickness=-1,
                                alpha=0)
    utils.imshow(img, desc="make window masked image", pause_sec=show_sec)

    return img
Ejemplo n.º 3
0
def get_roi(img, roi, imshow_sec=-1, clockwise_=True):
    roi = np.array(roi)
    roi = roi * np.array(img.shape[1::-1]) if not sum(sum(roi > 1)) else roi
    if clockwise_:
        roi[[2, 3]] = roi[[3, 2]]

    roi_corners = np.array([[tuple(x) for x in roi]], dtype=np.int32)
    ignore_mask_color = (255, ) * img.shape[2]
    mask = cv2.fillPoly(np.zeros(img.shape, dtype=np.uint8),
                        roi_corners,
                        color=ignore_mask_color)
    roi_img = cv2.bitwise_and(img, mask)
    utils.imshow(roi_img, desc="roi image", pause_sec=imshow_sec)

    # gray_img = cv2.cvtColor(roi_img, code=cv2.COLOR_RGB2GRAY)
    offset = [[0, 0], list(img.shape[1::-1])]

    return roi_img, offset
Ejemplo n.º 4
0
def make_window_boxed_image(img, veh_feats, color=None, show_sec=-1):
    for veh_idx, veh_feat in enumerate(veh_feats):
        if veh_feat.win_conf_arr:
            color = utils.get_color(i=veh_idx,
                                    primary_=False) if color is None else color
            for win_idx, win_pos in enumerate(veh_feat.win_pos_arr):
                img = utils.draw_quadrilateral_on_image(img,
                                                        win_pos,
                                                        color=color,
                                                        thickness=4)
                text = "{}: {:2d}".format("Window",
                                          veh_feat.win_conf_arr[win_idx])
                img = cv2.putText(img, text, (win_pos[0], win_pos[1] - 10),
                                  cv2.FONT_HERSHEY_SIMPLEX, 2, color, 4)

    utils.imshow(img, desc="make window boxed image", pause_sec=show_sec)

    return img
Ejemplo n.º 5
0
def make_face_masked_image(img,
                           face_pos_arr,
                           color=utils.RED,
                           margin=0,
                           show_sec=-1):
    if face_pos_arr is None:
        return img

    for face_pos in face_pos_arr:
        pos = [
            face_pos[0] - margin, face_pos[1] - margin, face_pos[2] + margin,
            face_pos[3] + margin
        ]
        pos = utils.check_box_boundary(pos, img.shape[1::-1])
        img = utils.draw_box_on_img(img,
                                    pos,
                                    color=color,
                                    thickness=-1,
                                    alpha=0)

    utils.imshow(img, desc="make face masked image", pause_sec=show_sec)

    return img
Ejemplo n.º 6
0
def derotate_image(img,
                   max_angle=30,
                   max_angle_candidates=50,
                   angle_resolution=0.5,
                   inside_margin_ratio=0.1,
                   rot_img_fname=None,
                   check_time_=False,
                   pause_img_sec=-1):
    """
    Derotate image.

    :param img:
    :param max_angle: Maximum rotated angle. The angles above this should be ignored.
    :param max_angle_candidates:
    :param angle_resolution:
    :param inside_margin_ratio:
    :param rot_img_fname:
    :param check_time_:
    :param pause_img_sec:
    :return:
    """
    start_time = None
    if check_time_:
        start_time = time.time()
    if len(img.shape) == 3:
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # img_gray = np.amin(img, axis=2)
    else:
        img_gray = np.copy(img)

    inside_margin = [int(x * inside_margin_ratio) for x in img.shape[1::-1]]

    img_gray[:inside_margin[1], :] = 255
    img_gray[-inside_margin[1]:, :] = 255
    img_gray[:, :inside_margin[0]] = 255
    img_gray[:, -inside_margin[0]:] = 255

    if False:
        check_lines_in_img(img, algorithm='HoughLineTransform')
        check_lines_in_img(img, algorithm='ProbabilisticHoughTransform')

    ret, img_bw = cv2.threshold(img_gray, 128, 255,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    """
    kernel = np.ones((5, 5), np.uint8)  # note this is a horizontal kernel
    bw = np.copy(img_bw)
    for i in range(9):
        bw = cv2.erode(bw, kernel, iterations=1)
        bw = cv2.dilate(bw, kernel, iterations=1)
    hp.hp_imshow(hp.hstack_images((img_bw, bw)))
    """
    img_edge = cv2.Canny(img_bw, 50, 150, apertureSize=3)
    if False:
        hp.hp_imshow(img_edge)
        # hp.plt_imshow(edges)

    lines = cv2.HoughLines(img_edge, 1, np.pi / 360,
                           int(min(img_edge.shape) / 8.))

    angles = []
    if lines is not None:
        for cnt, line in enumerate(lines):
            angle = int((90 - line[0][1] * 180 / np.pi) /
                        float(angle_resolution)) * angle_resolution
            draw_line_from_rho_and_theta(img,
                                         line[0][0],
                                         line[0][1],
                                         pause_sec=-1)

            if abs(angle) < max_angle:
                angles.append(angle)
            if max_angle_candidates < cnt:
                break

    # rot_angle = max(set(angles), key=angles.count)
    sorted_angles = sorted({x: angles.count(x)
                            for x in angles}.items(),
                           key=operator.itemgetter(1),
                           reverse=True)

    if len(sorted_angles) == 0:
        rot_angle = 0
    elif len(sorted_angles) == 1:
        rot_angle = sorted_angles[0][0]
    elif sorted_angles[0][0] == 0 and (sorted_angles[0][1] <
                                       2 * sorted_angles[1][1]):
        rot_angle = sorted_angles[1][0]
    elif (sorted_angles[0][1] / sorted_angles[1][1]
          ) < 3 and abs(sorted_angles[0][0] - sorted_angles[1][0]) <= 1.0:
        rot_angle = (sorted_angles[0][0] + sorted_angles[1][0]) / 2.
    else:
        rot_angle = sorted_angles[0][0]
    """
    if rot_angle != 0:
        rot_angle += 0.5
    """
    if pause_img_sec >= 0:
        print("# Rotated angle is {:5.1f} degree.".format(rot_angle))

    sz = img_bw.shape[1::-1]
    rot_img = ~imutils.rotate(~img,
                              angle=-rot_angle,
                              center=(int(sz[0] / 2), int(sz[1] / 2)),
                              scale=1)
    if check_time_:
        print(
            " # Time for rotation detection and de-rotation if any : {:.2f} sec"
            .format(float(time.time() - start_time)))

    if 0 <= pause_img_sec:
        hp.imshow(np.concatenate((img, rot_img), axis=1),
                  pause_sec=pause_img_sec,
                  desc="de-rotation")

    if rot_img_fname:
        hp.hp_imwrite(rot_img_fname, rot_img, 'RGB')

    return rot_img