Ejemplo n.º 1
0
def create_average_overlay(masks):
    binary = create_average_mask(masks)

    # mask contours
    contours_layer = (
        np.zeros((binary.shape[0], binary.shape[1], 3), dtype=np.uint8) + 255
    )
    for mask in masks:
        m = mask * 255
        contour = get_carrot_contour(m)

        # x offset: difference in length
        offset_x = binary.shape[1] - mask.shape[1]

        # y offset: difference in height
        offset_y = int((binary.shape[0] - mask.shape[0]) / 2)

        cv2.drawContours(
            contours_layer, [contour], -1, (0, 0, 0), 2, offset=(offset_x, offset_y)
        )

    # avg contour
    contour = get_carrot_contour(binary)

    avg_overlay = np.zeros((binary.shape[0], binary.shape[1], 3), dtype=np.uint8) + 255

    cv2.drawContours(avg_overlay, [contour], -1, (0, 0, 255), -1)

    alpha = 0.7
    cv2.addWeighted(avg_overlay, alpha, contours_layer, 1 - alpha, 0, contours_layer)

    return contours_layer
def assemble_instance(file):
    instance = get_attributes_from_filename(file)

    if "kernel" in file:
        image_type = "kernel"
    elif "in-shell" in file:
        image_type = "in-shell"
    instance["type"] = image_type

    image = cv2.imread(file, cv2.IMREAD_GRAYSCALE)

    white_pixels = cv2.countNonZero(image)
    instance["white_pixels"] = white_pixels

    scale = instance.get("Scale", None)
    if scale:
        instance["area"] = round(white_pixels / float(scale) ** 2, 3)
    else:
        instance["area"] = "no scale"

    contour = get_carrot_contour(image)

    # https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html
    # cnts = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    # print(type(cnts))
    # c = max(cnts, key=cv2.contourArea)
    # color_image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    # cv2.drawContours(color_image, [contour], -1, (0, 0, 255), 3)

    x, y, w, h = cv2.boundingRect(contour)
    # color_image = cv2.rectangle(color_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    instance["width"] = w
    instance["height"] = h

    # https://stackoverflow.com/questions/31281235/anomaly-with-ellipse-fitting-when-using-cv2-ellipse-with-different-parameters
    # https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_drawing_functions/py_drawing_functions.html#drawing-ellipse
    ellipse = cv2.fitEllipse(contour)
    ellipse_major_axis, ellipse_minor_axis = ellipse[1]
    ellipse_angle = ellipse[2]
    instance["ellipse_major_axis"] = round(ellipse_major_axis, 2)
    instance["ellipse_minor_axis"] = round(ellipse_minor_axis, 2)
    instance["ellipse_angle"] = round(ellipse_angle, 2)
    # cv2.ellipse(color_image, ellipse, (0, 255, 0), 2)

    # print(ellipse[1], ellipse[2])
    rect = cv2.minAreaRect(contour)
    _, width_height, _ = rect
    min_area_width, min_area_height = width_height
    instance["min_area_width"] = round(min_area_width, 2)
    instance["min_area_height"] = round(min_area_height, 2)
    # print(min_area_width, min_area_height)
    # box = cv2.boxPoints(rect)
    # box = np.int0(box)
    # cv2.drawContours(color_image, [box], 0, (255, 0, 0), 2)
    # print(len(cnts))
    # cv2.imwrite("/Users/creimers/Downloads/glibba.png", color_image)
    return instance
Ejemplo n.º 3
0
def create_nut_mask(nut_image, masking_method):
    """
    create a binary mask of the nut
    """
    output = np.zeros(nut_image.shape, dtype=np.uint8)

    mask = masking_method(nut_image)
    contour = get_carrot_contour(mask)
    if contour is not None and contour.shape[0] > 100:
        cv2.drawContours(output, [contour], -1, (255, 255, 255), -1)
        return output
Ejemplo n.º 4
0
def create_nut_mask_overlay(nut_image, masking_method):
    overlay = nut_image.copy()
    output = nut_image.copy()

    mask = masking_method(nut_image)
    contour = get_carrot_contour(mask)
    overlay_color = (0, 255, 0)

    alpha = 0.5

    cv2.drawContours(overlay, [contour], -1, overlay_color, -1)
    cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)

    return output
Ejemplo n.º 5
0
def create_ellipse_overlay(nut_image, masking_method):
    # https://docs.opencv.org/master/dd/d49/tutorial_py_contour_features.html
    image = nut_image.copy()
    mask = masking_method(nut_image)
    contour = get_carrot_contour(mask)

    if contour is not None:
        ellipse = cv2.fitEllipse(contour)
        cv2.ellipse(image, ellipse, (0, 255, 0), 2)
        _, cols = image.shape[:2]
        [vx, vy, x, y] = cv2.fitLine(contour, cv2.DIST_L2, 0, 0.01, 0.01)
        lefty = int((-x * vy / vx) + y)
        righty = int(((cols - x) * vy / vx) + y)
        cv2.line(image, (cols - 1, righty), (0, lefty), (0, 0, 255), 2)
        return image