Exemplo n.º 1
0
def compare(function, image, gray_image):
    show_original(image)
    desc = function_name(function)
    for i in range(0, 3):
        eroded = function(gray_image.copy(), None, iterations=i + 1)
        cv2.imshow(f"{desc} {i + 1} times", eroded)
    wait_and_destroy_all_windows()
Exemplo n.º 2
0
def compare_average_blur():
    """Fast, but doesn't preserve edges"""
    show_original()
    for (kx, ky) in kernel_sizes:
        blurred_img = cv2.blur(img, (kx, ky))
        cv2.imshow("Average ({}, {})".format(kx, ky), blurred_img)
    wait_and_destroy_all_windows()
Exemplo n.º 3
0
def compare_gaussian_blur():
    """Slightly slower and better at preserving edges"""
    show_original()
    for (kx, ky) in kernel_sizes:
        blurred_img = cv2.GaussianBlur(img, (kx, ky), sigmaX=0, sigmaY=0)
        cv2.imshow("Gaussian ({}, {})".format(kx, ky), blurred_img)
    wait_and_destroy_all_windows()
Exemplo n.º 4
0
def show_otsu_thresholding(image, blurred_image):
    (T, thresh_inv) = cv2.threshold(
        blurred_image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    cv2.imshow("Original", image)
    cv2.imshow(f"Otsu Thresholded T={T}", thresh_inv)
    cv2.imshow("Masked", cv2.bitwise_and(image, image, mask=thresh_inv))
    wait_and_destroy_all_windows()
Exemplo n.º 5
0
def compare_morphological_op(op_type, elem_type, image, gray_image):
    show_original(image)
    kernel_sizes = [(3, 3), (5, 5), (7, 7)]
    for size in kernel_sizes:
        kernel = cv2.getStructuringElement(elem_type, size)
        opening = cv2.morphologyEx(gray_image, op_type, kernel)
        op_title = morphological_op_name(op_type)
        cv2.imshow(f"{op_title}: ({size[0]}, {size[1]})", opening)
    wait_and_destroy_all_windows()
Exemplo n.º 6
0
def show_contour_masking(image, image_gray):
    contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
    for idx, contour in enumerate(contours):
        mask = np.zeros(image_gray.shape, dtype="uint8")
        cv2.drawContours(mask, [contour], -1, (255), -1)
        cv2.imshow("Original", image)
        cv2.imshow("Mask", mask)
        cv2.imshow(f"Masked contours: {idx}",
                   cv2.bitwise_and(image, image, mask=mask))
        wait_and_destroy_all_windows()
Exemplo n.º 7
0
def compare_median_blur():
    """
    Used to reduce salt-and-pepper style noise as the median statistic is much
    less sensitive to outliers than other statistical methods like the mean
    """
    show_original()
    for k_size in (3, 9, 15):
        blurred_img = cv2.medianBlur(img, k_size)
        cv2.imshow("Median ({})".format(k_size), blurred_img)
    wait_and_destroy_all_windows()
Exemplo n.º 8
0
def show_simple_thresholding(image, blurred_image):
    (T, thresh_inv) = cv2.threshold(
        blurred_image, thresh=200, maxval=255, type=cv2.THRESH_BINARY_INV)
    (T, thresh) = cv2.threshold(
        blurred_image, thresh=200, maxval=255, type=cv2.THRESH_BINARY)
    cv2.imshow("Original", image)
    cv2.imshow("Thresholded Inverse", thresh_inv)
    cv2.imshow("Thresholded", thresh)
    cv2.imshow("Masked", cv2.bitwise_and(image, image, mask=thresh_inv))
    wait_and_destroy_all_windows()
Exemplo n.º 9
0
def compare_lab(rgb_image):
    """
    L* - Lightness, a* - blue-yellow scale, b* red-green scale.
    Euclidean distance has perceptual meaning.
    """
    lab_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2LAB)
    cv2.imshow("L*a*b*", lab_image)
    for (name, chan) in zip(("L*", "a*", "b*"), cv2.split(lab_image)):
        cv2.imshow(name, chan)
    wait_and_destroy_all_windows()
Exemplo n.º 10
0
def compare_bilateral_blur():
    """Substantially slower, removes detail while preserving edges"""
    show_original()
    params = [(11, 21, 7), (11, 41, 21), (11, 61, 39)]
    for (diameter, sigma_color, sigma_space) in params:
        blurred_img = cv2.bilateralFilter(img, diameter, sigma_color,
                                          sigma_space)
        title = "Bilateral d={} sc={} ss={}".format(diameter, sigma_color,
                                                    sigma_space)
        cv2.imshow(title, blurred_img)
    wait_and_destroy_all_windows()
Exemplo n.º 11
0
def show(function, image, image_gray):
    desc = None
    if function == cv2.Sobel:
        desc = "Sobel"
    elif function == cv2.Scharr:
        desc = "Scharr"
    g_x = function(image_gray, ddepth=cv2.CV_64F, dx=1, dy=0)
    g_y = function(image_gray, ddepth=cv2.CV_64F, dx=0, dy=1)
    g_x = cv2.convertScaleAbs(g_x)
    g_y = cv2.convertScaleAbs(g_y)
    image_sobel = cv2.addWeighted(g_x, 0.5, g_y, 0.5, 0)
    cv2.imshow("Original", image)
    cv2.imshow(f"{desc} X", g_x)
    cv2.imshow(f"{desc} Y", g_y)
    cv2.imshow(f"{desc} Combined", image_sobel)
    wait_and_destroy_all_windows()
Exemplo n.º 12
0
def show_all_contours(image, contours):
    clone = image.copy()
    cv2.drawContours(clone, contours, -1, (0, 255, 0), 2)
    cv2.imshow("Original", image)
    cv2.imshow("Contours", clone)
    wait_and_destroy_all_windows()
Exemplo n.º 13
0
def show_external_contours(image, image_gray):
    clone = image.copy()
    contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
    cv2.drawContours(clone, contours, -1, (0, 255, 0), 2)
    cv2.imshow("External contours", clone)
    wait_and_destroy_all_windows()
Exemplo n.º 14
0
def show_each_contour(image, contours):
    for (index, contour) in enumerate(contours):
        clone = image.copy()
        cv2.drawContours(clone, [contour], -1, (0, 255, 0), 2)
        cv2.imshow(f"Contour {index}", clone)
    wait_and_destroy_all_windows()
Exemplo n.º 15
0
def compare_rgb(rgb_image):
    cv2.imshow("RGB", rgb_image)
    for (name, chan) in zip(("B", "G", "R"), cv2.split(rgb_image)):
        cv2.imshow(name, chan)
    wait_and_destroy_all_windows()
Exemplo n.º 16
0
def compare_hsv(rgb_image):
    hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2HSV)
    cv2.imshow("HSV", hsv_image)
    for (name, chan) in zip(("H", "S", "V"), cv2.split(hsv_image)):
        cv2.imshow(name, chan)
    wait_and_destroy_all_windows()