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


i newbie java. using eclipse.

i have written code in matlab, imports .pgm image 'imread' function, algorithm applies brushfire method on it.

the thing is, it's quite easy newbie me code , see results in same time, because matlab has such tools "workspace" (where can see image in matrix form) or plotting options, command window. how go in eclipse? how can write code , monitor results? eclipse have analogue "imread" function?

matlab (in cases) prototyping environment allows import in data (images in case), have tools plot data , has read-eval-print-loop can interactively communicate matlab , see results immediately.

one of biggest problems when working in environment complete transfer code environment... in case, java. if this, lot of matlab's capabilities go out of window , quite pain replicate. best thing can either write functionality yourself... very difficult, or find external libraries or code that.

let's address concern plotting. there many (i not kidding) java external libraries out there can plot 2d , 3d data. here's 1 of favourites github:

https://github.com/yannrichet/jmathplot

it's defining bunch of points in pair (or triple) or arrays , calling couple of methods. display of data in nice jframe you.

as images, gets bit more difficult. there built-in java libraries read in images you. specifically, if reference imageio class, java can read in gif, png, jpeg, bmp, , wbmp natively, , there extensions if want tif or jpeg 2000. see here more details. however, there no native support pgm images.

you have 2 options:

  1. use matlab , read in pgm image matlab , resave png, jpeg, or whichever data type supported imageio. once that, it's doing assuming imported right libraries: bufferedimage, imageio, etc.:

    bufferedimage img = null; try {     img = imageio.read(new file("image_to_load.jpg")); } catch (ioexception e) { } 

    once that, skip down part talk loading in bufferedimage , displaying on jframe below.

  2. write own pgm parser. if want that, here's code on stackoverflow reads in pgm images:

    how read pgm images in java?

    pgm images when examining actual data bunch characters. first 3 (or 4) lines deal header information itself. starting first fourth line, pgm images consist of

    • line 1 - p5 - denotes grayscale pgm image
    • line 2 - comment delimited # sign. in matlab when save pgm images, there comment tells saved in matlab. pgm files don't have this.
    • line 3 - 2 integers width , height respectively
    • line 4 - maximum intensity encountered in image

    what follows after these lines image data in raster-scan format. meant "raster-scan" format rows of image stacked 1 giant row , long total size of image (width x height). code linked above gives java implementation on how read in pixels 2d integer array.

    what need next convert single interleaved pixel array, creating bufferedimage type out of this. can figure out going here:

    convert 2d array of doubles bufferedimage


once right format, goal display image. use bufferedimage instance created , display in jframe. can done here:

how can display bufferedimage in jframe?

all you're doing creating new imageicon , placing inside jlabel container , adding container jframe. give contents of image display.


as replicating matlab's repl, not possible in limited amount of time. 1 thing, require writing own backend - text parsing , writing methods display things well. matlab great in whenever declare variables in workspace, have immediate access them in workspace window or using who. if want replicate same behaviour in java, that's going more difficult , not recommend if goal plot data or display images. let eclipse of work through use of debugger. use debugger instead , enough examine of variables , objects created @ given time.


hope helps!


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -