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()
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 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 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]
def prewit_color_detection(image, image_height, image_width): pixels = np.array(image) pixels_red = pixels[:, :, 0] pixels_green = pixels[:, :, 1] pixels_blue = pixels[:, :, 2] horizontal_matrix = get_prewit_horizontal_matrix() horizontal_image = np.zeros((image_height, image_width, 3)) vertical_matrix = get_prewit_vertical_matrix() vertical_image = np.zeros((image_height, image_width, 3)) new_image = np.zeros((image_height, image_width, 3)) 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, 0] = get_convolution(pixels_red, x, y, horizontal_matrix, 3) horizontal_image[y, x, 1] = get_convolution(pixels_green, x, y, horizontal_matrix, 3) horizontal_image[y, x, 2] = get_convolution(pixels_blue, x, y, horizontal_matrix, 3) vertical_image[y, x, 0] = get_convolution(pixels_red, x, y, vertical_matrix, 3) vertical_image[y, x, 1] = get_convolution(pixels_green, x, y, vertical_matrix, 3) vertical_image[y, x, 2] = get_convolution(pixels_blue, x, y, vertical_matrix, 3) new_image[y, x, 0] = sqrt( pow(horizontal_image[y, x, 0], 2) + pow(vertical_image[y, x, 0], 2)) new_image[y, x, 1] = sqrt( pow(horizontal_image[y, x, 1], 2) + pow(vertical_image[y, x, 1], 2)) new_image[y, x, 2] = sqrt( pow(horizontal_image[y, x, 2], 2) + pow(vertical_image[y, x, 2], 2)) save_colored_image( lineally_adjust_and_resize_colored_image_values( horizontal_image, image_width, image_height), save_path + "prewit_colored_horizontal_image.ppm") save_colored_image( lineally_adjust_and_resize_colored_image_values( vertical_image, image_width, image_height), save_path + "prewit_colored_vertical_image.ppm") save_colored_image( lineally_adjust_and_resize_colored_image_values( new_image, image_width, image_height), save_path + "prewit_colored_generated_image.ppm") image_one = Image.fromarray( lineally_adjust_and_resize_colored_image_values( horizontal_image, image_width, image_height), "RGB") image_two = Image.fromarray( lineally_adjust_and_resize_colored_image_values( vertical_image, image_width, image_height), "RGB") image_three = Image.fromarray( lineally_adjust_and_resize_colored_image_values( new_image, image_width, image_height), "RGB") image_one.show() image_two.show() image_three.show()