2

我正在尝试将缓冲图像转换为矩阵,但它会引发 UnsupportedOperationException,这是我以前从未见过的

public static Mat readMatImage(String path) {
    Mat mat = null;
    BufferedImage image;
    try {
        image = ImageIO.read(new FileInputStream(path));
        mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
        byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        System.out.println(data[data.length - 1]);
        mat.put(0, 0, data);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return mat;
}
Exception in thread "main" java.lang.UnsupportedOperationException: Provided data element number (4000000) should be multiple of the Mat channels count (3)   
4

1 回答 1

0

我认为您应该考虑您的读取图像类型,因为您的图像必须是单通道和 8 位才能将其转换为 mat 元素。如果您的图像是 RGB,请尝试将其转换为二进制图像。

于 2016-04-13T09:18:13.903 回答