Gaussian Filter without using ConvolveOp

自闭症网瘾萝莉.ら 提交于 2019-12-06 09:53:18

A convolution is essentially a quadruple nested loop: two to loop through the pixels in the image and, at each pixel, two to loop over the pixels in the kernel.

So you can clean up your code significantly with something like this:

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

     }
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!