I'm trying to use a sobel filter to do edge detection on a BufferedImage. But it seems like the Java filter system doesn't handle negative values in the image properly (while a manual implementation would just abs()
them). Is is possible to avoid this issue without writing the whole filter manually?
Code:
BufferedImage image, converted_image;
try {
image = ImageIO.read(new File("image.png"));
// convert image from ARGB to RGB
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
g.dispose();
image = newImage;
} catch (IOException ex) {
System.exit(1);
}
float sobel[] = {
1.0f, 0.0f, -1.0f,
2.0f, 0.0f, -2.0f,
1.0f, 0.0f, -1.0f
};
BufferedImageOp sobelFilter = new ConvolveOp(new Kernel(3, 3, sobel));
converted_image = sobelFilter.filter(image, null);
try {
ImageIO.write(converted_image, "png", new File("image2.png"));
} catch (IOException ex) {
System.exit(1);
}
Example image:
And the faulty edge detection. Note that while the filter should only handle vertical edges, it only handles black | white
edges and not white | black
ones.