Created
September 1, 2022 19:39
-
-
Save cychitivav/8cd9cb9192540f1c69f1a32876fe2c8f to your computer and use it in GitHub Desktop.
Function to find the convolution of an image with a mask of size nxn (n odd)
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 cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def convolution2D(Img, Mask): | |
inv_mask = Mask[::-1,::-1] | |
dim = Mask.shape | |
img_conv = np.zeros(Img.shape) | |
for i in range(1,Img.shape[0]-1): | |
for j in range(1,Img.shape[1]-1): | |
img_conv[i, j] = np.abs(np.sum((Img[i-dim[0]//2:i+dim[0]//2+1, j-dim[0]//2:j+dim[0]//2+1] * inv_mask))) | |
return img_conv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code represents the following operation
Where: