【问题标题】:Gaussian Filter without using ConvolveOp不使用 ConvolveOp 的高斯滤波器
【发布时间】:2012-04-26 09:39:37
【问题描述】:

我正在尝试在不使用 ConvolveOp 的情况下创建高斯滤波器。 我在试图让它工作时遇到了很多问题,我已经得到了一个灰度过滤器来工作,但是对于这个我在找到一个像素 8 个邻居的位置时遇到了问题,所以我可以应用过滤器。这是我到目前为止所拥有的。这是获取每个像素的正确方法吗?

public class Gaussian implements Filter {


   public void filter(PixelImage pi) {
    Pixel[][] data = pi.getData();
    Pixel[][] original = data;


    int kernel_rows = 3;
    int kernel_cols = 3;

    // define kernel here (double loop), these are the 1/16, 2/16, etc...
    // values that you're multiplying the image pixels by
    double[][] kernel = {{1,2,1},
      {2,4,2},
      {1,2,1}};

    // iterate over each pixel in the image
    for (int row = 0; row < pi.getHeight(); row ++) {
      for (int col = 0; col < pi.getWidth(); col++) {
        // iterate over each pixel in the kernel
        for (int row_offset = 0 ; row_offset < kernel_rows ; row_offset++ ) {
          for (int col_offset = 0 ; col_offset < kernel_cols ; col_offset++ ) {

            // subtract by half the kernel size to center the kernel
            // on the pixel in question
            // ** you'll have to modify to account for boundary conditions **
            int row_index = row + row_offset - kernel_rows/2;
            int col_index = col + col_offset - kernel_cols/2;

            int r =0;
            int g =0;
            int b =0;




            r += (data[row_index][col_index].red * kernel[row_offset][col_offset])/16;
            g += (data[row_index][col_index].green * kernel[row_offset][col_offset])/16;
            b += (data[row_index][col_index].blue * kernel[row_offset][col_offset])/16;
            Pixel temp =new Pixel( r, g, b );
            original[row][col] = temp;
          }
        }
        data = original;
        pi.setData(data);

      }
    }
   }
}

【问题讨论】:

    标签: java pixels gaussian


    【解决方案1】:

    卷积本质上是一个四重嵌套循环:两个循环遍历图像中的像素,并且在每个像素处,两个循环遍历内核中的像素。

    因此,您可以通过以下方式显着清理您的代码:

       int kernel_rows = 3;
       int kernel_cols = 3;
    
       // define kernel here (double loop), these are the 1/16, 2/16, etc...
       // values that you're multiplying the image pixels by
       double[][] kernel = ... 
    
       // iterate over each pixel in the image
       // leave a kernel_rows/2 sized gap around the edge of the image
       // so that we don't run into IndexOutOfBounds exceptions
       // when performing the convolution
       for (int row = kernel_rows/2; row < pi.getHeight() - kernel_rows/2; row ++) {
         for (int col = kernel_cols/2; col < pi.getWidth() - kernel_cols/2; col++) {
    
           int r = 0;
           int g = 0;
           int b = 0;
    
           // iterate over each pixel in the kernel
           for (int row_offset = 0 ; row_offset < kernel_rows ; row_offset++ ) {
             for (int col_offset = 0 ; col_offset < kernel_cols ; col_offset++ ) {
    
               // subtract by half the kernel size to center the kernel
               // on the pixel in question
               int row_index = row + row_offset - kernel_row/2;
               int col_index = col + col_offset - kernel_cols/2
    
               r += data[row_index][col_index].red * kernel[row_offset][col_offset];
               g += data[row_index][col_index].green * kernel[row_offset][col_offset];
               b += data[row_index][col_index].blue * kernel[row_offset][col_offset];
    
             }
           }
    
         data[row][col] = new Pixel( r, g, b );
    
         }
       }
    

    【讨论】:

    • 好的,所以我编辑了上面的代码。我收到 r, g,b 未初始化的错误。我尝试将它们放在循环之前,甚至最初给它一个零值。但我仍然没有初始化 r。
    • 您必须分别初始化它们(上面编辑过的代码)。
    • 好的,所以我想我明白了,但我仍然得到一个索引越界异常。我的内核阵列设置是否正确?我知道我的其余代码很好,因为我可以让其他过滤器工作。非常感谢你帮我解决这个问题!我已经编辑了上面的代码。
    • 当您尝试将内核应用于(0,0) 处的像素时,索引值row_indexcol_index 将是负数(将内核居中在(0,0) 上会将其扩展到图片。这就是我在代码中关于“修改以考虑边界条件”的评论的意思。
    • 您可以通过在1 开始外循环并在kernel_rows-1 结束来解决此问题(只是不计算像素外环的卷积)。对上面的代码进行了一些编辑以表明我的意思。当然,这只是一种处理方式。
    猜你喜欢
    • 2013-03-29
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 2018-01-26
    • 2013-02-01
    • 2014-10-02
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多