def crop_sides_equally(mask, nir, device, debug):
    device += 1
    # NumPy refers to y first then x
    mask_shape = np.shape(mask)  # type: tuple
    nir_shape = np.shape(nir)  # type: tuple
    final_y = mask_shape[0]
    final_x = mask_shape[1]
    difference_x = final_x - nir_shape[1]
    difference_y = final_y - nir_shape[0]
    if difference_x % 2 == 0:
        x1 = difference_x / 2
        x2 = difference_x / 2
    else:
        x1 = difference_x / 2
        x2 = (difference_x / 2) + 1

    if difference_y % 2 == 0:
        y1 = difference_y / 2
        y2 = difference_y / 2
    else:
        y1 = difference_y / 2
        y2 = (difference_y / 2) + 1
    crop_img = mask[y1:final_y - y2, x1:final_x - x2]

    if debug == "print":
        pcv.print_image(crop_img, str(device) + "_crop_sides_equally.png")
    elif debug == "plot":
        pcv.plot_image(crop_img, cmap="gray")

    return device, crop_img
def distance_transform(bin_img, distance_type, mask_size):
    """Creates an image where for each object pixel, a number is assigned that corresponds to the distance to the
    nearest background pixel.

    Inputs:
    img             = Binary image data
    distance_type   = Type of distance. It can be CV_DIST_L1, CV_DIST_L2 , or CV_DIST_C which are 1, 2 and 3,
                      respectively.
    mask_size       = Size of the distance transform mask. It can be 3, 5, or CV_DIST_MASK_PRECISE (the latter option
                      is only supported by the first function). In case of the CV_DIST_L1 or CV_DIST_C distance type,
                      the parameter is forced to 3 because a 3 by 3 mask gives the same result as 5 by 5 or any larger
                      aperture.

    Returns:
    norm_image      = grayscale distance-transformed image normalized between [0, 1]

    :param bin_img: numpy.ndarray
    :param distance_type: int
    :param mask_size: int
    :return norm_image: numpy.ndarray
    """

    params.device += 1
    dist = cv2.distanceTransform(src=bin_img, distanceType=distance_type, maskSize=mask_size)
    norm_image = cv2.normalize(src=dist, dst=dist, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

    if params.debug == 'print':
        print_image(norm_image, os.path.join(params.debug, str(params.device) + '_distance_transform.png'))
    elif params.debug == 'plot':
        plot_image(norm_image, cmap='gray')

    return norm_image
Beispiel #3
0
def remove_countors_roi(mask, contours, hierarchy, roi, device, debug=None):
    clean_mask = np.copy(mask)
    # Loop over all contours in the image
    for n, contour in enumerate(contours):
        # This is the number of vertices in the contour
        contour_points = len(contour) - 1
        # Generate a list of x, y coordinates
        stack = np.vstack(contour)
        tests = []
        # Loop over the vertices for the contour and do a point polygon test
        for i in range(0, contour_points):
            # The point polygon test returns
            # 1 if the contour vertex is inside the ROI contour
            # 0 if the contour vertex is on the ROI contour
            # -1 if the contour vertex is outside the ROI contour
            pptest = cv2.pointPolygonTest(contour=roi[0], pt=(stack[i][0], stack[i][1]), measureDist=False)
            # Append the test result to the list of point polygon tests for this contour
            tests.append(pptest)
        # If all of the point polygon tests have a value of 1, then all the contour vertices are in the ROI
        if all(t == 1 for t in tests):
            # Fill in the contour as black
            cv2.drawContours(image=clean_mask, contours=contours, contourIdx=n, color=0, thickness=-1, lineType=8,
                             hierarchy=hierarchy)
    if debug == "print":
        pcv.print_image(filename=str(device) + "_remove_contours.png", img=clean_mask)
    elif debug == "plot":
        pcv.plot_image(clean_mask, cmap='gray')

    return device, clean_mask
Beispiel #4
0
def _draw_roi(img, roi_contour):
    """Draw an ROI

    :param img: numpy.ndarray
    :param roi_contour: list
    """
    # Make a copy of the reference image
    ref_img = np.copy(img)
    # If the reference image is grayscale convert it to color
    if len(np.shape(ref_img)) == 2:
        ref_img = cv2.cvtColor(ref_img, cv2.COLOR_GRAY2BGR)
    # Draw the contour on the reference image
    cv2.drawContours(ref_img, roi_contour, -1, (255, 0, 0), 5)
    if params.debug == "print":
        # If debug is print, save the image to a file
        print_image(
            ref_img,
            os.path.join(params.debug_outdir,
                         str(params.device) + "_roi.png"))
    elif params.debug == "plot":
        # If debug is plot, print to the plotting device
        plot_image(ref_img)