我知道编辑这个网站吗?
the ImageJ2 platform本页介绍与 the ImageJ2 platform 相关的内容。点击徽标查看详情。

MATLAB 脚本

Prior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ2 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.

Scripts requiring direct access to classes from ImageJ2, or from the original ImageJ (without auto-conversion support) should launch ImageJ2 from within MATLAB.

E.g. 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 calling ij.IJ commands).

ImageJ-MATLAB 是一个扩展:

  • 在 ImageJ2 图像和 MATLAB 矩阵之间转换数据。
  • 允许从 ImageJ2 的Script Editor内部执行 MATLAB 脚本。
  • 允许您启动 ImageJ2 并从 MATLAB 内部交互。
  • 允许开发人员在新的方向上编写额外的pluginsextend这些功能。

ImageJ2 的 MATLAB 教程

先决条件

-添加ImageJ-MATLAB更新站点。有关详细信息,请参阅Following an update site。 1. You go to PreferencesGeneralJava Heap Memory 2. Once checking status is done, click Close 3. Tick “ImageJ-MATLAB” and then Manage update sites fig:Manage_update_sites_ImageJ_MATLAB.png 4. And then click Apply changes on ImageJ Updater 5. This will literally update and replace the existing, non-functional ImageJ.m file in the scripts folder with the genuine one.

  • 您需要安装您自己的 MATLAB 的许可副本。 ImageJ-MATLAB 提供的只是用于评估以 ImageJ2 编写的脚本到 MATLAB 的长度,以及 ImageJ2 和 MATLAB 之间的数据结构的转换器。
  • 在 MATLAB R2017b 中,MATLAB 附带 Java 7,但 ImageJ2 需要 Java 8。您需要按照适合您平台的说明更改 MATLAB 安装以使用 Java 8:WindowsmacOSLinux。如果您运行 MATLAB R2017b 及之前的更高版本,则需要执行此操作,因为它已经附带使用并 Java 8。
    • The command version -java will return the current Java version used by MATLAB
  • 如果您不熟悉MATLAB,请首先查看Mathworks的getting started guide
  • 如果您熟悉MATLAB语言但之前没有编写过.m脚本,请查看script examples

内存问题

  • 为了处理大图像,分配的默认Java堆内存大小不足,您可能会收到错误java.lang.OutOfMemoryError: Java heap space
  • 从 MATLAB R2010a 开始,您可以从 HelpUpdate… 增加 Jave 堆内存大小。 Matlab java heap memory -但是,首选项中允许的顶层对于您的目的来说仍然太小。在这种情况下,您可以直接编辑 prefdir<\\code> MATLAB 函数指定的文件夹中的`matlab.prf` (例如`C:\Users\xxxxxxx\AppData\Roaming\MathWorks\MATLAB\R2018b`)。找到文件中的参数`JavaMemHeapMax`并增加大写I后面的数字(以MB为单位)以增加最大Java堆内存大小。该更改将通过上述首选项文件来进行。

    JavaMemHeapMax=I25000 -或者,也可以通过在 the startup directory 中创建 java.opts 文件并覆盖默认内存设置来完成此操作(有关详细信息,请参阅this documentation)。例如,您的 java.opts 文件中的 -Xmx512m 可能是一个很好的起点。

  • 常见的“内存不足”错误,请参见 the Troubleshooting page

在 ImageJ2 中创建 MATLAB 脚本

使用Script Editor,您可以从language menu中选择MATLAB。您还可以通过 standard script plugin infrastructure安装并运行.m脚本。

其中,从 ImageJ2 运行 MATLAB 脚本就像只能脚本内容上 eval 一样。该脚本将在远程 MATLAB 实例中进行评估(需要注意,该实例将自动启动)。请以,这种方式评估脚本,而不能评估函数。有关这些概念的解释,请参阅the MATLAB documentation

注意:由于与eval相似,当您需要指定带有特殊字符的字符修饰或字符串时,需要对它们进行转义。一个典型的例子是 Windows 中文件/文件夹路径的处理。因此,

s = ‘C:\Users\xxxxx\Desktop’

将在 MATLAB 中出现错误:

警告:转义字符“\U”无效。请参阅“doc sprintf” 支持特殊字符。 scijava_script523255 = ‘s = ‘C:’ 错误:字符未正确终止。

您需要转义反斜杠,如下图:

s = ‘C:\Users\xxxxx\Desktop’

s = ‘C:\Users\xxxxx\Desktop’

有关更多详细信息,请参阅 sprintf 的文档并了解特殊字符。

用于MATLAB启动或新增现有MATLAB进程(例如控制)的选项可以通过以下方式访问:ImageHyperstacksRe-order Hyperstack…

Matlab options

Because the script is being passed from ImageJ2 to a remote MATLAB, MATLAB will not have access to ImageJ2’s classpath. Objects can be passed as variables to MATLAB (e.g. by using script parameters) but only if they are valid MATLAB classes or specially handled classes.

If you run MATLAB R2017b and later, you don’t need to do this, as it ships and uses Java 8 already.

例如,默认情况下,所有MatlabNumericArrays都将转换为MATLAB内部的矩阵。我们还支持开箱即用的ImageJ2 Dataset自动转换,可以通过使用matrix参数的脚本读取: #@ 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 该脚本将获取活动的`Dataset`,将其设置为[MATLAB](/scripting/matlab)中名为“data”的存储器变量,将matrixSum输出值设置为数据集前三个维度的总和。

全局状态

MATLAB 在执行命令时保留状态(例如声明的变量),而 ImageJ2 不会在脚本执行后进行特殊清理。因此,无论是内部运行还是与 MATLAB 外部通信,状态都将调用脚本执行后使用并持续存在。因此,例如,可以在 ImageJ2 中编写引用 MATLAB 中声明的变量的脚本,而需要在脚本中实际初始化它们。

传递对象

global state的警告是,在外部运行ImageJ2时,ImageJ2和MATLAB分别在JVMs中运行。结果,大多数对象无法在两者之间传递。这使得Datasets(和接口)成为这些应用程序之间传递的货币。

###返回值

ImageJ2 支持的大多数 scripting languages 都有隐式返回值。如上所述,ImageJ2 将仅执行真正的脚本,这些脚本没有返回值(在 MATLAB 函数中)。ans标记有一个类似的概念,如果没有显式标记,会自动获取执行语句的返回值。但是,由于 ImageJ-MATLAB 脚本语言的global nature,不一定明确ans是由脚本还是预先存在的命令设置的。因此决定 ImageJ-MATLAB 脚本永远不会隐式返回值。相反,应始终使用 the #@output annotation - 即使ans,如下所示: #@output double[] ans

% This trivial script demonstrates the use of
% the "ans" variable in the SciJava-MATLAB
% 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 ### 导入类

要从 MATLAB 中引用 Java 类,您需要导入它们。

与原始 ImageJ 不同,ImageJ2(因此也包括 Fiji)不会自动导入任何类。因此,为 ImageJ 编写的脚本如果不添加正确的导入,将无法在 ImageJ2 中运行。原因是自动导入功能并不安全。如果两个同名类位于两个不同的包中怎么办?或者引入了一个新类,使原本唯一的名称产生歧义怎么办?突然间,所有引用原始类的脚本都不再工作。简而言之:自动导入存在危险的精确性问题。


在外部运行 ImageJ2 时,MATLAB的类路径中不会有 ImageJ2 类 - 因此不能简单地导入它们。虽然MATLAB确实支持editing its classpath,但不建议这样,因为MATLAB加载的类与ImageJ2中加载的类不同。

相反,您可以launch ImageJ2 inside MATLAB并负责管理类加载。MATLAB然后支持使用simplify class names的导入语句。

在 MATLAB 中运行 ImageJ2

MATLAB versions prior to R2017b need to be tweaked to use Java 8 instead of Java 7.

ImageJ-MATLAB 更新站点提供了一个 ImageJ.m 脚本,该脚本将在运行时的 MATLAB应用程序中启动 ImageJ2 实例。启动脚本与 Miji 相同: addpath ‘/Applications/Fiji.app/scripts’ % Update for your ImageJ2 (or Fiji) installation as appropriate ImageJ; 现在,您应该看到一个新的 ImageJ2 实例显示为一个窗口。 ImageJ2 window launched from MATLAB

在 MATLAB 基础工作区中,您将找到一个变量 IJM,它是一个 net.imagej.matlab.ImageJMATLABCommands Java 对象。 IJM提供了一些有用的方法,如下:

  • 使用IJM.getDataset()其窗口标题作为指标名称,从活动ImageJ2图像创建MATLAB矩阵(像.这样的不兼容字符将使用_进行转义)。MATLAB矩阵为double类型。虽然X轴和Y轴交换了,但数据的第三第五到维仍被保留。

  • IJM.getDatasetAs(name) 从活动 ImageJ2 图像创建一个 MATLAB 矩阵,将其分配给指定的变量名称name

  • IJM.show(name)采用指定名称name的MATLAB矩阵,将其在ImageJ2中显示为图像。图像始终为32位类型。X轴和Y轴交换。原始MATLAB矩阵到第五维的全部占用在第五维中,并解释为时间范围。

在MATLAB命令窗口中,您将看到以下内容: ————————————————————– ImageJ-MATLAB 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 ImageJ-MATLAB --
ImageJ-MATLAB 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:
    https://imagej.net/scripting/matlab

--- 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.
-------------------------------------------------------------- 启动过程会自动将ImageJ2类路径注入到[MATLAB](/scripting/matlab)类路径中,将两者合并。此时,您将拥有一个可以工作的ImageJ2,并且现在[run MATLAB scripts as normal](#creating-matlab-scripts-inside-imagej)访问完整的统一类路径。

有用的命令

在 ImageJ2 中将 MATLAB 备份 I显示图像

  • 使用IJM
      IJM.show('I');
    
  • 使用原始 ImageJ 的 ImagePlus (see)
      imp = copytoImagePlus(I);
      imp.show();
    
  • 使用 ImgLib2 ArrayImg
      img = copytoImg(I);
      net.imglib2.img.display.imagej.ImageJFunctions.show(img);
    

    ####以MATLAB数据库形式检索ImageJ中的图像数据I

  • 使用IJM和当前窗口标题作为变量名
      IJM.getDataset();
    
  • 使用IJM和变量名I
      IJM.getDatasetAs('I');
    
  • 来自ImgLib2Img
      I = copytoMATLAB(img);
    

    从 MATLAB 命令行退出 ImageJ 实例

      ij.IJ.run("Quit","");
    

    已知问题

常见问题

-尽管 MATLAB 存储器的第一维和第二维在 MATLAB 中分别被视为行 (Y) 和列 (X),但在 ImageJ2 中,存储器的第一维和第二维分别被视为 X 和 Y。轴通过 IJM.show(name)IJM.getDataset()IJM.getDatasetAs(name) 转置。 (参见讨论here for details

  • IJM对象始终添加到 MATLAB 基础工作区。要从 MATLAB 函数中访问 IJM 方法,您需要使用 assigninevalin 和/或 global(请参阅here for details
  • 启动ImageJ2时,您将看到以下错误消息:
      log4j:WARN No appenders could be found for logger (loci.formats.ClassList).
      log4j:WARN Please initialize the log4j system properly.
    

    IJM.show(name)的特定问题

  • 多(>2)维图像(具有多通道、切片或时间帧)被视为具有多个时间帧的单通道图像,无论尺寸如何。例如,如果 MATLAB 存储器A的大小为[i,j,k],其中ijk对应于行、列和通道的大小,则IJM.show(name)其显示为[j,i,1,1,k],其中X轴和Y轴跳跃(见下方)并且通道被解释为框架。另外,IJM.getDataset()IJM.getDatasetAs(name)保持可以图像的尺寸(除了X轴和Y轴的跳跃)。
    • From GUI, you can fix this by EditOptionsMATLAB…
    • For a scripting-based solution, you can run ij.IJ.run("Re-order 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.
  • 另外,数据始终按每个通道 32 位 (int32) 进行处理。 转换为位 8 或 16 位图像需要额外小心否则不需要的数值缩放(请参阅here for details

IJM.show(name)问题的解决方案

  • 在有人可以正确修复IJM.show(name)之前,请考虑使用copytoImagePlus MATLAB函数。它解决了上述IJM.show(name)的所有问题,并创建了一个 ImagePlus 超级堆栈。请参阅下面的 5D Hyperstack 示例。
  • copytoImgcopytoImgPlus MATLAB 函数也可以从 Fiji.app/scripts 文件夹中获取,以允许您将 MATLAB 字节转换分别为 ImageJ2 (ImgLib2) ImgImgPlus 对象。其他请参见this page

示例

从 MATLAB 访问 ImageJ Java API

以下Java命令在MATLAB命令窗口中运行,以在ImageJ中打开示例图像。 imp = ij.IJ.openImage(“http://imagej.net/ij/images/boats.gif”); imp.show() Boats image opened in ImageJ2 from MATLAB

在 ImageJ2 中将 MATLAB 备份图像作为打开

下面将在 ImageJ2 中打开 MATLAB 备份数据图像作为。请注意,图像的 X 和 Y 在第二个 ImageJ2 中转设置(请参阅上面第 IJM.show() 的限制)。 corn_gray = imread(‘corn.tif’,3); imshow(corn_gray); % show in MATLAB IJM.show(‘corn_gray’); % show in ImageJ2 corn_gray_tp = corn_gray’; % transpose array IJM.show(‘corn_gray_tp’); % show in ImageJ2 properly

Corn image in MATLAB Corn image in ImageJ2 Corn image transposed in ImageJ2 对于这个 X-Y 转置问题,可以通过 MATLAB 的permute函数实现更通用的解决方案。但请注意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 如果您希望在 MATLAB 函数中使用 ImageJ 显示图像,可以通过使用 copytoImagePlus 来实现,如下所示: function demofun()

corn_gray = imread('corn.tif',3);

imp = copytoImagePlus(corn_gray);
imp.show();

end #### 来自 ImageJ2 中的搜索 MATLAB 备份数据

此外,ImageJ-MATLAB库还包括可扩展的MATLAB命令框架。这允许创建实用程序类,这些实用程序类将自动填充到MATLAB信号中传送于访问。例如,您可以使用 ImageJ2数据集并执行阈值处理(或任何其他处理步骤),然后在MATLAB中使用IJM.getDatasetAs(name)命令将活动数据集设置为具有指定名称的MATLAB矩阵变量,以进行进一步的开放分析。

例如,我们可以通过在 MATLAB 提示符下执行以下命令来获得相同的结果,而不是使用 described above 这样的脚本: 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 ImageJ2 IJM.show(‘mask’) % display the mask array in ImageJ2 如果您希望将 ImageJ2 图像的数值数据分配到 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 #### 在 ImageJ 中打开 2D-5D MATLAB 作为备份 Hyperstack

如果您使用copytoImagePlus,则可以使用简单的语法在 ImageJ 中使用正确的图像类型(对于 uint8、uint16 和其他类型的 MATLAB 总线分别为 8 位、16 位和 32 位)将 2D-5D MATLAB 总线打开为 Hyperstack。此外,您可以在 MATLAB 函数中使用 copytoImagePlus,独立于基础工作区中的 IJM 变量。 imp = ij.IJ.openImage(“http://imagej.net/ij/images/Spindly-GFP.zip”); imp.show(); IJM.getDatasetAs(‘I’); % I is 5-D double with the size of 171x196x2x5x51 in MATLAB I16 = uint16(I);

IJM.show('I16') % ImagePlus, 32-bit, 171x196x1x1x510

imp4 = copytoImagePlus(I16,'XYCZT') % ImagePlus, 16-bit, 171x196x2x5x51
imp4.show(); `IJM.show(name)`无法删除维度和数据类型。通道(C)、切片(Z)和帧(T)均被视为帧。图像类型为32位。<img src="/media/scripting/ijmshow-image006.png" alt="IJM.show result" width="150"/>

copytoImagePlus可以保留维度和数据类型。copytoImagePlus result

来源

视频演示

ImageJ+MATLAB: Reunited

</div> # 出版 doi:10.1093/bioinformatics/btw681 #另请参阅 - [Creating Imglib2 images in MATLAB](/libs/imglib2/matlab) - [Using TrackMate from MATLAB](/plugins/trackmate/using-from-matlab) - [Analyzing TrackMate results with MATLAB](/plugins/trackmate/analyzing-results-with-matlab) - [Comparison of Matlab functions and Ops](/libs/imagej-ops/comparison-with-matlab) - [Miji](/plugins/miji)(旧版)