Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple edge detection method Java

Tags:

java

pixel

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);
    }
  }
}
like image 763
Zach M. Avatar asked Dec 12 '25 05:12

Zach M.


2 Answers

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);
like image 166
us2012 Avatar answered Dec 14 '25 20:12

us2012


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

}

like image 29
Milk Man Avatar answered Dec 14 '25 18:12

Milk Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!