Zach M.
Zach M.

Reputation: 1194

Simple edge detection method Java

I am working on a method in Java to do some simple edge detection. I want to take the difference of two color intensities one at a pixel and the other at the pixel directly below it. The picture that I am using is being colored black no matter what threshold I put in for the method. I am not sure if my current method is just not computing what I need it to but I am at a loss what i should be tracing to find the issue.

Here is my method thus far:

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

Upvotes: 3

Views: 11351

Answers (2)

Milk Man
Milk Man

Reputation: 73

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

}

Upvotes: 1

us2012
us2012

Reputation: 16253

new Color(1,1,1) calls the Color(int,int,int) constructor of Color which takes values between 0 and 255. So your Color white is still basically black (well, very dark grey, but not enough to notice).

If you want to use the Color(float,float,float) constructor, you need float literals:

Color white = new Color(1.0f,1.0f,1.0f);

Upvotes: 5

Related Questions