Matlab 转 Java - 如何在 Java 中绘制数据并显示图像,就像在 MATLAB 中一样?

3

我完全是Java的新手。我正在使用Eclipse。

我用Matlab编写了代码,它使用'imread'函数导入.pgm图像,然后算法对其运用Brushfire方法。

问题是,对于像我这样的新手来说,在编写代码和同时看到结果方面非常容易,因为Matlab拥有诸如“工作空间”(在其中可以以矩阵形式查看图像)或绘图选项,还有命令窗口等工具。但是在Eclipse中如何实现呢?我该如何编写代码并监视结果?Eclipse是否也有类似于“imread”函数的功能?


1
你也可以查看我写的这个小绘图包装器:如何在Java中制作类似Matlab的图形。虽然不能完全回答问题,但对于从Matlab转到Java并尝试制作图形的人来说,这可能是一个很好的起点。 - Jean-Paul
1个回答

4
MATLAB(在大多数情况下)主要是一个原型环境,允许您轻松导入数据(在您的情况下是图像),拥有绘制数据的工具,还具有一个 Read-Eval-Print-Loop,您可以与MATLAB进行交互并立即看到结果。
在使用这样的完整环境时遇到的最大问题之一是将代码转移到另一个环境中...因此,在您的情况下是Java。如果这样做,许多MATLAB的功能都会消失,并且很难复制。您可以做的最好的事情是编写自己的功能...这非常困难,或者找到外部库或代码来帮助您执行此操作。
让我们解决您对绘图的担忧。有许多(我不是开玩笑)Java外部库可用于帮助您绘制2D和3D数据。以下是我从Github上选择的一个:
https://github.com/yannrichet/jmathplot
它只需要定义一些点在一对(或三个)数组中,并简单地调用几个方法。然后它会在一个漂亮的JFrame中显示所有这些数据。
至于图像,那就有点困难了。有内置的Java库可以为您读取图像。特别是,如果引用ImageIO类,Java可以本地读取GIF、PNG、JPEG、BMP和WBMP,如果您想要TIF或JPEG 2000,还有扩展。请参见此处以获取更多详细信息。但是,PGM图像没有本地支持。
你有两个选择:
  1. Use MATLAB and read in the PGM image into MATLAB and resave it as a PNG, JPEG, or whichever data type is supported by ImageIO. Once you do that, it's simply just doing this assuming that you imported the right libraries: BufferedImage, ImageIO, etc.:

    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("image_to_load.jpg"));
    } catch (IOException e) {
    }
    

    Once you do that, skip down to the part where I talk about loading in a BufferedImage and displaying it on a JFrame below.

  2. Write your own PGM parser. If you want to do that, here's some code on StackOverflow that reads in PGM images:

    How to read PGM images in Java?

    PGM images when examining the actual data are a bunch characters. The first 3 (or 4) lines deal with the header information itself. Starting from the first to fourth line, PGM images usually consist of

    • Line 1 - P5 - denotes that this is a grayscale PGM image
    • Line 2 - Comment delimited by a # sign. In MATLAB when you save PGM images, there is a comment that tells you that it was saved in MATLAB. Some PGM files don't have this.
    • Line 3 - Two integers that are the width and height respectively
    • Line 4 - The maximum intensity encountered in the image

    What follows after these lines are the image data itself in raster-scan format. What is meant by "raster-scan" format is that the rows of the image are stacked together into one giant row and is as long as the total size of the image (width x height). The code I linked above gives you a good Java implementation on how to read in the pixels into a 2D integer array.

    What you need to do next is convert this so that you get a single interleaved pixel array, then creating a BufferedImage type out of this. You can figure that out by going here:

    Convert a 2D array of doubles to a BufferedImage

一旦你将其格式化正确,你现在的目标就是显示图像。只需使用你创建的BufferedImage实例,并在JFrame中显示它即可。这可以在此处完成:
https://dev59.com/0nI-5IYBdhLWcg3w48xP
你所做的就是创建一个新的ImageIcon,并将其放置在JLabel容器中,然后将该容器添加到JFrame中。这最终将为你提供图像内容以供显示。
至于复制MATLAB的REPL,可能无法在有限的时间内完成。首先,它需要编写自己的后端 - 文本解析和编写方法来显示事物。MATLAB非常棒,因为每当你在工作区中声明变量时,你立即可以在工作区窗口或使用who访问它们。如果你想在Java中复制这种行为,那将更加困难,如果你的目标只是绘制数据或显示图像,我可能不建议你这样做。让Eclipse通过使用其调试器为您完成其中一些工作。我会建议使用调试器,希望这足以检查在特定时间创建的所有变量和对象。
希望这能帮到你!

2
非常感谢!听起来我有很多事情要做,但我希望我能成功。谢谢你! - Aidos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接