我正在尝试实现采用两个向量的卷积方法:图像;和一个内核。我的问题是,当我将内核“滑动”到图像矢量上时,我不知道如何计算图像相邻元素的索引。例如,对于两个相同的向量 {0, 1, 2, 3, 4, 5, 6, 7, 8} 我想实现以下结果:
到目前为止,我的代码如下:
public int[] convolve(int[] image, int[] kernel)
{
int imageValue;
int kernelValue;
int outputValue;
int[] outputImage = new int[image.length()];
// loop through image
for(int i = 0; i < image.length(); i++)
{
outputValue = 0;
// loop through kernel
for(int j = 0; j < kernel.length(); j++)
{
neighbour = ?;
// discard out of bound neighbours
if (neighbour >= 0 && neighbour < imageSize)
{
imageValue = image[neighbour];
kernelValue = kernel[j];
outputValue += imageValue * kernelValue;
}
}
output[i] = outputValue;
}
return output;
}