将JPG图像转换为Mat opencv UnsupportedOperationException

时间:2016-04-13 03:47:13

标签: java opencv mat unsupportedoperation

我试图将缓冲的图像转换为Matrix,但它会抛出一个我以前从未见过的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)   

1 个答案:

答案 0 :(得分:0)

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