Created
November 23, 2018 21:58
-
-
Save fxfactorial/00f3239c9e50cf25e12e290cc91b01db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
green_matrix = np.array( | |
[ | |
[1, 1, 1, 0, 0], | |
[0, 1, 1, 1, 0], | |
[0, 0, 1, 1, 1], | |
[0, 0, 1, 1, 0], | |
[0, 1, 1, 0, 0], | |
] | |
) | |
# This is our kernel | |
orange_matrix = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]]) | |
def compute_conv_matrix(image_matrix, kernel): | |
kernel_height, kernel_width = kernel.shape | |
image_height, image_width = image_matrix.shape | |
conv_matrix = np.zeros(kernel.shape) | |
height_window = kernel_height | |
row_start = 0 | |
while height_window < image_height + 1: | |
height_offset = image_height - height_window | |
m = image_matrix[row_start : image_height - height_offset] | |
column_offset = 0 | |
while True: | |
end_column = column_offset + kernel_width | |
if end_column > image_width: | |
break | |
submatrix = m[:, column_offset:end_column] | |
kernel_filter = submatrix * kernel | |
conv_matrix[row_start, column_offset] = kernel_filter.sum() | |
column_offset += 1 | |
row_start += 1 | |
height_window += 1 | |
return conv_matrix | |
print(compute_conv_matrix(green_matrix, orange_matrix)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment