Tuesday, November 25, 2014

Graphics: Marching Squares - OOP Script



In this improved version of the Marching Squares algorithm - see description [Link] - I use an Object-Oriented Programming (OOP) approach...


For sake of convenience, this script uses OOP paradigm and I defined two classes named Square and Row to simplify the code of the main script.
These two classes are written in separate files and are included in the main script during the execution. A specific ImageJ Javascript function called load(<name-of-the-file.js>) is used.

Note: In ImageJ, it is possible to load external files in a JavaScript script since version 1.49k and only if you use the Sun/Oracle Java Virtual Machine ("HotSpot"). The other java implementations like OpenJDK don't use the Mozilla Rhino engine and can't load external files...
...

 1. Class Square

The class Square contains all we need to define a marching square.
  • Top left (X,Y) coordinates
  • Pixel values of the four vertices
  • Key defining the configuration and the lines to be drawn
  • Indexes of the vertices of the isocontoured lines


+++ Javascript:marchingSquare_Square.js +++
+++ End of Script: marchingSquare_Square.js +++

2. Class Row

The class row corresponds to a temporary array of marching squares (instances of the class Square) to keep track of the previously calculated squares. Two methods (or functions) previous() and above() allow to retrieve the squares of coordinates (X-1,Y) and (X,Y-1), respectively.

+++ Javascript:marchingSquareNoDoubles_Row.js +++
+++ End of Script: marchingSquareNoDoubles_Row.js +++

3. Core

The skeleton of this script is the same as previously [Link].
  • First, the function dialog() is called to set the various parameters.
  • Second, the whole image (or slice) is scanned by two loops along the Y- and X-axes. For each iteration, ...
    1.  A new marching square is created
    2. Its code is generated
    3. The function createVertices() is called to compute the segment line(s) and its start and end points.
    4. The marching square is stored in an array.
  • Third, all the vertices and segment lines are saved in a file thanks to the function saveAsOBJ().
The code was modified in the function createVertices(). In this modified function, we test two cases:
  1. If the point of the line segment is located in edges e1 or e2, segment line(s) is(are) created and and the corresponding coordinates of the vertices are computed using the function interpolate(...).
  2. If the point is located in edges e0 or e3, it is already calculated in a previous marching square. The methods previous() or above() of the row object are used to get the index of the corresponding vertex.


To run this script, first download the three files in the same directory and in ImageJ, go to File > Open... and choose the file marchingSquareNoDoubles.js. Then, run this script with Ctrl+R.
If this error message appears in the Log window, ...

ERROR: Too old version. Please, update your ImageJ version
-- End of the script --

... update your ImageJ version by going to Help > Update ImageJ


+++ Javascript:marchingSquareNoDoubles.js +++
+++ End of Script: marchingSquareNoDoubles.js +++


Hope that helps.


4. Other crazybiocomputing posts

Further readings are available in ...
  • Computer Graphics Series  [Link]
  • Image Processing TOC [Link]





No comments:

Post a Comment