Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Created November 23, 2018 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxfactorial/00f3239c9e50cf25e12e290cc91b01db to your computer and use it in GitHub Desktop.
Save fxfactorial/00f3239c9e50cf25e12e290cc91b01db to your computer and use it in GitHub Desktop.
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