我想将字节数组转换为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*
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句