我完全是Java的新手。我正在使用Eclipse。
我用Matlab编写了代码,它使用'imread'函数导入.pgm图像,然后算法对其运用Brushfire方法。
问题是,对于像我这样的新手来说,在编写代码和同时看到结果方面非常容易,因为Matlab拥有诸如“工作空间”(在其中可以以矩阵形式查看图像)或绘图选项,还有命令窗口等工具。但是在Eclipse中如何实现呢?我该如何编写代码并监视结果?Eclipse是否也有类似于“imread”函数的功能?
我完全是Java的新手。我正在使用Eclipse。
我用Matlab编写了代码,它使用'imread'函数导入.pgm图像,然后算法对其运用Brushfire方法。
问题是,对于像我这样的新手来说,在编写代码和同时看到结果方面非常容易,因为Matlab拥有诸如“工作空间”(在其中可以以矩阵形式查看图像)或绘图选项,还有命令窗口等工具。但是在Eclipse中如何实现呢?我该如何编写代码并监视结果?Eclipse是否也有类似于“imread”函数的功能?
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.
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
P5
- denotes that this is a grayscale PGM image#
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.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: