Java简单边缘检测方法

Zac*_* M. 3 java pixel

我正在研究Java中的方法来执行一些简单的边缘检测。我想采用两种颜色强度的差值,一种在一个像素处,另一种在其正下方的像素处。无论我为该方法设置了什么阈值,我正在使用的图片都被涂成黑色。我不确定我当前的方法是否只是不计算所需的内容,但是我茫然不知所措。

到目前为止,这是我的方法:

public void edgeDetection(double threshold)
{

  Color white = new Color(1,1,1);
  Color black = new Color(0,0,0);

  Pixel topPixel = null;
  Pixel lowerPixel = null;

  double topIntensity;
  double lowerIntensity;

  for(int y = 0; y < this.getHeight()-1; y++){
    for(int x = 0; x < this.getWidth(); x++){

      topPixel = this.getPixel(x,y);
      lowerPixel = this.getPixel(x,y+1);

      topIntensity =  (topPixel.getRed() + topPixel.getGreen() + topPixel.getBlue()) / 3;
      lowerIntensity =  (lowerPixel.getRed() + lowerPixel.getGreen() + lowerPixel.getBlue()) / 3;

      if(Math.abs(topIntensity - lowerIntensity) < threshold)
        topPixel.setColor(white);
      else
        topPixel.setColor(black);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

us2*_*012 5

new Color(1,1,1)调用其Color(int,int,int)构造函数,Color该构造函数的值介于0到255之间。因此,您Color white基本上还是黑色的(很好,非常深的灰色,但不足以引起注意)。

如果要使用Color(float,float,float)构造函数,则需要浮点文字:

Color white = new Color(1.0f,1.0f,1.0f);
Run Code Online (Sandbox Code Playgroud)