Example #1
0
def prewit_detection(image, image_height, image_width):
    pixels = np.array(image)
    horizontal_matrix = get_prewit_horizontal_matrix()
    horizontal_image = np.zeros((image_height, image_width))
    vertical_matrix = get_prewit_vertical_matrix()
    vertical_image = np.zeros((image_height, image_width))
    new_image = np.zeros((image_height, image_width))
    window_y_center = 1
    window_x_center = 1
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            horizontal_image[y, x] = get_convolution(pixels, x, y,
                                                     horizontal_matrix, 3)
            vertical_image[y, x] = get_convolution(pixels, x, y,
                                                   vertical_matrix, 3)
            new_image[y, x] = sqrt(
                pow(horizontal_image[y, x], 2) + pow(vertical_image[y, x], 2))
    save_image(horizontal_image, save_path + "prewit_horizontal_image.ppm")
    save_image(vertical_image, save_path + "prewit_vertical_image.ppm")
    save_image(new_image, save_path + "prewit_generated_image.ppm")
    image_one = Image.fromarray(
        lineally_adjust_image_values(horizontal_image, image_width,
                                     image_height))
    image_two = Image.fromarray(
        lineally_adjust_image_values(vertical_image, image_width,
                                     image_height))
    image_three = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image_one.show()
    image_two.show()
    image_three.show()
Example #2
0
def four_direction_border_detection(image,
                                    image_height,
                                    image_width,
                                    famous_matrix=1):
    pixels = np.array(image)
    horizontal_matrix = get_famous_horizontal_matrix(famous_matrix)
    first_diagonal_matrix = rotate_matrix_with_angle(horizontal_matrix, 3, 45)
    vertical_matrix = rotate_matrix_with_angle(first_diagonal_matrix, 3, 45)
    second_diagonal_matrix = rotate_matrix_with_angle(vertical_matrix, 3, 45)
    horizontal_image = np.zeros((image_height, image_width))
    first_diagonal_image = np.zeros((image_height, image_width))
    vertical_image = np.zeros((image_height, image_width))
    second_diagonal_image = np.zeros((image_height, image_width))
    new_image = np.zeros((image_height, image_width))
    window_y_center = 1
    window_x_center = 1
    current_values = np.zeros(4)
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            horizontal_image[y, x] = get_convolution(pixels, x, y,
                                                     horizontal_matrix, 3)
            first_diagonal_image[y,
                                 x] = get_convolution(pixels, x, y,
                                                      first_diagonal_matrix, 3)
            vertical_image[y, x] = get_convolution(pixels, x, y,
                                                   vertical_matrix, 3)
            second_diagonal_image[y,
                                  x] = get_convolution(pixels, x, y,
                                                       second_diagonal_matrix,
                                                       3)
            current_values[0] = horizontal_image[y, x]
            current_values[1] = first_diagonal_image[y, x]
            current_values[2] = vertical_image[y, x]
            current_values[3] = second_diagonal_image[y, x]
            new_image[y, x] = np.max(current_values)
    save_image(horizontal_image, save_path + "horizontal_image.ppm")
    save_image(first_diagonal_image, save_path + "first_diagonal_image.ppm")
    save_image(vertical_image, save_path + "vertical_image.ppm")
    save_image(second_diagonal_image, save_path + "second_diagonal_image.ppm")
    save_image(new_image, save_path + "four_directions_generated_image.ppm")
    image_one = Image.fromarray(
        lineally_adjust_image_values(horizontal_image, image_width,
                                     image_height))
    image_two = Image.fromarray(
        lineally_adjust_image_values(first_diagonal_image, image_width,
                                     image_height))
    image_three = Image.fromarray(
        lineally_adjust_image_values(vertical_image, image_width,
                                     image_height))
    image_four = Image.fromarray(
        lineally_adjust_image_values(second_diagonal_image, image_width,
                                     image_height))
    image_five = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image_one.show()
    image_two.show()
    image_three.show()
    image_four.show()
    image_five.show()
def bilateral_filter(image,
                     image_height,
                     image_width,
                     sigma_s,
                     sigma_r,
                     window_size,
                     show_image=True,
                     load_image=True):
    new_image = np.zeros((image_height, image_width))
    pixels = image
    if load_image:
        pixels = np.array(image)
    window_y_center = int(window_size / 2)
    window_x_center = int(window_size / 2)
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            sliding_window = get_bilateral_window(pixels, window_size, sigma_s,
                                                  sigma_r, x, y)
            new_image[y, x] = get_convolution(pixels, x, y, sliding_window,
                                              window_size)
    if show_image:
        save_image(new_image, save_path + "bilateral_filter_image.ppm")
        image = Image.fromarray(
            lineally_adjust_image_values(new_image, image_width, image_height))
        image.show()
    return new_image
def gaussian_filter(image,
                    image_height,
                    image_width,
                    sigma,
                    show_image=True,
                    unread_image=True,
                    window=-1):
    new_image = np.zeros((image_height, image_width))
    window_size = window
    if window == -1:
        window_size = 2 * sigma + 1
    pixels = image
    if unread_image:
        pixels = np.array(image)
    window_y_center = int(window_size / 2)
    window_x_center = int(window_size / 2)
    sliding_window = get_gaussian_window(window_size, sigma)
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            new_image[y, x] = get_convolution(pixels, x, y, sliding_window,
                                              window_size)
    if show_image:
        save_image(new_image, save_path + "gaussian_filter_image.ppm")
        image = Image.fromarray(
            lineally_adjust_image_values(new_image, image_width, image_height))
        image.show()
    return new_image
Example #5
0
def laplacian_gaussian_method(image, image_height, image_width, sigma,
                              threshold, sinthesis_method):
    n = int(6 * sigma + 1)
    pixels = np.array(image)
    matrix = get_laplacian_gaussian_matrix(n, sigma)
    new_image = np.zeros((image_height, image_width))
    window_y_center = int(n / 2)
    window_x_center = int(n / 2)
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            new_image[y, x] = get_convolution(pixels, x, y, matrix, n)
    horizontal_image = horizontal_zero_crossing_with_slope(
        new_image, image_height, image_width, threshold)
    vertical_image = vertical_zero_crossing_with_slope(new_image, image_height,
                                                       image_width, threshold)
    if sinthesis_method == "and":
        new_image = and_sinthesis(horizontal_image, vertical_image,
                                  image_height, image_width)
    elif sinthesis_method == "or":
        new_image = or_sinthesis(horizontal_image, vertical_image,
                                 image_height, image_width)
    else:
        new_image = module_sinthesis(horizontal_image, vertical_image,
                                     image_height, image_width)
    save_image(new_image,
               save_path + "laplacian_gaussian_generated_with_slope_image.ppm")
    image = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image.show()
def border_enhancement_filter(image, image_height, image_width):
    new_image = np.zeros((image_height, image_width))
    window_size = 3
    pixels = np.array(image)
    window_y_center = 1
    window_x_center = 1
    sliding_window = get_border_enhancement_window()
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            new_image[y, x] = get_convolution(pixels, x, y, sliding_window,
                                              window_size)
    save_image(new_image, save_path + "border_enhancement_filter_image.ppm")
    image = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image.show(
        "Border enhancement",
        lineally_adjust_image_values(new_image, image_width, image_height))
    return new_image
Example #7
0
def gaussian_noise_generator(percentage, is_additive, image, image_width,
                             image_height, mu, sigma):
    pixels = image.load()
    noise_values = gaussian_generator(mu, sigma, image_height * image_width)
    new_image = get_image_with_noise(pixels, image_height, image_width,
                                     noise_values, is_additive, percentage)
    save_image(new_image, save_path + "gaussian_noise_image.ppm")
    img = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    img.show()
    return new_image
Example #8
0
def rayleigh_noise_generator(percentage, is_additive, image, image_width,
                             image_height, xi):
    pixels = image.load()
    noise_values = rayleigh_generator(xi, image_height * image_width)
    new_image = get_image_with_noise(pixels, image_height, image_width,
                                     noise_values, is_additive, percentage)
    save_image(new_image, save_path + "rayleigh_noise_image.ppm")
    img = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    img.show()
    return new_image
Example #9
0
def exponential_noise_generator(percentage, is_additive, image, image_width,
                                image_height, lambda_value):
    pixels = image.load()
    noise_values = exponential_generator(lambda_value,
                                         image_height * image_width)
    new_image = get_image_with_noise(pixels, image_height, image_width,
                                     noise_values, is_additive, percentage)
    save_image(new_image, save_path + "exponential_noise_image.ppm")
    img = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    img.show()
    return new_image
Example #10
0
def sobel_detection(image,
                    image_height,
                    image_width,
                    show_images=True,
                    is_colored=False):
    pixels = np.array(image)
    if is_colored:
        pixels = cv2.cvtColor(pixels, cv2.COLOR_BGR2GRAY)
    horizontal_matrix = get_sobel_horizontal_matrix()
    horizontal_image = np.zeros((image_height, image_width))
    vertical_matrix = get_sobel_vertical_matrix()
    vertical_image = np.zeros((image_height, image_width))
    new_image = np.zeros((image_height, image_width))
    window_y_center = 1
    window_x_center = 1
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            horizontal_image[y, x] = get_convolution(pixels, x, y,
                                                     horizontal_matrix, 3)
            vertical_image[y, x] = get_convolution(pixels, x, y,
                                                   vertical_matrix, 3)
            new_image[y, x] = sqrt(
                pow(horizontal_image[y, x], 2) + pow(vertical_image[y, x], 2))
    if show_images:
        save_image(horizontal_image, save_path + "sobel_horizontal_image.ppm")
        save_image(vertical_image, save_path + "sobel_vertical_image.ppm")
        save_image(new_image, save_path + "sobel_generated_image.ppm")
        image_one = Image.fromarray(
            lineally_adjust_image_values(horizontal_image, image_width,
                                         image_height))
        image_two = Image.fromarray(
            lineally_adjust_image_values(vertical_image, image_width,
                                         image_height))
        image_three = Image.fromarray(
            lineally_adjust_image_values(new_image, image_width, image_height))
        image_one.show()
        image_two.show()
        image_three.show()
    return [horizontal_image, vertical_image, new_image]
Example #11
0
def sift_method(image, image_height, image_width, is_colored=True):
    # cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    pixels = np.array(image, dtype=np.uint8)
    gray = pixels
    if is_colored:
        gray = cv2.cvtColor(pixels, cv2.COLOR_BGR2GRAY)
    else:
        gray = lineally_adjust_image_values(pixels, image_width, image_height)
    sift = cv2.xfeatures2d.SIFT_create()
    key_points, descriptors = sift.detectAndCompute(gray, None)
    # image = cv2.drawKeypoints(gray, key_points, pixels, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    # cv2.imwrite(save_path + 'sift_keypoint.jpg', image)
    # cv2.imshow('ventana', image)
    return [gray, key_points, descriptors]
def median_filter(image, image_height, image_width, window_size):
    new_image = np.zeros((image_height, image_width))
    pixels = image.load()
    window_y_center = int(window_size / 2)
    window_x_center = int(window_size / 2)
    middle = int(window_size * window_size / 2)
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            new_image[y, x] = get_median_window(pixels, x, y,
                                                window_size)[middle]
    save_image(new_image, save_path + "median_filter_image.ppm")
    image = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image.show()
    return new_image
def weighted_median_filter(image, image_height, image_width, window_size):
    new_image = np.zeros((image_height, image_width))
    pixels = image.load()
    window_y_center = 1
    window_x_center = 1
    sliding_window = get_weighted_median_window()
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            new_image[y,
                      x] = get_weighted_median_value(pixels, x, y, window_size,
                                                     sliding_window)
    save_image(new_image, save_path + "weighted_median_filter_image.ppm")
    image = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image.show()
    return new_image
def media_filter(image, image_height, image_width, window_size):
    if window_size % 2 == 0:
        window_size = window_size + 1
    new_image = np.zeros((image_height, image_width))
    pixels = np.array(image)
    sliding_window = generate_media_window(window_size)
    window_y_center = int(window_size / 2)
    window_x_center = int(window_size / 2)
    for y in range(window_y_center, image_height - window_y_center):
        for x in range(window_x_center, image_width - window_x_center):
            new_image[y, x] = get_convolution(pixels, x, y, sliding_window,
                                              window_size)
    save_image(new_image, save_path + "media_filter_image.ppm")
    image = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image.show()
    return new_image
def isotropic_diffusion_filter(image, image_height, image_width, t_max):
    new_image = np.zeros((image_height, image_width))
    window_size = 3
    pixels = np.array(image)
    window_y_center = int(window_size / 2)
    window_x_center = int(window_size / 2)
    for t in range(0, t_max):
        for y in range(window_y_center, image_height - window_y_center):
            for x in range(window_x_center, image_width - window_x_center):
                new_image[y, x] = get_diffusion_value(pixels, x, y, 1, False,
                                                      False)
        pixels = new_image
    save_image(new_image, save_path + "isotropic_diffusion.ppm")
    image = Image.fromarray(
        lineally_adjust_image_values(new_image, image_width, image_height))
    image.show()
    return new_image
Example #16
0
def canny_method(image,
                 image_height,
                 image_width,
                 sigma_s,
                 sigma_r,
                 window_size,
                 four_neighbours=True,
                 show_image=True,
                 load_image=True):
    filtered_image = bilateral_filter(image, image_height, image_width,
                                      sigma_s, sigma_r, window_size, False,
                                      load_image)
    images = sobel_detection(filtered_image, image_height, image_width, False)
    horizontal_image = images[0]
    vertical_image = images[1]
    synthesized_image = images[2]
    # synthesized_image = normalize(synthesized_image, image_height, image_width)
    angle_matrix = get_angle_matrix(horizontal_image, vertical_image,
                                    image_height, image_width)
    suppressed_image = suppress_false_maximums(synthesized_image, angle_matrix,
                                               image_height, image_width)
    # threshold = threshold_multiotsu(suppressed_image)
    # low_threshold = threshold[0]
    high_threshold = np.amax(suppressed_image) * 0.14
    low_threshold = np.amax(suppressed_image) * 0.06
    # high_threshold = low_threshold + 50
    umbralized_image = umbralization_with_two_thresholds(
        suppressed_image, image_height, image_width, high_threshold,
        low_threshold)
    border_image = hysteresis(umbralized_image, image_height, image_width,
                              four_neighbours)
    if show_image:
        save_image(border_image, save_path + "canny_generated_image.ppm")
        image = Image.fromarray(
            lineally_adjust_image_values(border_image, image_width,
                                         image_height))
        image.show()
    return border_image