-1

I try to find edge of image. The my main problem is, why my edge detection has white border?

Here the code :

public static BufferedImage executeSobelEdgeDetection(BufferedImage inputImage, int threshold) 
{
    int width = inputImage.getWidth();
    int height = inputImage.getHeight();
    double red = 0.0;
    double green = 0.0;
    double blue = 0.0;
    BufferedImage borderedImage = new BufferedImage(width+2, height+2, BufferedImage.TYPE_INT_RGB);

    for(int x = 0 ; x < width; x++)
    {
        for(int y = 0 ; y < height; y++)
        {
            Color color = new Color(inputImage.getRGB(x ,y));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            borderedImage.setRGB(x+1, y+1, new Color((int) red, (int) green, (int) blue).getRGB());
        }
    }

    BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);


    for (int x = 1; x <= width; x++) 
    {
        for (int y = 1; y <= height; y++) 
        {

            double gx = 0;
            double gy = 0;


            //Left Column Kernel
            Color color = new Color(borderedImage.getRGB(x-1 , y-1));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            double intensity = red + green + blue;
            gx = gx + (intensity) *-1;
            gy = gy + (intensity) *-1;

            color = new Color(borderedImage.getRGB(x-1 , y));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) *-2;
            gy = gy + (intensity) * 0;

            color = new Color(borderedImage.getRGB(x-1 , y+1));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) *-1;
            gy = gy + (intensity) * 1;

            //Middle Column Kernel
            color = new Color(borderedImage.getRGB(x , y-1));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) * 0;
            gy = gy + (intensity) * -2;

            color = new Color(borderedImage.getRGB(x , y));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) * 0;
            gy = gy + (intensity) * 0;

            color = new Color(borderedImage.getRGB(x , y+1));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) * 0;
            gy = gy + (intensity) * 2;

            //Right Column Kernel
            color = new Color(borderedImage.getRGB(x+1 , y-1));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) * 1;
            gy = gy + (intensity) * -1;

            color = new Color(borderedImage.getRGB(x+1 , y));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) * 2;
            gy = gy + (intensity) * 0;

            color = new Color(borderedImage.getRGB(x+1 , y+1));
            red = color.getRed();
            green = color.getGreen();
            blue = color.getBlue();
            intensity = red + green + blue;
            gx = gx + (intensity) * 1;
            gy = gy + (intensity) * 1;


            double length = Math.sqrt((Math.pow(gx, 2) + Math.pow(gy, 2)));

            length = length/4328 * 255;


            if(length <= threshold)
            {
                length = 0;
            }
            else
            {
                length = 255;
            }

            outputImage.setRGB(x-1, y-1, new Color((int) length, (int) length, (int) length).getRGB());
        }
    }

    return outputImage;
}

Here is input image as example :

enter image description here

Output Sobel Edge Detection :

enter image description here

If you cannot see the border, you can save the image and open with IrfanView or something else which has black/dark background (cause the border has white color )

Mr. Mike
  • 453
  • 5
  • 23
  • obviously you are in the same class as this guy http://stackoverflow.com/questions/37619514/my-sobel-edge-detection-operator-output-is-weird – gpasch Jun 03 '16 at 20:08
  • you are continuing useless operations on g - can you explain your approach? what is the purpose of intensity*-1 ?? etc – gpasch Jun 03 '16 at 20:09
  • @gpasch : i think it's different , his main problem in the first line in BufferedImage temp = img; (his primary fault cause he apply sobel operator to not finished edge image). My problem only at the border of image, why my code add white border of image. thanks for reply – Mr. Mike Jun 04 '16 at 03:27
  • @gpasch : you may read from here : https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/image-processing/edge_detection.html – Mr. Mike Jun 04 '16 at 03:27
  • @gpasch : if you ask, why i add 0 in border before convolution process, you may read this https://www.cs.auckland.ac.nz/courses/compsci373s1c/PatricesLectures/Convolution_1up.pdf (look at the last slide of that file) thanks again – Mr. Mike Jun 04 '16 at 04:03

2 Answers2

0

This usually happens with every algorithm that uses kernel matrices and don't handle the fact that border pixels do not have all neighbors. For instance, all pixels at x=0 don't have the x-1 neighbors, so many positions in the convolution matrix don't have intensity (zero value).

In your case, you can just disconsider border pixels processing only pixels in the intervals x[1,width-2] and y[1,height-2].

Gabriel Archanjo
  • 4,547
  • 2
  • 34
  • 41
0

Thats all because you use threshold:

    if(length <= threshold)
    {
        length = 0;
    }
    else
    {
        length = 255;
    }

try compute energy for each channel

NewBee
  • 1