当前正在通过斯坦福大学CS131的免费在线课程学习计算机视觉和机器学习。遇到了一些沉重的数学公式,并想知道是否有人可以只知道图像的高度,宽度和核的高度和宽度,向我解释如何为卷积算法实现朴素的4个嵌套的for循环。通过在线研究,我提出了这个解决方案。
image_padded = np.zeros((image.shape[0] + 2, image.shape[1] + 2))
image_padded[1:-1, 1:-1] = image
for x in range(image.shape[1]): # Loop over every pixel of the image
for y in range(image.shape[0]):
# element-wise multiplication of the kernel and the image
out[y, x] = (kernel * image_padded[y:y + 3, x:x + 3]).sum()
Run Code Online (Sandbox Code Playgroud)
通过使用这种算法的一些网站示例,我能够理解这一点,但是,我似乎无法理解4个嵌套的for循环是如何做到的。而且,如果可以的话,可以将公式分解为更容易理解的公式,然后再从网上找到给定的数学公式。
编辑:只是为了澄清一下,我留下的代码片段在某种程度上可以正常工作,我试图提出一种解决方案,该解决方案的优化程度较低,对初学者更友好,例如此代码在询问什么:
def conv_nested(image, kernel):
"""A naive implementation of convolution filter.
This is a naive implementation of convolution using 4 nested for-loops.
This function computes convolution of an image with a …Run Code Online (Sandbox Code Playgroud)