运行 CannyEdgeDetector.java 示例 Java

Running CannyEdgeDetector.java Example Java

我正在尝试 运行 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

您读取的文件的图像类型为6,对应BufferedImage.TYPE_4BYTE_ABGR。 CannyEdgeDetector 无法识别此图像类型(有关允许的图像类型,请参阅此 class 的 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);