我想将字节数组转换为 Mat 对象,但它抛出
java.lang.UnsupportedOperationException: Provided data element number (60181) should be multiple of the Mat channels count (3)
at org.opencv.core.Mat.put(Mat.java:992)
这是我的代码:
byte[] bytes = FileUtils.readFileToByteArray(new File("aaa.jpg"));
Mat mat = new Mat(576, 720, CvType.CV_8UC3);
//Imgcodecs.imencode(".jpg", mat, new MatOfByte(bytes));
mat.put(0, 0, bytes);
我尝试了很多方法,也搜索了很多,但没有找到任何解决方案。
注意:我知道 Imgcodecs.imread("aaa.jpg");
和
BufferedImage img = ImageIO.read(new ByteArrayInputStream(byteArray));
Mat mat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, ((DataBufferByte) img.getRaster().getDataBuffer()).getData());
但我想直接将字节数组转换为Mat而不需要任何额外的过程来加快处理时间。
提前致谢!
最佳答案
我是这样解决问题的:
byte[] bytes = FileUtils.readFileToByteArray(new File("aaa.jpg"));
Mat mat = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
现在它运行良好并且比 *bytes->BufferedImage->Mat*
快得多
关于java - 如何在 Java 中将字节数组转换为 Mat 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493941/