运行CannyEdgeDetector.java示例Java

时间:2015-04-21 18:57:58

标签: java bufferedimage edge-detection

我试图运行CannyEdgeDetector.java示例:This Example

public static void main(String args[]) {

    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("paper3.png"));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    CannyEdgeDetector detector = new CannyEdgeDetector();
    detector.setSourceImage(img);
    detector.process();
    BufferedImage edges = detector.getEdgesImage();

    File saveFile = new File("out.png");
    try {
        ImageIO.write(edges, "png", saveFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

即将获得Exception in thread "main" java.lang.IllegalArgumentException: Unsupported image type: 6

1 个答案:

答案 0 :(得分:1)

您读取的文件的图像类型是6,它对应于BufferedImage.TYPE_4BYTE_ABGR。 CannyEdgeDetector无法识别此图像类型(有关允许的图像类型,请参阅此类的readLuminance()方法)。您可以通过绘制相应类型的新图像将读取的图像转换为适当的图像类型:

BufferedImage bufImg = ImageIO.read( imageFile );
BufferedImage convertedImg = new BufferedImage(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = convertedImg.createGraphics();
g2d.drawImage(bufImg, 0, 0, null);
g2d.dispose();
....
///now feed convertedImg  into the CannyEdgeDetectory
detector.setSourceImage(convertedImg);