× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Matlab to Java - How do I plot data and show images in Java like in MATLAB?

Ray Phan
May 20, 2015
<p>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 <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop" rel="nofollow">Read-Eval-Print-Loop</a> where you can interactively communicate with MATLAB and see results immediately.</p> <p>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 <strong>very</strong> difficult, or find external libraries or code to help you do that.</p> <p>Let's address your concern with plotting. There are <strong>many</strong> (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:</p> <p><a href="https://github.com/yannrichet/jmathplot" rel="nofollow">https://github.com/yannrichet/jmathplot</a></p> <p>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 <code>JFrame</code> for you.</p> <p>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 <code>ImageIO</code> class, Java can read in GIF, PNG, JPEG, BMP, and WBMP natively, and there are extensions if you want TIF or JPEG 2000. See <a href="https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html" rel="nofollow">here for more details</a>. However, there is no native support for PGM images. </p> <p>You have two options:</p> <ol> <li><p>Use MATLAB and read in the PGM image into MATLAB and resave it as a PNG, JPEG, or whichever data type is supported by <code>ImageIO</code>. Once you do that, it's simply just doing this assuming that you imported the right libraries: <code>BufferedImage, ImageIO</code>, etc.:</p> <pre><code>BufferedImage img = null; try { img = ImageIO.read(new File("image_to_load.jpg")); } catch (IOException e) { } </code></pre> <p>Once you do that, skip down to the part where I talk about loading in a <code>BufferedImage</code> and displaying it on a <code>JFrame</code> below.</p></li> <li><p>Write your own PGM parser. If you want to do that, here's some code on StackOverflow that reads in PGM images:</p> <p><a href="http://stackoverflow.com/questions/3639198/how-to-read-pgm-images-in-java">How to read PGM images in Java?</a></p> <p>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</p> <ul> <li>Line 1 - <code>P5</code> - denotes that this is a grayscale PGM image</li> <li>Line 2 - Comment delimited by a <code>#</code> 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.</li> <li>Line 3 - Two integers that are the width and height respectively</li> <li>Line 4 - The maximum intensity encountered in the image</li> </ul> <p>What follows after these lines are the image data itself in <strong>raster-scan</strong> 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 (<code>width x height</code>). The code I linked above gives you a good Java implementation on how to read in the pixels into a 2D integer array.</p> <p>What you need to do next is convert this so that you get a single <strong>interleaved</strong> pixel array, then creating a <code>BufferedImage</code> type out of this. You can figure that out by going here:</p> <p><a href="http://stackoverflow.com/questions/21576096/convert-a-2d-array-of-doubles-to-a-bufferedimage">Convert a 2D array of doubles to a BufferedImage</a></p></li> </ol> <hr> <p>Once you get this into the right format, your goal now is to display the image. Simply use the <code>BufferedImage</code> instance you created and display it in a <code>JFrame</code>. That can be done here:</p> <p><a href="http://stackoverflow.com/questions/1626735/how-can-i-display-a-bufferedimage-in-a-jframe">How can I display a BufferedImage in a JFrame?</a></p> <p>All you're doing is creating a new <code>ImageIcon</code> and placing this inside a <code>JLabel</code> container and adding this container to a <code>JFrame</code>. This will finally give you the contents of the image for display.</p> <hr> <p>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 <code>who</code>. 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.</p> <hr> <p>Hope this helps!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30241745/Matlab%20to%20Java%20-%20How%20do%20I%20plot%20data%20and%20show%20images%20in%20Java%20like%20in%20MATLAB?/30247458">Stack Overflow</a>.</p>
comments powered by Disqus