MATLAB (in most cases) is primarily a prototyping environment that allows you to easily import in data (images in your case), have tools to plot data and also has a Read-Eval-Print-Loop where you can interactively communicate with MATLAB and see results immediately.
One of the biggest problems when working in an environment that is as complete like this is to transfer code to another environment... so in your case, Java. If you do this, a lot of MATLAB's capabilities go out of the window and are quite a pain to replicate. The best thing you can do is either write the functionality yourself... which is very difficult, or find external libraries or code to help you do that.
Let's address your concern with plotting. There are many (I am not kidding) Java external libraries out there that can help you plot 2D and 3D data. Here's one of my favourites from Github:
https://github.com/yannrichet/jmathplot
It's seriously just defining a bunch of points in a pair (or triple) or arrays and simply calling a couple of methods. It will then display all of this data in a nice JFrame
for you.
As for images, that gets a bit more difficult. There are built-in Java libraries that will read in images for you. Specifically, if you reference the ImageIO
class, Java can read in GIF, PNG, JPEG, BMP, and WBMP natively, and there are extensions if you want TIF or JPEG 2000. See here for more details. However, there is no native support for PGM images.
You have two options:
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
- 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
Once you get this into the right format, your goal now is to display the image. Simply use the BufferedImage
instance you created and display it in a JFrame
. That can be done here:
How can I display a BufferedImage in a JFrame?
All you're doing is creating a new ImageIcon
and placing this inside a JLabel
container and adding this container to a JFrame
. This will finally give you the contents of the image for display.
As for replicating MATLAB's REPL, that is probably not possible to do in a limited amount of time. For one thing, it would require writing your own backend - text parsing and writing methods to display things as well. MATLAB is great in that whenever you declare variables in the workspace, you have immediate access to them in the Workspace window or using who
. If you want to replicate this same behaviour in Java, that's going to be much more difficult and I would probably not recommend you do that if your goal is to just plot data or display images. Let Eclipse do some of that work for you through the use of their debugger. I would say use the debugger instead and hopefully that will be enough to examine all of the variables and objects that were created at a given time.
Hope this helps!