【问题标题】:circular gradient blur filter圆形渐变模糊滤镜
【发布时间】:2016-03-18 21:55:48
【问题描述】:

我正在做一个学校作业,我需要创建一个圆形渐变雾过滤器。我对如何模糊图像进行了一些研究,并发现我需要获取当前像素周围像素的颜色值并对它们进行平均以设置模糊图像的新颜色。到目前为止,我只关注模糊方面,我有这个代码:

public void circularGradientBlur()
{
      Pixel regularPixel = null;
      Pixel L_regularPixel = null;
      Pixel R_regularPixel = null;
      Pixel T_regularPixel = null;
      Pixel B_regularPixel = null;
      Pixel blurredPixel = null;
      Pixel pixels[][] = this.getPixels2D();
      for (int row = 2; row < pixels.length; row++)
      {
         for (int col = 2; col < pixels[0].length - 1; col++)
         {
            regularPixel = pixels[row][col];
            if (row != 0 && col != 0 && row != 498 && col != 498)
            {
               L_regularPixel = pixels[row - 1][col];
               R_regularPixel = pixels[row + 1][col];
               T_regularPixel = pixels[row][col - 1];
               B_regularPixel = pixels[row][col + 1];
               blurredPixel.setRed((L_regularPixel.getRed() + R_regularPixel.getRed() + T_regularPixel.getRed() + B_regularPixel.getRed()) / 4);
               blurredPixel.setGreen((L_regularPixel.getGreen() + R_regularPixel.getGreen() + T_regularPixel.getGreen() + B_regularPixel.getGreen()) / 4);
               blurredPixel.setBlue((L_regularPixel.getBlue() + R_regularPixel.getBlue() + T_regularPixel.getBlue() + B_regularPixel.getBlue()) / 4);               
            }
         }
      }   
   }

当我尝试使用此代码时,在设置新颜色的行上出现 NullPointerException - blurredPixel.setRed()、blurredPixel.setGreen()、blurredPixel.setBlue()。这个错误让我很困惑,因为我有其他方法具有类似的代码并且不会抛出这个错误。如果能得到任何帮助,我将不胜感激!

【问题讨论】:

标签: java image-processing filter pixels


【解决方案1】:

您必须创建一个 blurredPixel 的实例。如果您调用 setter,它将始终为 null。

【讨论】:

  • 添加这样的代码blurredPixel添加一些示例和解释。
  • blurredPixel = new Pixel(); 然后做二传手。否则,您会尝试用不存在的东西做某事。 BOOM NullPointerException。
猜你喜欢
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多