简单的边缘检测方法Java

时间:2013-02-08 04:03:30

标签: 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);
    }
  }
}

2 个答案:

答案 0 :(得分:4)

new Color(1,1,1)调用Color(int,int,int)的{​​{1}}构造函数,其值介于0到255之间。因此,您的Color仍然基本上是黑色的(嗯,非常深灰色,但不是足以注意到。)

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

Color(float,float,float)

答案 1 :(得分:0)

public void edgeDetection(int edgeDist)
  {
Pixel leftPixel = null;
Pixel rightPixel = null;
Pixel bottomPixel=null;
Pixel[][] pixels = this.getPixels2D();
Color rightColor = null;
boolean black;
for (int row = 0; row < pixels.length; row++)
{
  for (int col = 0; 
       col < pixels[0].length; col++)
  {
    black=false;
    leftPixel = pixels[row][col];
    if (col<pixels[0].length-1)
    {
      rightPixel = pixels[row][col+1];
      rightColor = rightPixel.getColor();
      if (leftPixel.colorDistance(rightColor) > 
          edgeDist)
        black=true;
    }
    if (row<pixels.length-1)
    {
      bottomPixel =pixels[row+1][col];
      if (leftPixel.colorDistance(bottomPixel.getColor())>edgeDist)
        black=true;

    }
    if (black)
      leftPixel.setColor(Color.BLACK);
    else
      leftPixel.setColor(Color.WHITE);
  }
}

}