图像处理卷积:为什么我从np滑动我的核.不等(pad, imgWidth +垫)?



我正在尝试学习核卷积图像处理。现在,我理解了内核卷积的概念,但是我对我在https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/

找到的代码有点困惑。具体来说,我对for循环的边界和卷积输出的位置感到困惑。

def convolve(image, kernel):
# grab the spatial dimensions of the image, along with
# the spatial dimensions of the kernel
(iH, iW) = image.shape[:2]
(kH, kW) = kernel.shape[:2]
# allocate memory for the output image, taking care to
# "pad" the borders of the input image so the spatial
# size (i.e., width and height) are not reduced
pad = (kW - 1) // 2
image = cv2.copyMakeBorder(image, pad, pad, pad, pad,
cv2.BORDER_REPLICATE)
output = np.zeros((iH, iW), dtype="float32")

# loop over the input image, "sliding" the kernel across
# each (x, y)-coordinate from left-to-right and top to
# bottom
#QUESTION 1 SECTION BEGIN
for y in np.arange(pad, iH + pad):
for x in np.arange(pad, iW + pad):
# extract the ROI of the image by extracting the
# *center* region of the current (x, y)-coordinates
# dimensions
roi = image[y - pad:y + pad + 1, x - pad:x + pad + 1]
#QUESTION 1 SECTION END
# perform the actual convolution by taking the
# element-wise multiplication between the ROI and
# the kernel, then summing the matrix
k = (roi * kernel).sum()
#QUESTION 2 SECTION BEGIN
# store the convolved value in the output (x,y)-
# coordinate of the output image
output[y - pad, x - pad] = k
#QUESTION 2 SECTION END

问题1:为什么np。从pad到iH+pad,而不是从pad到iH+pad ?我假设我们从pad开始,这样感兴趣区域的中心像素就不会在图像的边缘上。然而,我认为使用h +pad会过调,并使中心像素最终超出图像尺寸。

问题2:这段代码让我们将输出像素存储在我的卷积roi居中的左边和上面的位置,不是吗?如果是这样,有人能解释一下这样做背后的逻辑吗?

谢谢!

np.arange(pad, iH + pad)运行超过iH像素,这是原始输入图像的宽度。填充图像的宽度为iH + 2*pad,因此这是从图像列的末尾从pad像素运行到pad像素,这样就可以在两个方向上索引pad像素,而无需退出填充图像。

关于你的第二个问题:输入图像被填充,索引是到填充的图像。image[pad,pad]获取原始图像在填充前的左上角像素,对应output[0,0]output没有填充