如何将多个 java.awt.image.BufferedImage 传递给 Matlab
我正在编写一个用于实时图像处理的 Java 应用程序。我希望能够验证所使用的算法或随后使用 Matlab 在 Java 应用程序中呈现我获取的数据。在 Java 应用程序中,我使用 java.awt.image.BufferedImage 在内部处理数据。该程序通常运行约1-2分钟,以25Hz、160x120像素8位灰度抓取数据,因此每帧栅格权重为19200字节,使其每分钟获取30MB的数据。
向 Matlab 提供这些数据的最佳方式是什么?
我的目标是拥有一个最后包含所有帧的 Matlab 数组。我考虑过将帧存储在未压缩的 avi 中(我还不知道该怎么做),但也许有一种更简单、既定的方法来做到这一点?我已阅读此内容 ,但还不知道如何使用它,也不知道这是否是处理多个帧的正确方法。
感谢您的任何帮助。
I'm writing a Java application for real-time image processing. I'd like to be able to validate the algorithms used or present data I acquire in the Java app with Matlab afterwards. In the Java app I'm using java.awt.image.BufferedImage
's to process data internally. The program usually runs about 1-2 minutes and grabs data at 25Hz, 160x120 pixels 8-bit grayscale, so each frame raster weights 19200 bytes, making it 30MB of acquired data per minute.
What's the best way to make this data available to Matlab?
My aim is to have a Matlab array with all the frames in it at the end. I've considered storing frames in an uncompressed avi (which I don't know how to do yet), but maybe there is an easier, established, way of doing it? I've already read this, but wouldn't know how to use it yet or if this is the right way to handle multiple frames.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够将图像存储在内存中的数组中 - 只要您确保为 JVM 提供足够的堆内存(java 的 -Xmx 选项)。
捕获图像后,您可以使用以下命令将它们写入磁盘:
ImageIO.write(bufferedImage, "png", file)
如果您将它们全部放在具有简单文件名的目录中,例如“ img-00001.png”、“img-00002.png”,那么您应该能够在 Matlab 中加载图像序列。
You should be able to store the images in an array in memory - as long as you make sure you give the JVM enough heap memory (-Xmx option to java).
Once you have captured the images, you can write them to disk using, e.g.:
ImageIO.write(bufferedImage, "png", file)
If you put them all in the directory with a simple filename e.g. "img-00001.png", "img-00002.png", then you should be able to load in the sequence of images in Matlab.
我想我找到了一个非常干净的解决方案来解决我的问题,所以我将自己将其作为答案发布。有一个简单的开源 java 框架用于 .mat 文件导出,名为 JMatIO (也可在SourceForge,不过这里一定要下载最新版本0.2)。它允许轻松导出 .mat 格式的数据,然后以简单的方式供 Matlab 使用。如果您在使用它时遇到任何问题,由于文档很少,请从此处下载源代码:
并查看单元测试,它们显示了如何使用它。
回到我的问题:我需要将一系列帧上传到 Matlab。 Matlab 中的图像是 2D 矩阵,因此它们的突发将是 3D 矩阵。不过,我不知道如何在 Matlab 中将 Java 数组导出为 3D 矩阵,因此我在 Matlab 中将每个 java.awt.image.BufferedImage 导出为一行,这很容易完成。在 Matlab 中需要进行一些简单的数据操作才能按照随后想要的方式表示数据(矩阵转置和重塑函数)。看看下面的代码片段。函数导出将缓冲 100 帧,如果之后再次调用,它将把它们导出到具有 8 位颜色深度的文件中。
随意使用和更改它,希望它对某人有所帮助。还支持增量 .mat 文件写入,请参阅源代码。
I think I found a pretty clean solution for my problem, so I'll publish it as an answer myself. There is a simple open source java framework for .mat file exporting called JMatIO (also available at SourceForge, but here be sure to download the newest version 0.2). It allows to easily export data in .mat format which is then available to Matlab in a straightforward manner. If you have any problems using it, due to little documentation, download the source from here:
and take a look at unit tests, they show how it can be used.
Back to my problem: I needed to upload a burst of frames to Matlab. An image in Matlab is a 2D matrix, so a burst of them would be a 3D matrix. I didn't figure out how to export Java arrays as 3D martices in Matlab though, so I exported each
java.awt.image.BufferedImage
as a row in Matlab, which is easily done. It takes some trivial data manipulation in Matlab to represent the data as one would want to afterwards (matrix transpose and reshape function). Take a look at the code snippet below. The function export will buffer 100 frames and if called again after that it'll export them to a file with 8-bit color depth.Feel free to use and change it, hope it helps someone. Incremental .mat-file writing is also supported, consult the source code for that.