Difference between revisions of "MATLAB Scripting"
(Add source links) 
(Fix "list of update sites" link) 

(64 intermediate revisions by 6 users not shown)  
Line 1:  Line 1:  
−  {{WarningMATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: [http://www.mathworks.com/matlabcentral/answers/130359howdoichangethejavavirtualmachinejvmthatmatlabisusingonwindows Windows], [http://www.mathworks.com/matlabcentral/answers/103056howdoichangethejavavirtualmachinejvmthatmatlabisusingformacos macOS] or [http://www.mathworks.com/matlabcentral/answers/130360howdoichangethejavavirtualmachinejvmthatmatlabisusingforlinuxos Linux].}}  +  {{WarningPrior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: [http://www.mathworks.com/matlabcentral/answers/130359howdoichangethejavavirtualmachinejvmthatmatlabisusingonwindows Windows], [http://www.mathworks.com/matlabcentral/answers/103056howdoichangethejavavirtualmachinejvmthatmatlabisusingformacos macOS] or [http://www.mathworks.com/matlabcentral/answers/130360howdoichangethejavavirtualmachinejvmthatmatlabisusingforlinuxos Linux]. 
−  {{  +  
+  If you run MATLAB R2017b and later, you don't need to do this, as it ships and uses Java 8 already.  
+  }}  
+  {{Learn  languages}}'''ImageJMATLAB''' is an extension which:  
* Translates data between ImageJ images and MATLAB matrices.  * Translates data between ImageJ images and MATLAB matrices.  
* Enables execution of MATLAB scripts from inside ImageJ's [[Script Editor]].  * Enables execution of MATLAB scripts from inside ImageJ's [[Script Editor]].  
Line 10:  Line 13:  
== Prerequisites ==  == Prerequisites ==  
−  * Add the [[  +  * Add the {{ListOfUpdateSitesImageJMATLAB}} update site. See [[Following an update site]] for more detail. 
+  *# You go to <code>Help > Update...</code>  
+  *# Once checking status is done, open <code>Manage update sites</code>  
+  *# Tick <code>ImageJMATLAB</code> and then <code>Close</code> [[File:Manage_update_sites_ImageJ_MATLAB.png]]  
+  *# And then click <code>Apply changes</code> on ImageJ Updater  
+  *# This will literally update and replace the existing, nonfunctional <code>ImageJ.m</code> file in the scripts folder with the genuine one.  
* You will need to install your own licensed copy of [http://www.mathworks.com/products/matlab/ MATLAB]. All that is provided with ImageJ are adapters for evaluating scripts written in ImageJ to [[MATLAB]], and converters between ImageJ and [[MATLAB]] data structures.  * You will need to install your own licensed copy of [http://www.mathworks.com/products/matlab/ MATLAB]. All that is provided with ImageJ are adapters for evaluating scripts written in ImageJ to [[MATLAB]], and converters between ImageJ and [[MATLAB]] data structures.  
−  * MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: [http://www.mathworks.com/matlabcentral/answers/130359howdoichangethejavavirtualmachinejvmthatmatlabisusingonwindows Windows], [http://www.mathworks.com/matlabcentral/answers/103056howdoichangethejavavirtualmachinejvmthatmatlabisusingformacos macOS] or [http://www.mathworks.com/matlabcentral/answers/130360howdoichangethejavavirtualmachinejvmthatmatlabisusingforlinuxos Linux].  +  * Prior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: [http://www.mathworks.com/matlabcentral/answers/130359howdoichangethejavavirtualmachinejvmthatmatlabisusingonwindows Windows], [http://www.mathworks.com/matlabcentral/answers/103056howdoichangethejavavirtualmachinejvmthatmatlabisusingformacos macOS] or [http://www.mathworks.com/matlabcentral/answers/130360howdoichangethejavavirtualmachinejvmthatmatlabisusingforlinuxos Linux]. If you run MATLAB R2017b and later, you don't need to do this, as it ships and uses Java 8 already. 
−  * If you'  +  ** The command <code>version java</code> will return the current Java version used by MATLAB 
+  * If you're new to [[MATLAB]], first check out Mathworks' [http://www.mathworks.com/help/matlab/gettingstartedwithmatlab.html getting started guide].  
* If you're familiar with the [[MATLAB]] language but haven't written .m scripts before, look through the [http://www.mathworks.com/help/matlab/scripts.html script examples].  * If you're familiar with the [[MATLAB]] language but haven't written .m scripts before, look through the [http://www.mathworks.com/help/matlab/scripts.html script examples].  
+  
+  === Memory issue ===  
+  * In order to handle large images, the default Java Heap Memory size assigned won't be enough and you may get the error <code>java.lang.OutOfMemoryError: Java heap space</code>.  
+  * From MATLAB R2010a onward, you can increase the Jave Heap Memory size from '''Preferences > General > Java Heap Memory'''. [[File:MATLAB_java_heap_memory.png]]  
+  * However, the maximum value allowed in the Preferences can still be too small for your purpose. In that case, you can directly edit <code>matlab.prf</code> file in the folder specified by the <code>prefdir<\code> MATLAB function (eg. <code>C:\Users\xxxxxxx\AppData\Roaming\MathWorks\MATLAB\R2018b</code>). Find the parameter <code>JavaMemHeapMax</code> in the file and increase the number that follows the capital I (in MB) to increase the maximum Java heap memory size. The change will be reflected by the Preferences as above.  
+  <source>  
+  JavaMemHeapMax=I25000  
+  </source>  
+  * Alternatively, this can also be done by creating a <code>java.opts</code> file in [https://mathworks.com/help/matlab/matlab_env/matlabstartupfolder.html the startup directory] and overriding the default memory settings (see [https://mathworks.com/help/matlab/matlab_env/javaoptsfile.html this documentation] for more information). For instance, <code>Xmx512m</code> in your <code>java.opts</code> file may be a good start point.  
+  * For the common "Out of Memory" error, see [[Troubleshooting#OutOfMemoryError the Troubleshooting page]].  
== Creating MATLAB scripts inside ImageJ ==  == Creating MATLAB scripts inside ImageJ ==  
Line 20:  Line 39:  
Using the [[Script Editor]] you will be able to select [[MATLAB]] from the [[Script_Editor#Choosing_a_language  language menu]]. You can also install and run .m scripts via the [[Scripting_Help#Creating_scripts_and_using_.22refresh_scripts.22  standard script plugin infrastructure]].  Using the [[Script Editor]] you will be able to select [[MATLAB]] from the [[Script_Editor#Choosing_a_language  language menu]]. You can also install and run .m scripts via the [[Scripting_Help#Creating_scripts_and_using_.22refresh_scripts.22  standard script plugin infrastructure]].  
−  Actually running a [[MATLAB]] script from ImageJ is effectively like calling [http://www.mathworks.com/help/matlab/ref/eval.html eval] on the script's contents. The script will be evaluated as such in a remote [[MATLAB]] instance (which will be launched automatically, if needed). Note that only scripts, not functions, can be evaluated in this way. See [http://www.mathworks.com/help/matlab/learn_matlab/scriptsandfunctions.html the MATLAB documentation] for an explanation of these concepts.  +  Actually running a [[MATLAB]] script from ImageJ is effectively like calling <code>[http://www.mathworks.com/help/matlab/ref/eval.html eval]</code> on the script's contents. The script will be evaluated as such in a remote [[MATLAB]] instance (which will be launched automatically, if needed). Note that only scripts, not functions, can be evaluated in this way. See [http://www.mathworks.com/help/matlab/learn_matlab/scriptsandfunctions.html the MATLAB documentation] for an explanation of these concepts. 
+  <blockquote>  
+  NB: Because of the similarity to <code>eval</code>, when you need to assign character vector or string with special characters, they need to be '''escaped'''. A typical example is the handling of file/folder paths in Windows. Thus,  
+  
+  <source lang="matlab">  
+  s = 'C:\Users\xxxxx\Desktop'  
+  </source>  
+  will end up in an error in MATLAB:  
+  <source>  
+  Warning: Escaped character '\U' is not valid. See 'doc sprintf' for  
+  supported special characters.  
+  scijava_script523255 =  
+  's = 'C:'  
+  Error: Character vector is not terminated properly.  
+  </source>  
+  You need to escape backslashes as below:  
+  <source lang="matlab">  
+  s = 'C:\\Users\\xxxxx\\Desktop'  
+  </source>  
+  <source>  
+  s =  
+  'C:\Users\xxxxx\Desktop'  
+  </source>  
+  For more details, see documetation for <code>sprintf</code> and learn about Special Characters.  
+  
+  </blockquote>  
Options for controlling the startup of [[MATLAB]], or killing existing [[MATLAB]] processes (e.g. if hidden) can be accessed via:  Options for controlling the startup of [[MATLAB]], or killing existing [[MATLAB]] processes (e.g. if hidden) can be accessed via:  
{{bc  Edit  Options  MATLAB...}}  {{bc  Edit  Options  MATLAB...}}  
+  [[File:MATLAB_options.png]]  
+  
+  <blockquote>  
NB: because the script is being passed from ImageJ to a remote [[MATLAB]], [[MATLAB]] will not have access to ImageJ's classpath. Objects can be passed as variables to [[MATLAB]] (e.g. by using @ annotation) but only if they are valid [[MATLAB]] classes or specially handled classes.  NB: because the script is being passed from ImageJ to a remote [[MATLAB]], [[MATLAB]] will not have access to ImageJ's classpath. Objects can be passed as variables to [[MATLAB]] (e.g. by using @ annotation) but only if they are valid [[MATLAB]] classes or specially handled classes.  
+  
+  Scripts requiring ImageJ classes without autoconversion support should [[#Running ImageJ within MATLAB  launch ImageJ from within MATLAB]].  
+  
+  eg. a script using <code>[https://javadoc.scijava.org/ImageJ1/ij/IJ.html ij.IJ]</code> commands needs to be run from within MATLAB (or you'll need to add Java class paths to MATLAB search path before calling <code>ij.IJ</code> commands).  
+  
+  </blockquote>  
For example, by default all [http://matlabcontrol.googlecode.com/svn/javadocs/doc/matlabcontrol/extensions/MatlabNumericArray.html MatlabNumericArrays] will be converted to matrices within [[MATLAB]]. We also support autoconversion of ImageJ Datasets out of the box, which can be read in by scripts using "@matrix" parameters:  For example, by default all [http://matlabcontrol.googlecode.com/svn/javadocs/doc/matlabcontrol/extensions/MatlabNumericArray.html MatlabNumericArrays] will be converted to matrices within [[MATLAB]]. We also support autoconversion of ImageJ Datasets out of the box, which can be read in by scripts using "@matrix" parameters:  
Line 47:  Line 100:  
</source>  </source>  
−  This script will take the active Dataset, set it as an array variable named "data" in [[MATLAB]], and set the matrixSum output value to the sum of the first three dimensions of the dataset.  +  This script will take the active Dataset, set it as an array variable named "data" in [[MATLAB]], and set the matrixSum output value to the sum of the first three dimensions of the dataset. 
+  
Line 83:  Line 137:  
Instead, you can [[#Running ImageJ within MATLABlaunch ImageJ inside MATLAB]] and have it take care of managing the class loading for you. [[MATLAB]] then supports the use of import statements to [http://www.mathworks.com/help/matlab/matlab_external/bringingjavaclassesandmethodsintomatlabworkspace.html#f46341 simplify class names].  Instead, you can [[#Running ImageJ within MATLABlaunch ImageJ inside MATLAB]] and have it take care of managing the class loading for you. [[MATLAB]] then supports the use of import statements to [http://www.mathworks.com/help/matlab/matlab_external/bringingjavaclassesandmethodsintomatlabworkspace.html#f46341 simplify class names].  
−  
== Running ImageJ within MATLAB ==  == Running ImageJ within MATLAB ==  
−  {{Notice   +  {{Notice  MATLAB versions prior to R2017b need to be [http://www.mathworks.com/matlabcentral/answers/130359howdoichangethejavavirtualmachinejvmthatmatlabisusingonwindows tweaked to use Java 8] instead of Java 7.}} 
The ImageJ update site provides an <code>ImageJ.m</code> script that will start up an ImageJ instance inside a running [[MATLAB]] application. Launching the script is the same as for [[Miji]]:  The ImageJ update site provides an <code>ImageJ.m</code> script that will start up an ImageJ instance inside a running [[MATLAB]] application. Launching the script is the same as for [[Miji]]:  
<source lang="matlab">  <source lang="matlab">  
−  addpath  +  addpath '/Applications/Fiji.app/scripts' % Update for your ImageJ installation as appropriate 
ImageJ;  ImageJ;  
</source>  </source>  
+  
+  Now, you should see a new ImageJ instance shows up as a window. [[File:ImageJ_launched_from_MATLAB.png400px]]  
+  
+  In your MATLAB base workspace, you'll find a variable <code>IJM</code>, which is a <code>net.imagej.matlab.ImageJMATLABCommands</code> Java object. <code>IJM</code> offers a few useful methods as below:  
+  
+  * <code>IJM.getDataset()</code> creates a MATLAB matrix from the active ImageJ image using its window title as the variable name (incompatible characters like <code>.</code> will be escaped with <code>_</code>). The MATLAB matrix is in <code>double</code> type. Although the X and Y axes are swapped, the third to fifth dimensions of the data are preserved.  
+  
+  * <code>IJM.getDatasetAs(name)</code> creates a MATLAB matrix from the active ImageJ image, and assigns it to the specified variable name <code>name</code>.  
+  
+  * <code>IJM.show(name)</code> takes the MATLAB matrix with the specified name <code>name</code> and displays it as an image in ImageJ. The image is always in 32bit type. The X and Y axes are swapped. The third to fifth dimensions of the original MATLAB matrix are all piled in the fifth dimension and interpreted as time frames.  
+  
+  In the MATLAB command window, you'll see something like this:  
+  
+  <source>  
+    
+  ImageJMATLAB 0.7.2: MATLAB to ImageJ Interface  
+    
+  JVM> Version: 1.8.0_144  
+  JVM> Total amount of memory: 370176 Kb  
+  JVM> Amount of free memory: 122756 Kb  
+  
+   Welcome to ImageJMATLAB   
+  ImageJMATLAB consists of an extensible set of commands for passing information between ImageJ and MATLAB.  
+  See the individual sections below for a list of available commands.  
+  
+  For more information and examples see:  
+  http://imagej.net/MATLABScripting  
+  
+   MATLAB Command Plugins   
+  
+   ImageJ MATLAB commands   
+  
+  Usage: IJM.[command]  
+  help  prints a brief description of available commands  
+  getDataset  creates a MATLAB matrix from the active ImageJ image  
+  getDatasetAs(name)  creates a MATLAB matrix from the active ImageJ image, and assigns it to the specified variable name  
+  show(name)  takes the MATLAB matrix with the specified name and displays it as an image  
+  
+  
+  
+    
+  Status> ImageJ is running.  
+    
+  </source>  
+  
The startup process automatically injects the ImageJ classpath into the [[MATLAB]] classpath, merging the two. At this point, you'll have a working ImageJ and can now [[#Creating MATLAB scripts inside ImageJrun MATLAB scripts as normal]] with access to the full unified classpath.  The startup process automatically injects the ImageJ classpath into the [[MATLAB]] classpath, merging the two. At this point, you'll have a working ImageJ and can now [[#Creating MATLAB scripts inside ImageJrun MATLAB scripts as normal]] with access to the full unified classpath.  
−  Additionally, the {{GitHuborg=scijavarepo=scriptingmatlablabel=ScriptingMATLAB}} library also includes an extensible [[MATLAB]] command framework. This allows for the creation of utility classes that will be automatically populated into [[MATLAB]] variables for easy access. For example, you could use ImageJ to open a dataset and perform thresholding (or any other processing steps), then in [[MATLAB]] use the <code>IJM.  +  === Useful Commands === 
+  
+  ==== Show a MATLAB array <code>I</code> as an image in ImageJ ====  
+  * using <code>IJM</code>  
+  <source lang="matlab">  
+  IJM.show('I');  
+  </source>  
+  * using ImageJ1 <code>ImagePlus</code> ([https://github.com/kouichicnakamura/copytoImagePlus see])  
+  <source lang="matlab">  
+  imp = copytoImagePlus(I);  
+  imp.show();  
+  </source>  
+  * using ImgLib2 <code>ArrayImg</code>  
+  <source lang="matlab">  
+  img = copytoImg(I);  
+  net.imglib2.img.display.imagej.ImageJFunctions.show(img);  
+  </source>  
+  ==== Retrieve an image data in ImageJ as a MATLAB array <code>I</code> ====  
+  
+  * using <code>IJM</code> with the current window title as the variable name  
+  <source lang="matlab">  
+  IJM.getDataset();  
+  </source>  
+  * using <code>IJM</code> with the variable name <code>I</code>  
+  <source lang="matlab">  
+  IJM.getDatasetAs('I');  
+  </source>  
+  
+  * from ImgLib2 <code>Img</code>  
+  <source lang="matlab">  
+  I = copytoMATLAB(img);  
+  </source>  
+  
+  ==== Quit the ImageJ instance from MATLAB command line ====  
+  <source lang="matlab">  
+  ij.IJ.run("Quit","");  
+  </source>  
+  
+  === Known Issues===  
+  
+  ==== Common Issues ====  
+  * Although the first and second dimensions, respectively, of MATLAB array are treated as rows (Y)and columns (X) in MATLAB, the first and second dimensions of an array are treated as X and Y, respectively, in ImageJ. The axes are transposed by <code>IJM.show(name)</code>, <code>IJM.getDataset()</code>, <code>IJM.getDatasetAs(name)</code>. (See discussion [http://forum.imagej.net/t/ijmshowinimagejmatlabdoesnotsupportmultidimentionalormultichannelimages/10156 here for details])  
+  * At the moment, there isn't an explicit way to quit the ImageJ instance from MATLAB (If you find a way, please report to [http://forum.imagej.net/ ImageJ forum]).  
+  * <code>IJM</code> object is always added to the MATLAB base workspace. In order to access <code>IJM</code> methods from within a MATLAB function, you'll need to use <code>assignin</code>, <code>evalin</code>, and/or <code>global</code> (see [http://forum.imagej.net/t/howcaniuseimagejfromwithinamatlabfunction/10297 here for details])  
+  * When launching ImageJ, you'll see the error message below:  
+  <source>  
+  log4j:WARN No appenders could be found for logger (loci.formats.ClassList).  
+  log4j:WARN Please initialize the log4j system properly.  
+  </source>  
+  
+  ==== Issues specific to <code>IJM.show(name)</code> ====  
+  * A multi (>2) dimensional image (with multichannels, slices or time frames) is treated as single channel images with multiple time frames irrespective of the dimensions. For example, if a MATLAB array <code>A</code> has the size <code>[i,j,k]</code>, where <code>i</code>, <code>j</code>, and <code>k</code> corresponds to the size in rows, columns and channels, then <code>IJM.show(name)</code> will show it as <code>[j,i,1,1,k]</code>, where X and Y axes are flipped over (see below) and channels are interpreted as frames. On the other hand, <code>IJM.getDataset()</code> and <code>IJM.getDatasetAs(name)</code> can maintain the dimensions of the image data (except the flipping over of X and Y axes).  
+  ** From GUI, you can fix this by ''Image > Hyperstacks > Reorder Hyperstack...''  
+  ** For a scriptingbased solution, you can run <code>ij.IJ.run("Reorder Hyperstack ...", "channels=[Frames (t)] slices=[Slices (z)] frames=[Channels (c)]");</code> in MATLAB for the example above to have dimensions <code>[i,j,k,1,1]</code> in XYCZT format. See [http://forum.imagej.net/t/imagejmatlabijmshowdoesnotreliablypassnumericdatatoimagej/10724 here for details].  
+  ** When you have multiple channels, slices and frames, they're all damped into the fifth dimension, so it takes extra effort to correct the data format.  
+  * Also, data is always handled as 32bit (int32) per channel. '''Conversion to 8 bit or 16 bit images require an extra care''' to avoid unwanted scaling of numeric values (see [http://forum.imagej.net/t/imagejmatlabijmshowdoesnotreliablypassnumericdatatoimagej/10724 here for details])  
+  
+  ==== Solution to <code>IJM.show(name)</code> issues ====  
+  * Until someone can fix <code>IJM.show(name)</code> properly, consider using <code>[https://github.com/kouichicnakamura/copytoImagePlus copytoImagePlus]</code> MATLAB function instead. It solves all the issues of <code>IJM.show(name)</code> descrived above and create an ImagePlus hyperstack. See the example of a 5D Hyperstack below.  
+  * <code>copytoImg</code> and <code>copytoImgPlus</code> MATLAB functions are also available from <code>Fiji.app/scripts</code> folder to allow you convert a MATLAB array to ImageJ2 (ImgLib2) Img and ImgPlus objects, respectively. See also [[Creating_Imglib2_images_in_MATLAB  this page]].  
+  
+  === Examples ===  
+  ==== Accessing ImageJ Java API from MATLAB ====  
+  
+  The following Java commands work in MATLAB command window to open a sample image in ImageJ.  
+  
+  <source lang="matlab">  
+  imp = ij.IJ.openImage("http://imagej.nih.gov/ij/images/boats.gif");  
+  imp.show()  
+  </source>  
+  [[File:boats_screenshot.png400px]]  
+  
+  ==== Opening a MATLAB array as an image in ImageJ ====  
+  
+  The following will open a MATLAB array data as an image in ImageJ. Note that the X and Y of the image is transposed in ImageJ in the second image (see Limitations of <code>IJM.show()</code> above).  
+  <source lang="matlab">  
+  corn_gray = imread('corn.tif',3);  
+  imshow(corn_gray); % show in MATLAB  
+  IJM.show('corn_gray'); % show in ImageJ  
+  corn_gray_tp = corn_gray'; % transpose array  
+  IJM.show('corn_gray_tp'); % show in ImageJ properly  
+  
+  </source>  
+  [[File:corn_gray_matlab.png250px]]  
+  [[File:corn_gray.png250px]]  
+  [[File:corn_gray_transposed.png250px]]  
+  
+  A more general solution to this issue of XY transposition can be achieved by <code>permute</code> function of MATLAB. But please beware of [https://stackoverflow.com/questions/36065182/whydoesmatlabspermutenotneedextramemory memory demands by <code>permute</code>].  
+  <source lang="matlab">  
+  % I is a MATLAB array with 2 or more dimensions  
+  order = [{2, 1}, num2cell(3:ndims(I))];  
+  I2 = permute(I,[order{:}]);  
+  IJM.show(I2) % this will show an image in proper X and Y  
+  </source>  
+  
+  If you wish to show an image with ImageJ from within a MATLAB function, you can achieve that by using <code>[https://github.com/kouichicnakamura/copytoImagePlus copytoImagePlus ]</code> as below:  
+  
+  <source lang="matlab">  
+  function demofun()  
+  
+  corn_gray = imread('corn.tif',3);  
+  
+  imp = copytoImagePlus(corn_gray);  
+  imp.show();  
+  
+  end  
+  </source>  
+  
+  ==== Retrieving a MATLAB array data from an image in ImageJ ====  
+  
+  Additionally, the {{GitHuborg=scijavarepo=scriptingmatlablabel=ScriptingMATLAB}} library also includes an extensible [[MATLAB]] command framework. This allows for the creation of utility classes that will be automatically populated into [[MATLAB]] variables for easy access. For example, you could use ImageJ to open a dataset and perform thresholding (or any other processing steps), then in [[MATLAB]] use the <code>IJM.getDatasetAs(name)</code> command to set the active dataset as a [[MATLAB]] matrix variable with a specified name, for further analysis.  
For example, instead of using a script as [[#Creating MATLAB scripts inside ImageJdescribed above]], we could achieve the same result by executing the following commands in the MATLAB prompt:  For example, instead of using a script as [[#Creating MATLAB scripts inside ImageJdescribed above]], we could achieve the same result by executing the following commands in the MATLAB prompt:  
Line 113:  Line 321:  
IJM.show('mask') % display the mask array in ImageJ  IJM.show('mask') % display the mask array in ImageJ  
</source>  </source>  
+  
+  If you wish to assign the numeric data of an ImageJ image into the Workspace of a MATLAB function, rather than the base Workspace, you can achieve that by making the variable global as below:  
+  
+  <source lang="matlab">  
+  
+  function demofun()  
+  
+  evalin('base','global data');  
+  evalin('base','IJM.getDatasetAs(''data'')');  
+  global data % data is now available in the Workspace of this function  
+  disp(size(data))  
+  
+  clear global data % leaving a global varibale can cause unexpected problems  
+  
+  end  
+  
+  </source>  
+  
+  
+  ==== Opening a 2D5D MATLAB array as a Hyperstack in ImageJ ====  
+  
+  If you use <code>[https://github.com/kouichicnakamura/copytoImagePlus copytoImagePlus]</code>, you can open a 2D5D MATLAB array as a Hyperstack with the right image type (8bit, 16bit, and 32bit for uint8, uint16, and other types of MATLAB array, respectively) in ImageJ with a simple syntax. Also, you can use <code>copytoImagePlus</code> within a MATLAB function independently of the <code>IJM</code> variable in base workspace.  
+  
+  <source lang="matlab">  
+  imp = ij.IJ.openImage("http://imagej.nih.gov/ij/images/SpindlyGFP.zip");  
+  imp.show();  
+  IJM.getDatasetAs('I'); % I is 5D double with the size of 171x196x2x5x51 in MATLAB  
+  I16 = uint16(I);  
+  
+  IJM.show('I16') % ImagePlus, 32bit, 171x196x1x1x510  
+  
+  imp4 = copytoImagePlus(I16,'XYCZT') % ImagePlus, 16bit, 171x196x2x5x51  
+  imp4.show();  
+  </source>  
+  
+  <code>IJM.show(name)</code> cannot reproduce the dimensions and the data type. Channels (C), slices (Z), and frames (T) are all treated as frames. Image type is 32 bit.  
+  [[File:IJMshow_Image006.png 150px]]  
+  
+  <code>copytoImagePlus</code> can keep the dimensions and the data type.  
+  [[File:Image003.png 150px]]  
= Source =  = Source =  
−  *  +  * {{GitHuborg=scijavarepo=scriptingmatlablabel=ScriptingMATLAB}} provides the script engine plugins. 
−  *  +  * {{GitHuborg=imagejrepo=imagejmatlablabel=ImageJMATLAB}} defines the ImageJspecific translators. 
= Video presentation =  = Video presentation =  
Line 129:  Line 377:  
[[Category:MATLAB]]  [[Category:MATLAB]]  
[[Category:Citable]]  [[Category:Citable]]  
+  
+  = See also =  
+  
+  * [[Creating Imglib2 images in MATLAB]]  
+  * [[Using TrackMate from MATLAB]]  
+  * [[Analyzing TrackMate results with MATLAB]]  
+  * [[Comparison of Matlab functions and Ops]]  
+  * [[Miji]] (legacy) 
Latest revision as of 18:27, 13 December 2018
ImageJMATLAB is an extension which:
 Translates data between ImageJ images and MATLAB matrices.
 Enables execution of MATLAB scripts from inside ImageJ's Script Editor.
 Lets you launch ImageJ and interact with it from inside MATLAB.
 Allows developers to write additional plugins which extend these capabilities in new directions.
Contents
MATLAB tutorial for ImageJ
Prerequisites
 Add the ImageJMATLAB update site. See Following an update site for more detail.
 You will need to install your own licensed copy of MATLAB. All that is provided with ImageJ are adapters for evaluating scripts written in ImageJ to MATLAB, and converters between ImageJ and MATLAB data structures.
 Prior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: Windows, macOS or Linux. If you run MATLAB R2017b and later, you don't need to do this, as it ships and uses Java 8 already.
 The command
version java
will return the current Java version used by MATLAB
 The command
 If you're new to MATLAB, first check out Mathworks' getting started guide.
 If you're familiar with the MATLAB language but haven't written .m scripts before, look through the script examples.
Memory issue
 In order to handle large images, the default Java Heap Memory size assigned won't be enough and you may get the error
java.lang.OutOfMemoryError: Java heap space
.  From MATLAB R2010a onward, you can increase the Jave Heap Memory size from Preferences > General > Java Heap Memory.
 However, the maximum value allowed in the Preferences can still be too small for your purpose. In that case, you can directly edit
matlab.prf
file in the folder specified by theprefdir<\code> MATLAB function (eg. <code>C:\Users\xxxxxxx\AppData\Roaming\MathWorks\MATLAB\R2018b
). Find the parameterJavaMemHeapMax
in the file and increase the number that follows the capital I (in MB) to increase the maximum Java heap memory size. The change will be reflected by the Preferences as above.
JavaMemHeapMax=I25000
 Alternatively, this can also be done by creating a
java.opts
file in the startup directory and overriding the default memory settings (see this documentation for more information). For instance,Xmx512m
in yourjava.opts
file may be a good start point.  For the common "Out of Memory" error, see the Troubleshooting page.
Creating MATLAB scripts inside ImageJ
Using the Script Editor you will be able to select MATLAB from the language menu. You can also install and run .m scripts via the standard script plugin infrastructure.
Actually running a MATLAB script from ImageJ is effectively like calling eval
on the script's contents. The script will be evaluated as such in a remote MATLAB instance (which will be launched automatically, if needed). Note that only scripts, not functions, can be evaluated in this way. See the MATLAB documentation for an explanation of these concepts.
NB: Because of the similarity to
eval
, when you need to assign character vector or string with special characters, they need to be escaped. A typical example is the handling of file/folder paths in Windows. Thus,s = 'C:\Users\xxxxx\Desktop'will end up in an error in MATLAB:
Warning: Escaped character '\U' is not valid. See 'doc sprintf' for supported special characters. scijava_script523255 = 's = 'C:' Error: Character vector is not terminated properly.You need to escape backslashes as below:
s = 'C:\\Users\\xxxxx\\Desktop's = 'C:\Users\xxxxx\Desktop'For more details, see documetation for
sprintf
and learn about Special Characters.
Options for controlling the startup of MATLAB, or killing existing MATLAB processes (e.g. if hidden) can be accessed via:
Edit ▶ Options ▶ MATLAB...
NB: because the script is being passed from ImageJ to a remote MATLAB, MATLAB will not have access to ImageJ's classpath. Objects can be passed as variables to MATLAB (e.g. by using @ annotation) but only if they are valid MATLAB classes or specially handled classes.
Scripts requiring ImageJ classes without autoconversion support should launch ImageJ from within MATLAB.
eg. a script using
ij.IJ
commands needs to be run from within MATLAB (or you'll need to add Java class paths to MATLAB search path before callingij.IJ
commands).
For example, by default all MatlabNumericArrays will be converted to matrices within MATLAB. We also support autoconversion of ImageJ Datasets out of the box, which can be read in by scripts using "@matrix" parameters:
% @matrix data % @OUTPUT net.imagej.Dataset rval % @OUTPUT net.imagej.Dataset mask % Performs dilation with a 3x3 square, % operating on the active dataset % Outputs the dilated mask and the original image % with the mask applied. rval = uint8(data); % convert to uint8 rval = mat2gray(rval); % normalize data mask = im2bw(rval,0.5); % make logical mask se = strel('square',3); % create structure to use in dilation mask = imdilate(mask,se); % perform dilation on the mask rval(~mask) = 0; % subtract mask from original dataset
This script will take the active Dataset, set it as an array variable named "data" in MATLAB, and set the matrixSum output value to the sum of the first three dimensions of the dataset.
Global state
MATLAB retains state (e.g. declared variables) as commands are executed, and ImageJ makes no special effort to clean up after a script. So whether running internally or communicating externally with MATLAB, state will be available to and persist after script execution. Thus one can, for example, write scripts in ImageJ referencing variables declared in MATLAB, without actually initializing them in the script.
Passing Objects
The caveat to global state is that, when running ImageJ externally, ImageJ and MATLAB run in separate JVMs. As a result, most objects can not be passed between the two. This makes Datasets (and arrays) the currency that is passed between these applications.
Return values
Most of the ImageJ scripting languages have implicit return values. As mentioned above, ImageJ will only execute true scripts, which do not have return values (in the MATLAB functional sense). There is a similar concept in the ans
variable, which automatically gets the return value of executed statements if they are not explicitly assigned. However, due to the global nature of the ImageJMATLAB script language, it is not necessarily clear if ans
was set by the script or a preexisting command. Thus the decision was made that ImageJMATLAB scripts will never implicitly return a value. Instead, the @OUTPUT annotation should always be used  even for ans
, as shown here:
% @OUTPUT double[] ans % This trivial script demonstrates the use of % the "ans" variable in the SciJavaMATLAB % script language. % Unassigned statements in MATLAB are automatically % assigned to "ans". However, these scripts will not % return "ans" unless it is explicitly requested as % an output parameter. 0
Importing classes
To reference Java classes from MATLAB you will need to import them.
When running ImageJ externally, MATLAB will not have ImageJ classes in its classpath  so they can not simply be imported. Although MATLAB does support editing its classpath this is NOT recommended, as the classes loaded by MATLAB will not be the same as those loaded in ImageJ.
Instead, you can launch ImageJ inside MATLAB and have it take care of managing the class loading for you. MATLAB then supports the use of import statements to simplify class names.
Running ImageJ within MATLAB
The ImageJ update site provides an ImageJ.m
script that will start up an ImageJ instance inside a running MATLAB application. Launching the script is the same as for Miji:
addpath '/Applications/Fiji.app/scripts' % Update for your ImageJ installation as appropriate ImageJ;
Now, you should see a new ImageJ instance shows up as a window.
In your MATLAB base workspace, you'll find a variable IJM
, which is a net.imagej.matlab.ImageJMATLABCommands
Java object. IJM
offers a few useful methods as below:

IJM.getDataset()
creates a MATLAB matrix from the active ImageJ image using its window title as the variable name (incompatible characters like.
will be escaped with_
). The MATLAB matrix is indouble
type. Although the X and Y axes are swapped, the third to fifth dimensions of the data are preserved.

IJM.getDatasetAs(name)
creates a MATLAB matrix from the active ImageJ image, and assigns it to the specified variable namename
.

IJM.show(name)
takes the MATLAB matrix with the specified namename
and displays it as an image in ImageJ. The image is always in 32bit type. The X and Y axes are swapped. The third to fifth dimensions of the original MATLAB matrix are all piled in the fifth dimension and interpreted as time frames.
In the MATLAB command window, you'll see something like this:
 ImageJMATLAB 0.7.2: MATLAB to ImageJ Interface  JVM> Version: 1.8.0_144 JVM> Total amount of memory: 370176 Kb JVM> Amount of free memory: 122756 Kb  Welcome to ImageJMATLAB  ImageJMATLAB consists of an extensible set of commands for passing information between ImageJ and MATLAB. See the individual sections below for a list of available commands. For more information and examples see: http://imagej.net/MATLABScripting  MATLAB Command Plugins   ImageJ MATLAB commands  Usage: IJM.[command] help  prints a brief description of available commands getDataset  creates a MATLAB matrix from the active ImageJ image getDatasetAs(name)  creates a MATLAB matrix from the active ImageJ image, and assigns it to the specified variable name show(name)  takes the MATLAB matrix with the specified name and displays it as an image  Status> ImageJ is running. 
The startup process automatically injects the ImageJ classpath into the MATLAB classpath, merging the two. At this point, you'll have a working ImageJ and can now run MATLAB scripts as normal with access to the full unified classpath.
Useful Commands
Show a MATLAB array I
as an image in ImageJ
 using
IJM
IJM.show('I');
 using ImageJ1
ImagePlus
(see)
imp = copytoImagePlus(I); imp.show();
 using ImgLib2
ArrayImg
img = copytoImg(I); net.imglib2.img.display.imagej.ImageJFunctions.show(img);
Retrieve an image data in ImageJ as a MATLAB array I
 using
IJM
with the current window title as the variable name
IJM.getDataset();
 using
IJM
with the variable nameI
IJM.getDatasetAs('I');
 from ImgLib2
Img
I = copytoMATLAB(img);
Quit the ImageJ instance from MATLAB command line
ij.IJ.run("Quit","");
Known Issues
Common Issues
 Although the first and second dimensions, respectively, of MATLAB array are treated as rows (Y)and columns (X) in MATLAB, the first and second dimensions of an array are treated as X and Y, respectively, in ImageJ. The axes are transposed by
IJM.show(name)
,IJM.getDataset()
,IJM.getDatasetAs(name)
. (See discussion here for details)  At the moment, there isn't an explicit way to quit the ImageJ instance from MATLAB (If you find a way, please report to ImageJ forum).

IJM
object is always added to the MATLAB base workspace. In order to accessIJM
methods from within a MATLAB function, you'll need to useassignin
,evalin
, and/orglobal
(see here for details)  When launching ImageJ, you'll see the error message below:
log4j:WARN No appenders could be found for logger (loci.formats.ClassList). log4j:WARN Please initialize the log4j system properly.
Issues specific to IJM.show(name)
 A multi (>2) dimensional image (with multichannels, slices or time frames) is treated as single channel images with multiple time frames irrespective of the dimensions. For example, if a MATLAB array
A
has the size[i,j,k]
, wherei
,j
, andk
corresponds to the size in rows, columns and channels, thenIJM.show(name)
will show it as[j,i,1,1,k]
, where X and Y axes are flipped over (see below) and channels are interpreted as frames. On the other hand,IJM.getDataset()
andIJM.getDatasetAs(name)
can maintain the dimensions of the image data (except the flipping over of X and Y axes). From GUI, you can fix this by Image > Hyperstacks > Reorder Hyperstack...
 For a scriptingbased solution, you can run
ij.IJ.run("Reorder Hyperstack ...", "channels=[Frames (t)] slices=[Slices (z)] frames=[Channels (c)]");
in MATLAB for the example above to have dimensions[i,j,k,1,1]
in XYCZT format. See here for details.  When you have multiple channels, slices and frames, they're all damped into the fifth dimension, so it takes extra effort to correct the data format.
 Also, data is always handled as 32bit (int32) per channel. Conversion to 8 bit or 16 bit images require an extra care to avoid unwanted scaling of numeric values (see here for details)
Solution to IJM.show(name)
issues
 Until someone can fix
IJM.show(name)
properly, consider usingcopytoImagePlus
MATLAB function instead. It solves all the issues ofIJM.show(name)
descrived above and create an ImagePlus hyperstack. See the example of a 5D Hyperstack below. 
copytoImg
andcopytoImgPlus
MATLAB functions are also available fromFiji.app/scripts
folder to allow you convert a MATLAB array to ImageJ2 (ImgLib2) Img and ImgPlus objects, respectively. See also this page.
Examples
Accessing ImageJ Java API from MATLAB
The following Java commands work in MATLAB command window to open a sample image in ImageJ.
imp = ij.IJ.openImage("http://imagej.nih.gov/ij/images/boats.gif"); imp.show()
Opening a MATLAB array as an image in ImageJ
The following will open a MATLAB array data as an image in ImageJ. Note that the X and Y of the image is transposed in ImageJ in the second image (see Limitations of IJM.show()
above).
corn_gray = imread('corn.tif',3); imshow(corn_gray); % show in MATLAB IJM.show('corn_gray'); % show in ImageJ corn_gray_tp = corn_gray'; % transpose array IJM.show('corn_gray_tp'); % show in ImageJ properly
A more general solution to this issue of XY transposition can be achieved by permute
function of MATLAB. But please beware of memory demands by permute
.
% I is a MATLAB array with 2 or more dimensions order = [{2, 1}, num2cell(3:ndims(I))]; I2 = permute(I,[order{:}]); IJM.show(I2) % this will show an image in proper X and Y
If you wish to show an image with ImageJ from within a MATLAB function, you can achieve that by using copytoImagePlus
as below:
function demofun() corn_gray = imread('corn.tif',3); imp = copytoImagePlus(corn_gray); imp.show(); end
Retrieving a MATLAB array data from an image in ImageJ
Additionally, the ScriptingMATLAB library also includes an extensible MATLAB command framework. This allows for the creation of utility classes that will be automatically populated into MATLAB variables for easy access. For example, you could use ImageJ to open a dataset and perform thresholding (or any other processing steps), then in MATLAB use the IJM.getDatasetAs(name)
command to set the active dataset as a MATLAB matrix variable with a specified name, for further analysis.
For example, instead of using a script as described above, we could achieve the same result by executing the following commands in the MATLAB prompt:
IJM.getDatasetAs('data') % import the image as a MATLAB matrix rval = uint8(data); % convert to uint8 rval = mat2gray(rval); % normalize data mask = im2bw(rval,0.5); % make logical mask se = strel('square',3); % create structure to use in dilation mask = imdilate(mask,se); % perform dilation on the mask rval(~mask) = 0; % subtract mask from original dataset IJM.show('rval') % display the rval array in ImageJ IJM.show('mask') % display the mask array in ImageJ
If you wish to assign the numeric data of an ImageJ image into the Workspace of a MATLAB function, rather than the base Workspace, you can achieve that by making the variable global as below:
function demofun() evalin('base','global data'); evalin('base','IJM.getDatasetAs(''data'')'); global data % data is now available in the Workspace of this function disp(size(data)) clear global data % leaving a global varibale can cause unexpected problems end
Opening a 2D5D MATLAB array as a Hyperstack in ImageJ
If you use copytoImagePlus
, you can open a 2D5D MATLAB array as a Hyperstack with the right image type (8bit, 16bit, and 32bit for uint8, uint16, and other types of MATLAB array, respectively) in ImageJ with a simple syntax. Also, you can use copytoImagePlus
within a MATLAB function independently of the IJM
variable in base workspace.
imp = ij.IJ.openImage("http://imagej.nih.gov/ij/images/SpindlyGFP.zip"); imp.show(); IJM.getDatasetAs('I'); % I is 5D double with the size of 171x196x2x5x51 in MATLAB I16 = uint16(I); IJM.show('I16') % ImagePlus, 32bit, 171x196x1x1x510 imp4 = copytoImagePlus(I16,'XYCZT') % ImagePlus, 16bit, 171x196x2x5x51 imp4.show();
IJM.show(name)
cannot reproduce the dimensions and the data type. Channels (C), slices (Z), and frames (T) are all treated as frames. Image type is 32 bit.
copytoImagePlus
can keep the dimensions and the data type.
Source
 ScriptingMATLAB provides the script engine plugins.
 ImageJMATLAB defines the ImageJspecific translators.
Video presentation
Publication
 Hiner, M. C.; Rueden, C. R. & Eliceiri, K. W. (2016), "ImageJMATLAB: a bidirectional framework for scientific image analysis interoperability", Bioinformatics, PMID 27797782, doi:10.1093/bioinformatics/btw681, <http://bioinformatics.oxfordjournals.org/content/early/2016/10/25/bioinformatics.btw681>.