我试图找到图像的边缘。我的主要问题是,为什么我的边缘检测有白色边框?
这里是代码:
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;
}
以下是输入图像:
输出Sobel边缘检测:
如果您看不到边框,可以保存图像并使用IrfanView或其他具有黑/深背景的东西打开(因为边框有白色)
答案 0 :(得分:0)
这通常发生在使用内核矩阵的每个算法上,并且不处理边界像素没有所有邻居的事实。例如,x = 0处的所有像素都没有x-1个邻居,因此卷积矩阵中的许多位置没有强度(零值)。
在您的情况下,您可以忽略边界像素,只处理区间x [1,width-2]和y [1,height-2]中的像素。
答案 1 :(得分:0)
多数是因为你使用了门槛:
if(length <= threshold)
{
length = 0;
}
else
{
length = 255;
}
尝试为每个频道计算能量