画像のエッジを見つけようとします。私の主な問題は、エッジ検出に白い境界線があるのはなぜですか?
ここにコード:
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;
}
例としての入力画像は次のとおりです。
出力ソーベル エッジ検出:
境界線が見えない場合は、画像を保存して、IrfanView または背景が黒/暗いもので開くことができます (境界線が白いため)。