|
| 1 | +import numpy as np |
| 2 | +from contextlib import suppress |
| 3 | + |
| 4 | + |
| 5 | +def convolve_linear(signal, filter, output_size): |
| 6 | + out = np.zeros(output_size) |
| 7 | + sum = 0 |
| 8 | + |
| 9 | + for i in range(output_size[0]): |
| 10 | + for j in range(output_size[1]): |
| 11 | + for k in range(max(0, i-filter.shape[0]), i+1): |
| 12 | + for l in range(max(0, j-filter.shape[1]), j+1): |
| 13 | + with suppress(IndexError): |
| 14 | + sum += signal[k, l] * filter[i-k, j-l] |
| 15 | + out[i, j] = sum |
| 16 | + sum = 0 |
| 17 | + |
| 18 | + return out |
| 19 | + |
| 20 | + |
| 21 | +def create_gaussian_kernel(kernel_size): |
| 22 | + kernel = np.zeros((kernel_size, kernel_size)) |
| 23 | + |
| 24 | + # The center must be offset by 0.5 to find the correct index |
| 25 | + center = kernel_size*0.5 + 0.5 |
| 26 | + |
| 27 | + sigma = np.sqrt(0.1*kernel_size) |
| 28 | + |
| 29 | + def kernel_function(x, y): |
| 30 | + return np.exp(-((x-center+1)**2 + (y-center+1)**2)/(2*sigma**2)) |
| 31 | + |
| 32 | + kernel = np.fromfunction(kernel_function, (kernel_size, kernel_size)) |
| 33 | + return kernel / np.linalg.norm(kernel) |
| 34 | + |
| 35 | + |
| 36 | +def create_sobel_operators(): |
| 37 | + Sx = np.dot([[1.0], [2.0], [1.0]], [[-1.0, 0.0, 1.0]]) / 9 |
| 38 | + Sy = np.dot([[-1.0], [0.0], [1.0]], [[1.0, 2.0, 1.0]]) / 9 |
| 39 | + |
| 40 | + return Sx, Sy |
| 41 | + |
| 42 | +def sum_matrix_dimensions(mat1, mat2): |
| 43 | + return (mat1.shape[0] + mat2.shape[0], |
| 44 | + mat1.shape[1] + mat2.shape[1]) |
| 45 | + |
| 46 | +def compute_sobel(signal): |
| 47 | + Sx, Sy = create_sobel_operators() |
| 48 | + |
| 49 | + Gx = convolve_linear(signal, Sx, sum_matrix_dimensions(signal, Sx)) |
| 50 | + Gy = convolve_linear(signal, Sy, sum_matrix_dimensions(signal, Sy)) |
| 51 | + |
| 52 | + return np.sqrt(np.power(Gx, 2) + np.power(Gy, 2)) |
| 53 | + |
| 54 | + |
| 55 | +def create_circle(image_resolution, grid_extents, radius): |
| 56 | + out = np.zeros((image_resolution, image_resolution)) |
| 57 | + |
| 58 | + for i in range(image_resolution): |
| 59 | + x_position = ((i * grid_extents / image_resolution) |
| 60 | + - 0.5 * grid_extents) |
| 61 | + for j in range(image_resolution): |
| 62 | + y_position = ((j * grid_extents / image_resolution) |
| 63 | + - 0.5 * grid_extents) |
| 64 | + if x_position ** 2 + y_position ** 2 <= radius ** 2: |
| 65 | + out[i, j] = 1.0 |
| 66 | + |
| 67 | + return out |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + |
| 72 | + # Random distribution in x |
| 73 | + x = np.random.rand(100, 100) |
| 74 | + |
| 75 | + # Gaussian signals |
| 76 | + def create_gaussian_signals(i, j): |
| 77 | + return np.exp(-(((i-50)/100) ** 2 + |
| 78 | + ((j-50)/100) ** 2) / .01) |
| 79 | + y = np.fromfunction(create_gaussian_signals, (100, 100)) |
| 80 | + |
| 81 | + # Normalization is not strictly necessary, but good practice |
| 82 | + x /= np.linalg.norm(x) |
| 83 | + y /= np.linalg.norm(y) |
| 84 | + |
| 85 | + # full convolution, output will be the size of x + y |
| 86 | + full_linear_output = convolve_linear(x, y, sum_matrix_dimensions(x, y)) |
| 87 | + |
| 88 | + # simple boundaries |
| 89 | + simple_linear_output = convolve_linear(x, y, x.shape) |
| 90 | + |
| 91 | + np.savetxt("full_linear.dat", full_linear_output) |
| 92 | + np.savetxt("simple_linear.dat", simple_linear_output) |
| 93 | + |
| 94 | + # creating simple circle and 2 different Gaussian kernels |
| 95 | + circle = create_circle(50, 2, 0.5) |
| 96 | + |
| 97 | + circle = circle / np.linalg.norm(circle) |
| 98 | + |
| 99 | + small_kernel = create_gaussian_kernel(3) |
| 100 | + large_kernel = create_gaussian_kernel(25) |
| 101 | + |
| 102 | + small_kernel_output = convolve_linear(circle, small_kernel, |
| 103 | + sum_matrix_dimensions(circle, |
| 104 | + small_kernel)) |
| 105 | + |
| 106 | + large_kernel_output = convolve_linear(circle, large_kernel, |
| 107 | + sum_matrix_dimensions(circle, |
| 108 | + large_kernel)) |
| 109 | + |
| 110 | + np.savetxt("small_kernel.dat", small_kernel_output) |
| 111 | + np.savetxt("large_kernel.dat", large_kernel_output) |
| 112 | + |
| 113 | + circle = create_circle(50, 2, 0.5) |
| 114 | + |
| 115 | + # Normalization |
| 116 | + circle = circle / np.linalg.norm(circle) |
| 117 | + |
| 118 | + # using the circle for sobel operations as well |
| 119 | + sobel_output = compute_sobel(circle) |
| 120 | + |
| 121 | + np.savetxt("sobel_output.dat", sobel_output) |
| 122 | + |
0 commit comments