Last active
January 30, 2022 14:40
-
-
Save yoya/fb5de57bd35b15333e1e919996b095ef to your computer and use it in GitHub Desktop.
convolve repeated edge
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 math | |
import numpy as np | |
def convolveRepeatedEdge(arr, kernel): | |
arrLen = len(arr) | |
kernelLen = len(kernel) | |
kernelLen_harf = math.floor((kernelLen - 1) / 2) | |
if type(arr) is list: | |
arr2 = [0] * arrLen | |
else: | |
arr2 = np.zeros(arrLen, dtype=arr.dtype) | |
for i in range(arrLen): | |
v = 0; | |
for k in range(kernelLen): | |
ii = i + k - kernelLen_harf | |
if ii < 0: | |
v += kernel[k] * arr[0] | |
elif ii < arrLen: | |
v += kernel[k] * arr[ii] | |
else: | |
v += kernel[k] * arr[arrLen-1] | |
arr2[i] = v; | |
return arr2; |
Author
yoya
commented
Jan 30, 2022
•
from scipy import ndimage
import numpy as np
import matplotlib.pyplot as plt
arr1 = [1]*4 + [0]*4;
arr2 = [0]*4 + [1]*4;
kernel = np.array([1, 2, 1]) / 4
arr11 = np.zeros(len(arr1))
arr22 = np.zeros(len(arr2))
ndimage.convolve(arr1, kernel, arr11, mode='nearest')
ndimage.convolve(arr2, kernel, arr22, mode='nearest')
plt.plot(arr11)
plt.plot(arr22)
plt.show()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment