卷积 - 计算矢量化图像的邻域元素索引 [英] Convolution - Calculating a Neighbour Element Index for a Vectorised Image

查看:168
本文介绍了卷积 - 计算矢量化图像的邻域元素索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下矩阵在矩阵卷积运算中充当图像和内核:

Assume the following matrix acts as both an image and a kernel in a matrix convolution operation:

0 1 2  
3 4 5  
6 7 8

要计算您将使用的邻居像素索引以下公式:

To calculate the neighbour pixel index you would use the following formula:

neighbourColumn = imageColumn + (maskColumn - centerMaskColumn);
neighbourRow = imageRow + (maskRow - centerMaskRow);

因此卷积的输出将是:

output1 = {0,1,3,4} x {4,5,7,8} = 58
output2 = {0,1,2,3,4,5} x {3,4,5,6,7,8} = 100 
output2 = {1,2,4,5} x {3,4,6,7} = 70
output3 = {0,1,3,4,6,7} x {1,2,4,5,7,8} = 132
output4 = {0,1,2,3,4,5,6,7,8} x {0,1,2,3,4,5,6,7,8} = 204
output5 = {1,2,4,5,7,8} x {0,1,3,4,6,7} = 132
output6 = {3,4,6,7} x {1,2,4,5} = 70
output7 = {3,4,5,6,7,8} x {0,1,2,3,4,5} = 100
output8 = {4,5,7,8} x {0,1,3,4} = 58

因此输出矩阵将是:

58  100 70
132 204 132
70  100 58

现在假设矩阵被展平以给出以下向量:

Now assume the matrix is flattened to give the following vector:

0 1 2 3 4 5 6 7 8

此向量现在充当矢量卷积运算中的图像和内核,输出应为:

This vector now acts as an image and a kernel in a vector convolution operation for which the ouput should be:

58 100 70 132 204 132 70 100 58

根据下面的代码,如何计算向量的邻居元素索引它与矩阵中的相同邻居元素相对应?

Given the code below how do you calculate the neighbour element index for the vector such that it corresponds with the same neighbour element in the matrix?

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;
      }
    }

    outputImage[i] = outputValue;
  }        

  return output;
}


推荐答案

邻居索引的计算方法是通过当前元素的索引与矩阵大小的一半之间的差来抵消原始像素索引。例如,要计算列索引:

The neighbour index is computed by offsetting the original pixel index by the difference between the index of the current element and half the size of the matrix. For example, to compute the column index:

int neighbourCol = imageCol + col - (size / 2);

我放了一个工作演示在GitHub上,试图让整个卷积算法尽可能可读:

I put a working demo on GitHub, trying to keep the whole convolution algorithm as readable as possible:

int[] dstImage = new int[srcImage.width() * srcImage.height()];

srcImage.forEachElement((image, imageCol, imageRow) -> {
  Pixel pixel = new Pixel();
  forEachElement((filter, col, row) -> {
    int neighbourCol = imageCol + col - (size / 2);
    int neighbourRow = imageRow + row - (size / 2);
    if (srcImage.hasElementAt(neighbourCol, neighbourRow)) {
      int color = srcImage.at(neighbourCol, neighbourRow);
      int weight = filter.at(col, row);
      pixel.addWeightedColor(color, weight);
    }
  });

  dstImage[(imageRow * srcImage.width() + imageCol)] = pixel.rgb();
});

这篇关于卷积 - 计算矢量化图像的邻域元素索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆