From 2d19c08bb9be955ea5efb02ffa56871890d0b2a8 Mon Sep 17 00:00:00 2001 From: Jacques Pecreaux Date: Sat, 9 Oct 2010 02:10:05 +0200 Subject: [PATCH 01/36] Add a convenience Matlab script to call MIJ Signed-off-by: Johannes Schindelin --- scripts/Miji.m | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 scripts/Miji.m diff --git a/scripts/Miji.m b/scripts/Miji.m new file mode 100644 index 0000000..40c306e --- /dev/null +++ b/scripts/Miji.m @@ -0,0 +1,34 @@ +cwd = pwd; +%% +cd (fileparts(mfilename('fullpath'))); +cd ..; +cd jars; +%% +if ~exist('mij.jar', 'file') + disp('you seem not to have mij.jar in the jars/ folder, downloading it!'); + urlwrite('http://bigwww.epfl.ch/sage/soft/mij/mij.jar', 'mij.jar'); +end +%% +%path_ = fullfile(pwd,'ij.jar'); +%% +test = dir('*.jar'); +path_= cell(1); +for i = 1:length(test) + path_{i} = (fullfile(pwd, test(i).name)); +end +shift = length(test); +%% +cd ..; +cd plugins; +test = dir('*.jar'); +for i = 1:length(test) + path_{i+shift} = (fullfile(pwd, test(i).name)); +end +%% +javaaddpath(path_, '-end'); +%% +cd ..; +disp([sprintf('\n') sprintf('\n') 'Use MIJ.exit to end the session' sprintf('\n') sprintf('\n')]); +MIJ.start(pwd); +%% +cd(cwd); From bbb461cca321b5de0cf805d9f4fb6e34ba9c1cff Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 9 Oct 2010 02:11:04 +0200 Subject: [PATCH 02/36] Miji: Add some rudimentary documentation Signed-off-by: Johannes Schindelin --- scripts/Miji.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/Miji.m b/scripts/Miji.m index 40c306e..428b759 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,3 +1,5 @@ +%% This script sets up the classpath to Fiji and starts MIJ +%% Author: Jacques Pecreaux cwd = pwd; %% cd (fileparts(mfilename('fullpath'))); From c0d0fd44fa1a1a0e00cfc2fee60d339191874690 Mon Sep 17 00:00:00 2001 From: Jacques Pecreaux Date: Sat, 9 Oct 2010 02:19:10 +0200 Subject: [PATCH 03/36] Add a demonstration how to use Miji Signed-off-by: Johannes Schindelin --- scripts/Miji_Test.m | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 scripts/Miji_Test.m diff --git a/scripts/Miji_Test.m b/scripts/Miji_Test.m new file mode 100644 index 0000000..dbb7365 --- /dev/null +++ b/scripts/Miji_Test.m @@ -0,0 +1,6 @@ +Miji(); +MIJ.run('Embryos (42K)'); +I=MIJ.getCurrentImage; +E = imadjust(wiener2(im2double(I(:,:,1)))); +imshow(E); +MIJ.createImage('result',E); From 0377a63d39c5245c51ab90552a4b6f227357c5c8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 9 Oct 2010 11:35:33 +0200 Subject: [PATCH 04/36] Miji: add an option not to start ImageJ Sometimes, you only want to use parts of Fiji as a library, not start the interactive ImageJ. To do this, start 'Miji(false)'. By default, ImageJ will still be started (e.g. 'Miji' does the same as 'Miji(true)'). Signed-off-by: Johannes Schindelin --- scripts/Miji.m | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index 428b759..f24e7dc 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,5 +1,11 @@ +function Miji(open_imagej) %% This script sets up the classpath to Fiji and starts MIJ %% Author: Jacques Pecreaux + +if nargin < 1 + open_imagej = true; +end + cwd = pwd; %% cd (fileparts(mfilename('fullpath'))); @@ -29,8 +35,12 @@ %% javaaddpath(path_, '-end'); %% -cd ..; -disp([sprintf('\n') sprintf('\n') 'Use MIJ.exit to end the session' sprintf('\n') sprintf('\n')]); -MIJ.start(pwd); + +if open_imagej + cd ..; + disp([sprintf('\n') sprintf('\n') 'Use MIJ.exit to end the session' sprintf('\n') sprintf('\n')]); + MIJ.start(pwd); +end + %% cd(cwd); From 5f2889b7039e6489b4d6b38ba7ffb7334aa673f9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 8 Dec 2010 21:53:41 +0100 Subject: [PATCH 05/36] Miji: avoid warnings While at it, almost rewrite the thing to avoid unnecessary cd'ing. Signed-off-by: Johannes Schindelin --- scripts/Miji.m | 78 +++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index f24e7dc..d4854de 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,46 +1,46 @@ function Miji(open_imagej) -%% This script sets up the classpath to Fiji and starts MIJ -%% Author: Jacques Pecreaux + %% This script sets up the classpath to Fiji and optionally starts MIJ + %% Author: Jacques Pecreaux & Johannes Schindelin + if nargin < 1 + open_imagej = true; + end -if nargin < 1 - open_imagej = true; -end + %% Get the Fiji directory + fiji_directory = fileparts(fileparts(mfilename('fullpath'))); -cwd = pwd; -%% -cd (fileparts(mfilename('fullpath'))); -cd ..; -cd jars; -%% -if ~exist('mij.jar', 'file') - disp('you seem not to have mij.jar in the jars/ folder, downloading it!'); - urlwrite('http://bigwww.epfl.ch/sage/soft/mij/mij.jar', 'mij.jar'); -end -%% -%path_ = fullfile(pwd,'ij.jar'); -%% -test = dir('*.jar'); -path_= cell(1); -for i = 1:length(test) - path_{i} = (fullfile(pwd, test(i).name)); -end -shift = length(test); -%% -cd ..; -cd plugins; -test = dir('*.jar'); -for i = 1:length(test) - path_{i+shift} = (fullfile(pwd, test(i).name)); + %% Get the Java classpath + classpath = javaclasspath('-all'); + + %% Add all libraries in jars/ and plugins/ to the classpath + add_to_classpath(classpath, strcat([fiji_directory filesep 'jars'])); + add_to_classpath(classpath, strcat([fiji_directory filesep 'plugins'])); + + %% Maybe open the ImageJ window + if open_imagej + cd ..; + fprintf('\n\nUse MIJ.exit to end the session\n\n'); + MIJ.start(pwd); + end end -%% -javaaddpath(path_, '-end'); -%% -if open_imagej - cd ..; - disp([sprintf('\n') sprintf('\n') 'Use MIJ.exit to end the session' sprintf('\n') sprintf('\n')]); - MIJ.start(pwd); +function add_to_classpath(classpath, directory) + % Get all .jar files in the directory + test = dir(strcat([directory filesep '*.jar'])); + path_= cell(0); + for i = 1:length(test) + if not_yet_in_classpath(classpath, test(i).name) + path_{length(path_) + 1} = strcat([directory filesep test(i).name]); + end + end + + %% Add them to the classpath + if ~isempty(path_) + javaaddpath(path_, '-end'); + end end -%% -cd(cwd); +function test = not_yet_in_classpath(classpath, filename) +%% Test whether the library was already imported +expression = strcat([filesep filename '$']); +test = isempty(cell2mat(regexp(classpath, expression))); +end \ No newline at end of file From 25251b0df1733f32868d52fa472fd3cd306fd166 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Tue, 19 Jul 2011 15:52:18 +0200 Subject: [PATCH 06/36] Miji: fix bug in startup path Signed-off-by: Jean-Yves Tinevez --- scripts/Miji.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index d4854de..fc442bd 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -19,7 +19,7 @@ function Miji(open_imagej) if open_imagej cd ..; fprintf('\n\nUse MIJ.exit to end the session\n\n'); - MIJ.start(pwd); + MIJ.start(fiji_directory); end end @@ -43,4 +43,4 @@ function add_to_classpath(classpath, directory) %% Test whether the library was already imported expression = strcat([filesep filename '$']); test = isempty(cell2mat(regexp(classpath, expression))); -end \ No newline at end of file +end From 01c1c9052446a837e59424306c20d5dd5db106e5 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Thu, 21 Jul 2011 23:22:55 +0200 Subject: [PATCH 07/36] Miji: turn off some warnings Signed-off-by: Jean-Yves Tinevez --- scripts/Miji.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/Miji.m b/scripts/Miji.m index fc442bd..ab902cc 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -4,6 +4,7 @@ function Miji(open_imagej) if nargin < 1 open_imagej = true; end + %% Get the Fiji directory fiji_directory = fileparts(fileparts(mfilename('fullpath'))); @@ -12,8 +13,15 @@ function Miji(open_imagej) classpath = javaclasspath('-all'); %% Add all libraries in jars/ and plugins/ to the classpath + + % Switch off warning + warning_state = warning('off'); + add_to_classpath(classpath, strcat([fiji_directory filesep 'jars'])); add_to_classpath(classpath, strcat([fiji_directory filesep 'plugins'])); + + % Switch warning back to initial settings + warning(warning_state) %% Maybe open the ImageJ window if open_imagej From 670b64607344decb7a3c860d836c84c01f0c7873 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Mon, 25 Jul 2011 22:19:29 +0200 Subject: [PATCH 08/36] Miji: updated Java3D installer, added 2 demo scripts Signed-off-by: Jean-Yves Tinevez --- scripts/Matlab3DViewerDemo_1.m | 119 ++++++++++++++++++++++++++++++ scripts/Matlab3DViewerDemo_2.m | 131 +++++++++++++++++++++++++++++++++ scripts/Miji_Test.m | 2 +- 3 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 scripts/Matlab3DViewerDemo_1.m create mode 100644 scripts/Matlab3DViewerDemo_2.m diff --git a/scripts/Matlab3DViewerDemo_1.m b/scripts/Matlab3DViewerDemo_1.m new file mode 100644 index 0000000..62a7691 --- /dev/null +++ b/scripts/Matlab3DViewerDemo_1.m @@ -0,0 +1,119 @@ +%% MATLAB 3D viewer demo 1 +% +% This example demonstrates how to use Miji to render a volume in 3D using +% accelerated graphics, if your computer is adequately equiped. We use the +% well known 'mri' data distributed with MATLAB, and render it in the +% ImageJ 3D viewer. + +%% Load and prepare data using MATLAB +% Load an example of 3D data from standard Matlab +load('mri.mat'); + +%% +% Make a simply 8-bit 3D image from it. There is a singleton dimension to +% remove. +I = squeeze(D); + +%% +% Ok, now this is an indexed color image (even if it is made a grays). The +% actual color mapping is contained in the 'map' variable. We will make a +% RGB image out of it. +[R G B] = ind2rgb(I, map); + +%% +% Just a short note here: we are going a complicated way to regenerate a +% color image from an indexed image and its color mapping, because this is +% the way the data is shipped to us in that MATLAB sample. It is a step +% proper to MATLAB only, and does not affect nor relate to the Miji +% framework we demonstrate here. +% We could skip this step by directly feeding the image 'I' to MIJ, instead +% of the color image 'J', using another method. The colors would not be +% right, but would be enough to demonstrate the 3D rendering. +% However, let us do things properly. + +%% +% An extra step to do: R, G and B now contains doubles (0 to 1), and we +% would like to make 3 channels of uint8 out of them. +R = uint8(255 * R); +G = uint8(255 * G); +B = uint8(255 * B); + +%% +% We now put them together into one 3D color image (that is, with 4D). To +% do so, we simply concatenate them along the 3th dimension. +J = cat(4, R,G,B); + +%% +% A note here: MIJ expects the dimensions of a 3D color image to be the +% following: [ x y z color ]; this is why we did this 'cat' operation just +% above. However, if you want to display the data in MATLAB's native +% implay, they must be in the following order: [ x y color z ]. In the +% latter case, 'permute' is your friend. + +%% Configure data volume using ImageJ objects +% Now we start the fun and new part. +% Everything so far has been only about MATLAB operations. + +%% +% First, we launch Miji. Here we use the launcher in non-interactive mode. +% The only thing that this will do is actually to set the path so that the +% subsequent commands and classes can be found by Matlab. +% We launched it with a 'false' in argument, to specify that we do not want +% to diplay the ImageJ toolbar. Indeed, this example is a command line +% example, so we choose not to display the GUI. Feel free to experiment. +Miji(false) + +%% +% The 3D viewer can only display ImagePlus. ImagePlus is the way ImageJ +% represent images. We can't feed it directly MATLAB data. Fortunately, +% that is where MIJ comes into handy. It has a function that can create an +% ImagePlus from a Matlab object. +% 1. The first argument is the name we will give to the image. +% 2. The second argument is the Matlab data +% 3. The last argument is a boolean. If true, the ImagePlus will be +% displayed as an image sequence. You might find this useful as well. +imp = MIJ.createColor('MRI data', J, false); + +%% +% Since we had a color volume (4D data), we used the createColor method. If +% we had only a grayscale volume (3D data), we could have used the +% createImage method instead, which works the same. + +%% +% Now comes a little non-mandatory tricky bit. +% By default, the 3D viewer will assume that the image voxel is square, +% that is, every voxel has a size of 1 in the X, Y and Z direction. +% However, for the MRI data we are playing with, this is incorrect, as a +% voxel is 2.5 times larger in the Z direction that in the X and Y +% direction. +% If we do not correct that, the head we are trying to display will look +% flat. +% A way to tell this to the 3D viewer is to create a Calibration object and +% set its public field pixelDepth to 2.5. Then we set this object to be the +% calibration of the ImagePlus, and the 3D viewer will be able to deal with +% it. +calibration = ij.measure.Calibration(); +calibration.pixelDepth = 2.5; +imp.setCalibration(calibration); + +%% Display the data in ImageJ 3D viewer +% Now for the display itself. +% +% We create an empty 3D viewer to start with. We do not show it yet. +universe = ij3d.Image3DUniverse(); + +%% +% Now we show the 3D viewer window. +universe.show(); + +%% +% Then we send it the data, and ask it to be displayed as a volumetric +% rendering. +c = universe.addVoltex(imp); +%% +% Et voilà! +%% +% +% <> +% + diff --git a/scripts/Matlab3DViewerDemo_2.m b/scripts/Matlab3DViewerDemo_2.m new file mode 100644 index 0000000..017bf95 --- /dev/null +++ b/scripts/Matlab3DViewerDemo_2.m @@ -0,0 +1,131 @@ +%% MATLAB 3D viewer demo 2 +% +% In this example, we will demonstrate the surface rendering capabilities +% of the 3D viewer. +% +% Rather than using an existing dataset, we will create a dummy one, in the +% shape of multiple balls or octahedrons. The balls will be created one by +% one, from a volume where all pixels are 0, but within the object volume, +% which location and size is set randomly. From this volume, we ask the 3D +% viewer to create an iso-surface (one for each object), and to display it +% in the 3D universe, with a random color. + +%% +% Let us define the size of our target image volume. Not too big to save +% memory. +width = 128; +height = 128; +depth = 64; + +%% +% Here we determine how many objects we want, and their mean size. We will +% generate them randomly. +nballs = 10; +radius = 20; + +%% +% We pre-allocate the image volume. +I = zeros(width, height, depth, 'uint8'); + +%% +% To simplify the object creation step, we create the grid containing each +% pixel coordinates. +[X, Y, Z] = meshgrid(1:width, 1:height, 1:depth); + +%% +% Since we want to add the objects in live mode, we need to create and show +% the universe before creating and adding the balls. So we call Miji first, +Miji(false) + +%% +% Then deal with the 3D universe. +universe = ij3d.Image3DUniverse(); +universe.show(); + +%% +% Now add the objects one by one, in a loop. +for i = 1 : nballs + + %% + % Determine the ball center and radius randomly. + x = round( width * rand ); + y = round( height * rand ); + z = round( depth * rand ); + r = round( radius * (1 + randn/2) ); + intensity = 255; + + %% + % Logical indexing: we find the pixel indices that are within object + % volume. + + % This gives you a ball + % index = (X-x).^2 + (Y-y).^2 + (Z-z).^2 < r^2; + + % This gives you an octaedron + index = abs(X-x) + abs(Y-y) + abs(Z-z) < r; + + %% + % Then we set the intensity of these pixels to 'intensity' (here, 255) + % and the value of pixels outside the object volume to 0. + I(~index) = 0; + I(index) = intensity; + + %% + % We now export this MATLAB volume to a new ImagePlus image, without + % displaying it. + imp = MIJ.createImage(['Ball ' int2str(i)], I, false); + + %% + % It is possible to specify the color of the iso-surface at creation, + % but the 3D viewer expects a |Color3f| object, which is part of the + % |javax.vecmath package|. We determine its actual color randmoly + % again. + color = javax.vecmath.Color3f(rand, rand, rand); + + %% + % Finally, we add the object to the 3D universe, in the shape of an + % iso-surface. Arguments meaning is detailed below. + c = universe.addMesh(imp, ... - this is the source ImagePlus + color, ... - this is the destination color + ['Ball ' int2str(i)], ... - the name of the iso-surface + 1, ... - the threshold, see below + [true true true], ... - what channels to display + 1 ... - the resampling factor + ); + + %% + % Ok, so the meanings of the |imp|, |color| and |name| arguments are + % trivial. The |threshold| value, here set to 1, is important for this + % display mode. + % + % Isosurface rendering uses the fast marching cube algorithm, which + % assumes that there is threshold that separates the background from + % the foreground. In our case, it is easy to determine, since pixels + % out of the ball have a 0 intensity, and inside the ball they have an + % intensity of 255. So any threshold value between these two numbers + % would have worked. + % + % The 3 boolean array is used to specify what channels will be included + % in the surface rendering, if you feed it a color image. In our case, + % we simply set them all to true and do not care much. Finally, the + % resampling factor is used to win some time and memory at the expense + % of precision. If set to more than 1, the image is downsampled before + % the marching cube algorithm is applied, resulting in less meshes + % obtained in smaller time. + + %% + % Now, this individual step is going to take some time, so it is very + % likely that you see each ball appearing one by one. That is because + % computing the surface mesh is expensive, so the rendering lags a bit + % the command. + +end + +%% +% That's it. Note that some of our objects might be clipped because they +% cross the boundaries of the volume we defined in the variable |I|. +% +% +% +% <> +% diff --git a/scripts/Miji_Test.m b/scripts/Miji_Test.m index dbb7365..4f4955f 100644 --- a/scripts/Miji_Test.m +++ b/scripts/Miji_Test.m @@ -3,4 +3,4 @@ I=MIJ.getCurrentImage; E = imadjust(wiener2(im2double(I(:,:,1)))); imshow(E); -MIJ.createImage('result',E); +MIJ.createImage('result',E, true); From 75587c849ceba775d1f7639a04cfc3cda163338a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 25 Jul 2011 23:16:18 +0200 Subject: [PATCH 09/36] Miji's 3D examples: auto-install Java3D Unfortunately, a restart is required if Java3D was not installed, but it is as convenient now as we could make it, at least. Signed-off-by: Johannes Schindelin --- scripts/Matlab3DViewerDemo_1.m | 7 +++++++ scripts/Matlab3DViewerDemo_2.m | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/scripts/Matlab3DViewerDemo_1.m b/scripts/Matlab3DViewerDemo_1.m index 62a7691..4367fa0 100644 --- a/scripts/Matlab3DViewerDemo_1.m +++ b/scripts/Matlab3DViewerDemo_1.m @@ -5,6 +5,13 @@ % well known 'mri' data distributed with MATLAB, and render it in the % ImageJ 3D viewer. +%% Make sure Java3D is installed +% If not, try to install it + +if ~IsJava3DInstalled(true) + return +end + %% Load and prepare data using MATLAB % Load an example of 3D data from standard Matlab load('mri.mat'); diff --git a/scripts/Matlab3DViewerDemo_2.m b/scripts/Matlab3DViewerDemo_2.m index 017bf95..59629c6 100644 --- a/scripts/Matlab3DViewerDemo_2.m +++ b/scripts/Matlab3DViewerDemo_2.m @@ -10,6 +10,13 @@ % viewer to create an iso-surface (one for each object), and to display it % in the 3D universe, with a random color. +%% Make sure Java3D is installed +% If not, try to install it + +if ~IsJava3DInstalled(true) + return +end + %% % Let us define the size of our target image volume. Not too big to save % memory. From 8e45667aa88496f99f2df8cd82ad8560d439178d Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Mon, 25 Jul 2011 23:59:39 +0200 Subject: [PATCH 10/36] Miji: add the overview Matlab file for the 3D Viewer examples This file only contains comments, as it is meant to be published using Matlab's publish() function. Signed-off-by: Johannes Schindelin --- scripts/Matlab3DViewerIntroduction.m | 135 +++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 scripts/Matlab3DViewerIntroduction.m diff --git a/scripts/Matlab3DViewerIntroduction.m b/scripts/Matlab3DViewerIntroduction.m new file mode 100644 index 0000000..865601a --- /dev/null +++ b/scripts/Matlab3DViewerIntroduction.m @@ -0,0 +1,135 @@ +%% How to install Fiji, and make MATLAB aware of it. +% +% ImageJ is an image processing software, very popular in the Life-Science +% field. If you are a master student, a PhD or a postdoc in Biology, there +% is a very good chance that ImageJ is installed on your computer. +% +% *ImageJ* is developed mainly by Wayne Rasband, and is written in Java +% (see http://rsb.info.nih.gov/ij/index.html). It has a clever plugin +% system that allows users to extend it with new functionalities. +% +% *Fiji* (Fiji is Just ImageJ, +% http://pacific.mpi-cbg.de/wiki/index.php/Main_Page) is a _distribution_ +% of ImageJ, that include(amongst other things) a lot of plugins, some +% useful libraries and an updater to keep everything up to date. +% +% Amongst other things, it is made to facilitate inter-operability with +% other softwares. For instance, it offers scripting facilities using +% popular languages such as Python, Ruby, Clojure, Beanshell, Javascript, +% etc. It is also shipped with *Miji*, a set of utilities made to ease its +% interaction with MATLAB. Since ImageJ is written in Java, it can be +% called directly from within MATLAB, and be used as a supporting image +% visualization, processing or analysis tool. +% +% In this demo bundle, we demonstrate how to use Fiji to get a very nice, +% hardware accelerated, 3D viewer in MATLAB. +% +% This is not a totally trivial example, as it requires to have the Java +% libraries for accelerated 3D rendering installed for the MATLAB Java +% Virtual Machine. Fortunately, Fiji is shipped with a script that +% automates the process. +% +% But you still have to install Fiji. + +%% Installing Fiji +% +% Simply go to http://pacific.mpi-cbg.de/wiki/index.php/Downloads and +% download the binaries for your OS. You will not find an installer; you +% just have to unzip the Fiji folder to somewhere convenient for you. +% +% For demonstration purposes, let us assume you have a PC machine that +% runs a 64-bit version of Windows. You then have downloaded the matching +% Fiji version (fiji-win64-something.zip), and unzipped it in |C:\Program +% Files\| and now have a folder called |C:\Program Files\Fiji.app|. +% +% Before launching MATLAB, launch Fiji itself. You will find an executable +% in the Fiji folder, that you just need to run. At startup, Fiji should +% offer you to go to the *updater*. Do that and run the update. If you are +% not proposed this, you can find the updater in the _Help > Update Fiji_ +% menu item. +% +% Close Fiji when the update is done. +% +% A side note: ImageJ and its plugin are very useful if you are interested +% in scientific Image Processing. There is a zillion things you can do with +% it. If you are interested in using Fiji as a standalone application, +% check some of the tutorials there: +% http://pacific.mpi-cbg.de/wiki/index.php/Documentation. +% +%% Making MATLAB aware of Fiji +% +% The utilities for interaction with MATLAB can all be accessed by adding a +% single folder to the MATLAB path. They are all located in the |scripts| +% folder of the Fiji folder. +% +% So, if you have installed Fiji in |C:\Program Files\Fiji.app|, you just +% have to add |C:\Program Files\Fiji.app\scripts| to the MATLAB path, and +% that's all. + +%% Installing the Java3D libraries +% +% You are done with the main installation part. You just have to do it +% once, and if some updates are made to Fiji, you just have to launch its +% updater to get them. +% +% You still have to install the *Java3D* libraries. Fortunately, this is +% done automatically with a Fiji script: +% +% * Launch MATLAB; +% * type |InstallJava3D| in the command window. +% +% If you get a message stating that the function cannot be done, this is +% because you did not add the Fiji |scripts| folder to the MATLAB path. in +% that case, go back to the previous paragraph. +% +% The |InstallJava3D| will tell you what it does verbosely. It will check +% whether your JVM already has Java3D installed, and if the version +% requirement is met. If not, it will automatically download it and install +% it to the right folder. +% +% There might be some issues, if you do not have write permission to the +% target folder. In that case, the script will tell you what folder to make +% writable. Then you can simple re-launch the script. +% +% Once you are done, you have to restart MATLAB, so that the new libraries +% are sourced. +% +%% What now? +% +% You are all set. +% +% You can now play with the 2 demo files |Matlab3DViewerDemo_1.m| and +% |Matlab3DViewerDemo_2.m|, that are heavily annotated and published in +% the same folder than this file. +% +% * |Matlab3DViewerDemo_1| shows how to load a 3D data in MATLAB and render +% it in 3D (volumetric rendering); +% * |Matlab3DViewerDemo_2| shows how to create simple objects volumes in +% MATLAB and make a surface rendering of them. +% +% *MIJ* is the low level Java class that contains static methods to +% exchange data between MATLAB and ImageJ. You can find its documentation +% there: http://bigwww.epfl.ch/sage/soft/mij/ +% +% *Miji* is the glue that facilitates the use of all these components. It +% is described in the aforementioned demo files. +% +%% Authors +% +% * ImageJ is written by Wayne Rasband and friends for the core and the +% plugins; +% * Fiji is written by a bunch of enthusiasts, under the holy auspices of +% Johannes Schindelin and friends; +% * The ImageJ 3D viewer is written by Benjamin Schmidt and Johannes +% Schindelin; +% * MIJ is written by Daniel Sage; +% * Miji is written by Johannes Schindelin, Jacques Pécréaux and Jean-Yves +% Tinevez. +% +% It's all open source, feel free to modify, distribute, comment or +% whatever you feel like. +% +%% +% +% _Jean-Yves Tinevez \ - July 2011_ + From 25c5069022deb0b37c93a6f6c6b5a3b3211b4fde Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 26 Jul 2011 00:12:10 +0200 Subject: [PATCH 11/36] Miji 3D Viewer example: a few touchups Signed-off-by: Johannes Schindelin --- scripts/Matlab3DViewerDemo_1.m | 23 +++++----- scripts/Matlab3DViewerDemo_2.m | 66 +++++++++++++-------------- scripts/Matlab3DViewerIntroduction.m | 68 ++++++++++++++-------------- 3 files changed, 77 insertions(+), 80 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_1.m b/scripts/Matlab3DViewerDemo_1.m index 4367fa0..366e692 100644 --- a/scripts/Matlab3DViewerDemo_1.m +++ b/scripts/Matlab3DViewerDemo_1.m @@ -1,9 +1,9 @@ %% MATLAB 3D viewer demo 1 % % This example demonstrates how to use Miji to render a volume in 3D using -% accelerated graphics, if your computer is adequately equiped. We use the -% well known 'mri' data distributed with MATLAB, and render it in the -% ImageJ 3D viewer. +% accelerated graphics, if your computer is adequately equiped (most +% computers are, these days). We use the well known 'mri' data distributed +% with MATLAB, and render it in the ImageJ 3D viewer. %% Make sure Java3D is installed % If not, try to install it @@ -17,7 +17,7 @@ load('mri.mat'); %% -% Make a simply 8-bit 3D image from it. There is a singleton dimension to +% Make a simple 8-bit 3D image from it. There is a singleton dimension to % remove. I = squeeze(D); @@ -52,7 +52,7 @@ %% % A note here: MIJ expects the dimensions of a 3D color image to be the -% following: [ x y z color ]; this is why we did this 'cat' operation just +% following: [ x y z color ]; this is why we did this 'cat' operation just % above. However, if you want to display the data in MATLAB's native % implay, they must be in the following order: [ x y color z ]. In the % latter case, 'permute' is your friend. @@ -74,7 +74,7 @@ % The 3D viewer can only display ImagePlus. ImagePlus is the way ImageJ % represent images. We can't feed it directly MATLAB data. Fortunately, % that is where MIJ comes into handy. It has a function that can create an -% ImagePlus from a Matlab object. +% ImagePlus from a Matlab object. % 1. The first argument is the name we will give to the image. % 2. The second argument is the Matlab data % 3. The last argument is a boolean. If true, the ImagePlus will be @@ -92,7 +92,7 @@ % that is, every voxel has a size of 1 in the X, Y and Z direction. % However, for the MRI data we are playing with, this is incorrect, as a % voxel is 2.5 times larger in the Z direction that in the X and Y -% direction. +% direction. % If we do not correct that, the head we are trying to display will look % flat. % A way to tell this to the 3D viewer is to create a Calibration object and @@ -104,7 +104,7 @@ imp.setCalibration(calibration); %% Display the data in ImageJ 3D viewer -% Now for the display itself. +% Now for the display itself. % % We create an empty 3D viewer to start with. We do not show it yet. universe = ij3d.Image3DUniverse(); @@ -120,7 +120,6 @@ %% % Et voilà! %% -% -% <> -% - +% +% <> +% diff --git a/scripts/Matlab3DViewerDemo_2.m b/scripts/Matlab3DViewerDemo_2.m index 59629c6..a58566b 100644 --- a/scripts/Matlab3DViewerDemo_2.m +++ b/scripts/Matlab3DViewerDemo_2.m @@ -2,11 +2,11 @@ % % In this example, we will demonstrate the surface rendering capabilities % of the 3D viewer. -% +% % Rather than using an existing dataset, we will create a dummy one, in the % shape of multiple balls or octahedrons. The balls will be created one by % one, from a volume where all pixels are 0, but within the object volume, -% which location and size is set randomly. From this volume, we ask the 3D +% whose location and size is set randomly. From this volume, we ask the 3D % viewer to create an iso-surface (one for each object), and to display it % in the 3D universe, with a random color. @@ -17,14 +17,14 @@ return end -%% +%% % Let us define the size of our target image volume. Not too big to save % memory. width = 128; height = 128; depth = 64; -%% +%% % Here we determine how many objects we want, and their mean size. We will % generate them randomly. nballs = 10; @@ -34,7 +34,7 @@ % We pre-allocate the image volume. I = zeros(width, height, depth, 'uint8'); -%% +%% % To simplify the object creation step, we create the grid containing each % pixel coordinates. [X, Y, Z] = meshgrid(1:width, 1:height, 1:depth); @@ -52,7 +52,7 @@ %% % Now add the objects one by one, in a loop. for i = 1 : nballs - + %% % Determine the ball center and radius randomly. x = round( width * rand ); @@ -60,72 +60,72 @@ z = round( depth * rand ); r = round( radius * (1 + randn/2) ); intensity = 255; - + %% - % Logical indexing: we find the pixel indices that are within object - % volume. - + % Logical indexing: we find the pixel indices that are within the + % object's volume. + % This gives you a ball % index = (X-x).^2 + (Y-y).^2 + (Z-z).^2 < r^2; - + % This gives you an octaedron index = abs(X-x) + abs(Y-y) + abs(Z-z) < r; - + %% % Then we set the intensity of these pixels to 'intensity' (here, 255) % and the value of pixels outside the object volume to 0. I(~index) = 0; I(index) = intensity; - + %% % We now export this MATLAB volume to a new ImagePlus image, without % displaying it. imp = MIJ.createImage(['Ball ' int2str(i)], I, false); - + %% % It is possible to specify the color of the iso-surface at creation, % but the 3D viewer expects a |Color3f| object, which is part of the - % |javax.vecmath package|. We determine its actual color randmoly + % |javax.vecmath package|. We determine its actual color randomly % again. color = javax.vecmath.Color3f(rand, rand, rand); - + %% % Finally, we add the object to the 3D universe, in the shape of an - % iso-surface. Arguments meaning is detailed below. + % iso-surface. Arguments meaning is detailed below. c = universe.addMesh(imp, ... - this is the source ImagePlus color, ... - this is the destination color ['Ball ' int2str(i)], ... - the name of the iso-surface 1, ... - the threshold, see below [true true true], ... - what channels to display 1 ... - the resampling factor - ); + ); %% % Ok, so the meanings of the |imp|, |color| and |name| arguments are % trivial. The |threshold| value, here set to 1, is important for this - % display mode. + % display mode. % % Isosurface rendering uses the fast marching cube algorithm, which - % assumes that there is threshold that separates the background from + % assumes that there is a threshold that separates the background from % the foreground. In our case, it is easy to determine, since pixels % out of the ball have a 0 intensity, and inside the ball they have an % intensity of 255. So any threshold value between these two numbers - % would have worked. + % would have worked. % - % The 3 boolean array is used to specify what channels will be included - % in the surface rendering, if you feed it a color image. In our case, - % we simply set them all to true and do not care much. Finally, the - % resampling factor is used to win some time and memory at the expense - % of precision. If set to more than 1, the image is downsampled before - % the marching cube algorithm is applied, resulting in less meshes - % obtained in smaller time. - + % The array containing the 3 boolean values is used to specify what + % channels will be included in the surface rendering, if you feed it a + % color image. In our case, we simply set them all to true and do not + % care much. Finally, the resampling factor is used to win some time + % and memory at the expense of precision. If set to more than 1, the + % image is downsampled before the marching cube algorithm is applied, + % resulting in less meshes obtained in smaller time. + %% % Now, this individual step is going to take some time, so it is very % likely that you see each ball appearing one by one. That is because % computing the surface mesh is expensive, so the rendering lags a bit - % the command. - + % behind the command. + end %% @@ -133,6 +133,6 @@ % cross the boundaries of the volume we defined in the variable |I|. % % -% +% % <> -% +% diff --git a/scripts/Matlab3DViewerIntroduction.m b/scripts/Matlab3DViewerIntroduction.m index 865601a..b24dfbb 100644 --- a/scripts/Matlab3DViewerIntroduction.m +++ b/scripts/Matlab3DViewerIntroduction.m @@ -2,40 +2,39 @@ % % ImageJ is an image processing software, very popular in the Life-Science % field. If you are a master student, a PhD or a postdoc in Biology, there -% is a very good chance that ImageJ is installed on your computer. -% +% is a very good chance that ImageJ is installed on your computer. +% % *ImageJ* is developed mainly by Wayne Rasband, and is written in Java % (see http://rsb.info.nih.gov/ij/index.html). It has a clever plugin % system that allows users to extend it with new functionalities. % -% *Fiji* (Fiji is Just ImageJ, -% http://pacific.mpi-cbg.de/wiki/index.php/Main_Page) is a _distribution_ -% of ImageJ, that include(amongst other things) a lot of plugins, some +% *Fiji* (Fiji is Just ImageJ, http://fiji.sc/) is a _distribution_ of +% ImageJ, that includes (amongst other things) a lot of plugins, some % useful libraries and an updater to keep everything up to date. % % Amongst other things, it is made to facilitate inter-operability with % other softwares. For instance, it offers scripting facilities using % popular languages such as Python, Ruby, Clojure, Beanshell, Javascript, -% etc. It is also shipped with *Miji*, a set of utilities made to ease its +% etc. It also ships with *Miji*, a set of utilities made to ease its % interaction with MATLAB. Since ImageJ is written in Java, it can be % called directly from within MATLAB, and be used as a supporting image % visualization, processing or analysis tool. % % In this demo bundle, we demonstrate how to use Fiji to get a very nice, -% hardware accelerated, 3D viewer in MATLAB. +% hardware accelerated, 3D viewer in MATLAB. % % This is not a totally trivial example, as it requires to have the Java % libraries for accelerated 3D rendering installed for the MATLAB Java -% Virtual Machine. Fortunately, Fiji is shipped with a script that -% automates the process. +% Virtual Machine. Fortunately, Fiji comes with a script that automates +% the process. % % But you still have to install Fiji. %% Installing Fiji % -% Simply go to http://pacific.mpi-cbg.de/wiki/index.php/Downloads and -% download the binaries for your OS. You will not find an installer; you -% just have to unzip the Fiji folder to somewhere convenient for you. +% Simply go to http://fiji.sc/Downloads and download the binaries for your +% OS. You will not find an installer; you just have to unzip the Fiji +% folder to somewhere convenient for you. % % For demonstration purposes, let us assume you have a PC machine that % runs a 64-bit version of Windows. You then have downloaded the matching @@ -43,7 +42,7 @@ % Files\| and now have a folder called |C:\Program Files\Fiji.app|. % % Before launching MATLAB, launch Fiji itself. You will find an executable -% in the Fiji folder, that you just need to run. At startup, Fiji should +% in the Fiji folder that you just need to run. At startup, Fiji should % offer you to go to the *updater*. Do that and run the update. If you are % not proposed this, you can find the updater in the _Help > Update Fiji_ % menu item. @@ -51,10 +50,10 @@ % Close Fiji when the update is done. % % A side note: ImageJ and its plugin are very useful if you are interested -% in scientific Image Processing. There is a zillion things you can do with -% it. If you are interested in using Fiji as a standalone application, -% check some of the tutorials there: -% http://pacific.mpi-cbg.de/wiki/index.php/Documentation. +% in scientific Image Processing. There are a zillion things you can do +% with it. If you are interested in using Fiji as a standalone +% application, check some of the tutorials there: +% http://fiji.sc/Documentation. % %% Making MATLAB aware of Fiji % @@ -74,22 +73,22 @@ % % You still have to install the *Java3D* libraries. Fortunately, this is % done automatically with a Fiji script: -% +% % * Launch MATLAB; % * type |InstallJava3D| in the command window. % -% If you get a message stating that the function cannot be done, this is -% because you did not add the Fiji |scripts| folder to the MATLAB path. in -% that case, go back to the previous paragraph. -% +% If you get a message stating that the function cannot be performed, this +% is because you did not add the Fiji |scripts| folder to the MATLAB path. +% in that case, go back to the previous paragraph. +% % The |InstallJava3D| will tell you what it does verbosely. It will check -% whether your JVM already has Java3D installed, and if the version -% requirement is met. If not, it will automatically download it and install -% it to the right folder. +% whether your Java Virtual Machine already has Java3D installed, and if +% the version requirement is met. If not, it will automatically download +% Java 3D and install it to the right folder. % -% There might be some issues, if you do not have write permission to the +% There might be some issues if you do not have write permission to the % target folder. In that case, the script will tell you what folder to make -% writable. Then you can simple re-launch the script. +% writable. Then you can simply re-launch the script. % % Once you are done, you have to restart MATLAB, so that the new libraries % are sourced. @@ -100,11 +99,11 @@ % % You can now play with the 2 demo files |Matlab3DViewerDemo_1.m| and % |Matlab3DViewerDemo_2.m|, that are heavily annotated and published in -% the same folder than this file. +% the same folder as this file. % -% * |Matlab3DViewerDemo_1| shows how to load a 3D data in MATLAB and render -% it in 3D (volumetric rendering); -% * |Matlab3DViewerDemo_2| shows how to create simple objects volumes in +% * |Matlab3DViewerDemo_1| shows how to load a 3D data set in MATLAB and +% render it in 3D (volumetric rendering); +% * |Matlab3DViewerDemo_2| shows how to create simple object volumes in % MATLAB and make a surface rendering of them. % % *MIJ* is the low level Java class that contains static methods to @@ -120,9 +119,9 @@ % plugins; % * Fiji is written by a bunch of enthusiasts, under the holy auspices of % Johannes Schindelin and friends; -% * The ImageJ 3D viewer is written by Benjamin Schmidt and Johannes -% Schindelin; -% * MIJ is written by Daniel Sage; +% * The ImageJ 3D viewer is written by Benjamin Schmid, Mark Longair, +% Albert Cardona and Johannes Schindelin; +% * MIJ is written by Daniel Sage and Dimiter Prodanov; % * Miji is written by Johannes Schindelin, Jacques Pécréaux and Jean-Yves % Tinevez. % @@ -132,4 +131,3 @@ %% % % _Jean-Yves Tinevez \ - July 2011_ - From dd2df8ff83884f5c65cffa0ed4d8c591407033e0 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Tue, 26 Jul 2011 20:28:30 +0200 Subject: [PATCH 12/36] Miji demos: add links and abide my ego Signed-off-by: Jean-Yves Tinevez --- scripts/Matlab3DViewerDemo_1.m | 3 +++ scripts/Matlab3DViewerDemo_2.m | 3 +++ scripts/Matlab3DViewerIntroduction.m | 8 ++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_1.m b/scripts/Matlab3DViewerDemo_1.m index 366e692..2bcb6c8 100644 --- a/scripts/Matlab3DViewerDemo_1.m +++ b/scripts/Matlab3DViewerDemo_1.m @@ -123,3 +123,6 @@ % % <> % +%% +% +% _Jean-Yves Tinevez \ - July 2011_ diff --git a/scripts/Matlab3DViewerDemo_2.m b/scripts/Matlab3DViewerDemo_2.m index a58566b..75f609e 100644 --- a/scripts/Matlab3DViewerDemo_2.m +++ b/scripts/Matlab3DViewerDemo_2.m @@ -135,4 +135,7 @@ % % % <> +%% % +% _Jean-Yves Tinevez \ - July 2011_ + diff --git a/scripts/Matlab3DViewerIntroduction.m b/scripts/Matlab3DViewerIntroduction.m index b24dfbb..5f106e8 100644 --- a/scripts/Matlab3DViewerIntroduction.m +++ b/scripts/Matlab3DViewerIntroduction.m @@ -101,10 +101,10 @@ % |Matlab3DViewerDemo_2.m|, that are heavily annotated and published in % the same folder as this file. % -% * |Matlab3DViewerDemo_1| shows how to load a 3D data set in MATLAB and -% render it in 3D (volumetric rendering); -% * |Matlab3DViewerDemo_2| shows how to create simple object volumes in -% MATLAB and make a surface rendering of them. +% * shows how to load a 3D +% data set in MATLAB and render it in 3D (volumetric rendering); +% * shows how to create +% simple object volumes in MATLAB and make a surface rendering of them. % % *MIJ* is the low level Java class that contains static methods to % exchange data between MATLAB and ImageJ. You can find its documentation From be41c4a371133587c0128b503778bf541191339a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 28 Jul 2011 13:53:45 +0200 Subject: [PATCH 13/36] Un-DOSify files Signed-off-by: Stephan Preibisch From 82654626f176e1fafc222a2c56964a5ba941a238 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Thu, 4 Aug 2011 21:41:06 +0200 Subject: [PATCH 14/36] Miji: update MATLAB java3D installer for Windows paths We also have to ensure that the bin folder in the MATLAB jre path is writable, otherwise it will confuse the user. Reported by Peter Beemiller Signed-off-by: Jean-Yves Tinevez --- scripts/Matlab3DViewerIntroduction.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/scripts/Matlab3DViewerIntroduction.m b/scripts/Matlab3DViewerIntroduction.m index 5f106e8..e461ffd 100644 --- a/scripts/Matlab3DViewerIntroduction.m +++ b/scripts/Matlab3DViewerIntroduction.m @@ -64,6 +64,11 @@ % So, if you have installed Fiji in |C:\Program Files\Fiji.app|, you just % have to add |C:\Program Files\Fiji.app\scripts| to the MATLAB path, and % that's all. +% +% By the way, it is in this folder that you will find all the m-files of +% this demo bundle. So you can launch the demos listed below simply by +% typing their name in MATLAB command window; they will be found since they +% are on the path. %% Installing the Java3D libraries % @@ -128,6 +133,13 @@ % It's all open source, feel free to modify, distribute, comment or % whatever you feel like. % +%% Version history +% +% * 26 Jul 2011: Initial release +% * 4 Aug 2011: Fix a bug in write permission detection on Windows +% platform, thanks to report by Peter Beemiller +% +% %% % % _Jean-Yves Tinevez \ - July 2011_ From 28fc3f178ae0fade04d177a995fb457b2302d6bf Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Thu, 11 Aug 2011 11:48:46 +0200 Subject: [PATCH 15/36] Miji: add a 3rd demo, using surface plot Now that Johannes fixed the surface plot mode of the 3D viewer, we can put this demo back. It is a demo explainging how to get the MATLAB logo rendered in 3D in Fiji 3D viewer. Signed-off-by: Jean-Yves Tinevez --- scripts/Matlab3DViewerDemo_3.m | 76 ++++++++++++++++++++++++++++ scripts/Matlab3DViewerIntroduction.m | 6 +++ 2 files changed, 82 insertions(+) create mode 100644 scripts/Matlab3DViewerDemo_3.m diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m new file mode 100644 index 0000000..8f0927b --- /dev/null +++ b/scripts/Matlab3DViewerDemo_3.m @@ -0,0 +1,76 @@ +%% MATLAB 3D viewer demo 3 +% +% In this demo file, we will use the surface plot mode of the 3D viewer. It +% is a mode of rendering where a 3D surface mesh is created from a 2D +% image. Here, the pixel intensity of the 2D image is interpreted as the +% elevation (Z coordinates), which is enough to generate the 3D surface. +% +% It is a mode which is particularly useful for images that have indeed a +% pixel intensity that can be interpreted as elevation, such as maps. Here, +% we try to use the surface plot mode of the 3D viewer with a surface well +% known to MATLAB users. + + +%% Make sure Java3D is installed +% If not, try to install it + +if ~IsJava3DInstalled(true) + return +end + +%% Generate elevation data in MATLAB +% This is the well known *peaks* dataset, that serves as a generator for +% the MATLAB logo. + +%% +% This will generate a 51*51 image, for which intensity should be +% interpreted as height. It is not a 3D data per se, but its rendering will +% be. +Z = membrane(1,25); + +%% +% The trouble is that Z is made of doubles, the most common MATLAB type, +% whether the 3D viewer only accepts 8-bit images. So we have do some +% conversion before rendering it. The following commands will stretch the +% range of Z to [0; 255] and cast to |uint8|. +Imin = min(Z(:)); +Imax = max(Z(:)); +I = uint8( 200 * (Z-Imin) / (Imax-Imin) ); + +%% Send data to the 3D viewer, through miji +% Launch Miji +Miji(false); + +%% +% We create an ImagePlus from the 2D image. +imp = MIJ.createImage('MATLAB peaks', I, false); + +%% +% Create and display a new 3D universe. +universe = ij3d.Image3DUniverse(); +universe.show(); + +%% +% Feed it the previous ImagePlus, but render it as a surface plot, where +% the intensity is encoded as height in a 3D space. +color = javax.vecmath.Color3f(rand, rand, rand); +c = universe.addSurfacePlot(imp, ... + color, ... + 'Peaks in 3D', ... + 1, ... + [true true true], ... + 1); + +%% +% Et voilà! A beautiful monochrome MATLAB logo, rendered in an accelerated +% 3D viewer. You can try the _Fullscreen_ option in the _View_ menu, to +% maximize your experience. +% +%% +% +% <> +% +%% +% +% _Jean-Yves Tinevez \ - August 2011_ + \ No newline at end of file diff --git a/scripts/Matlab3DViewerIntroduction.m b/scripts/Matlab3DViewerIntroduction.m index e461ffd..82068d3 100644 --- a/scripts/Matlab3DViewerIntroduction.m +++ b/scripts/Matlab3DViewerIntroduction.m @@ -110,6 +110,9 @@ % data set in MATLAB and render it in 3D (volumetric rendering); % * shows how to create % simple object volumes in MATLAB and make a surface rendering of them. +% * demonstrates the +% surface plot mode. A 2D image is used to generate a 3D surface, where +% pixel intensity is interpreted as elevation. % % *MIJ* is the low level Java class that contains static methods to % exchange data between MATLAB and ImageJ. You can find its documentation @@ -138,6 +141,9 @@ % * 26 Jul 2011: Initial release % * 4 Aug 2011: Fix a bug in write permission detection on Windows % platform, thanks to report by Peter Beemiller +% * 11 Aug 2011: Added a 3rd demo, demonstrating the surface plot of 2D +% image, thanks to the fix on the 3D viewer applied today by Johannes +% Schindelin. % % %% From a861e33682650706866a2479cc716e5b1627213b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 11 Aug 2011 14:59:55 +0200 Subject: [PATCH 16/36] Rotate the Matlab Peak and give it a similar color Apparently, the light was bluish, dunno whether we can do that, too :O) Signed-off-by: Johannes Schindelin --- scripts/Matlab3DViewerDemo_3.m | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m index 8f0927b..e6f815a 100644 --- a/scripts/Matlab3DViewerDemo_3.m +++ b/scripts/Matlab3DViewerDemo_3.m @@ -53,14 +53,23 @@ %% % Feed it the previous ImagePlus, but render it as a surface plot, where % the intensity is encoded as height in a 3D space. -color = javax.vecmath.Color3f(rand, rand, rand); +color = javax.vecmath.Color3f(240 / 255, 120 / 255, 20 / 255); c = universe.addSurfacePlot(imp, ... - color, ... - 'Peaks in 3D', ... + javax.vecmath.Color3f(), ... + 'Matlab Peak in 3D', ... 1, ... [true true true], ... 1); - + +%% +% Rotate it a little +universe.resetView(); +c.setColor(color); +c.setTransform([1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1]); +universe.fireContentChanged(c); +universe.centerSelected(c); +universe.rotateUniverse(javax.vecmath.Vector3d(-1, -0.5, +0.2), +120 * pi / 180); + %% % Et voilà! A beautiful monochrome MATLAB logo, rendered in an accelerated % 3D viewer. You can try the _Fullscreen_ option in the _View_ menu, to From fa95d1fd3b3ba67c609f31300652dbbff8809275 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Sat, 13 Aug 2011 09:02:48 +0200 Subject: [PATCH 17/36] Miji: added comments on Johannes comments Signed-off-by: Jean-Yves Tinevez --- scripts/Matlab3DViewerDemo_3.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m index e6f815a..f60ab05 100644 --- a/scripts/Matlab3DViewerDemo_3.m +++ b/scripts/Matlab3DViewerDemo_3.m @@ -19,7 +19,7 @@ end %% Generate elevation data in MATLAB -% This is the well known *peaks* dataset, that serves as a generator for +% This is the well known *membrane* dataset, that serves as a generator for % the MATLAB logo. %% @@ -62,7 +62,8 @@ 1); %% -% Rotate it a little +% Rotate it a little, so that it shows the same orientation that of the +% actual MATLAB logo. universe.resetView(); c.setColor(color); c.setTransform([1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1]); @@ -76,10 +77,20 @@ % maximize your experience. % %% +% Note that it is monochrome: the MATLAB logo (type |logo| in the command +% window) has two colors: the close side is yellow-orange-ish and the back +% face is blueish. If you look at the |logo.m| code, you will see that +% MATLAB guys generated these colors using 2 different light source of 2 +% different colors, which you cannot do in the 3D viewer. +%% % % <> % %% % -% _Jean-Yves Tinevez \ - August 2011_ +% _Jean-Yves Tinevez \_ +% +% _Johannes Schindelin \_ +% +% _- August 2011_ \ No newline at end of file From 25e4515a4deccb49c84036f8e4a9691718bfeb83 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 17 Aug 2011 16:26:31 +0200 Subject: [PATCH 18/36] UTF-8ify Matlab scripts Signed-off-by: Johannes Schindelin --- scripts/Matlab3DViewerDemo_1.m | 2 +- scripts/Matlab3DViewerDemo_3.m | 2 +- scripts/Matlab3DViewerIntroduction.m | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_1.m b/scripts/Matlab3DViewerDemo_1.m index 2bcb6c8..9dc9a4d 100644 --- a/scripts/Matlab3DViewerDemo_1.m +++ b/scripts/Matlab3DViewerDemo_1.m @@ -118,7 +118,7 @@ % rendering. c = universe.addVoltex(imp); %% -% Et voilà! +% Et voilà! %% % % <> diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m index f60ab05..cf1a010 100644 --- a/scripts/Matlab3DViewerDemo_3.m +++ b/scripts/Matlab3DViewerDemo_3.m @@ -72,7 +72,7 @@ universe.rotateUniverse(javax.vecmath.Vector3d(-1, -0.5, +0.2), +120 * pi / 180); %% -% Et voilà! A beautiful monochrome MATLAB logo, rendered in an accelerated +% Et voilà! A beautiful monochrome MATLAB logo, rendered in an accelerated % 3D viewer. You can try the _Fullscreen_ option in the _View_ menu, to % maximize your experience. % diff --git a/scripts/Matlab3DViewerIntroduction.m b/scripts/Matlab3DViewerIntroduction.m index 82068d3..f57db06 100644 --- a/scripts/Matlab3DViewerIntroduction.m +++ b/scripts/Matlab3DViewerIntroduction.m @@ -130,7 +130,7 @@ % * The ImageJ 3D viewer is written by Benjamin Schmid, Mark Longair, % Albert Cardona and Johannes Schindelin; % * MIJ is written by Daniel Sage and Dimiter Prodanov; -% * Miji is written by Johannes Schindelin, Jacques Pécréaux and Jean-Yves +% * Miji is written by Johannes Schindelin, Jacques Pécréaux and Jean-Yves % Tinevez. % % It's all open source, feel free to modify, distribute, comment or From 4b943bdd82d9c9a58616612cc5cbd2a6ddd61fae Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Sep 2011 19:18:19 -0500 Subject: [PATCH 19/36] Miji: initialize all the plugins and scripts correctly ... also if the user asked the main window to be hidden... Noticed and reported by Walter de Back. Signed-off-by: Johannes Schindelin --- scripts/Miji.m | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index ab902cc..f72dacc 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -23,12 +23,22 @@ function Miji(open_imagej) % Switch warning back to initial settings warning(warning_state) + % Set the Fiji directory (and plugins.dir which is not Fiji.app/plugins/) + java.lang.System.setProperty('fiji.dir', fiji_directory); + java.lang.System.setProperty('plugins.dir', fiji_directory); + %% Maybe open the ImageJ window if open_imagej - cd ..; - fprintf('\n\nUse MIJ.exit to end the session\n\n'); - MIJ.start(fiji_directory); + cd ..; + fprintf('\n\nUse MIJ.exit to end the session\n\n'); + MIJ.start(); + else + % initialize ImageJ with the NO_SHOW flag (== 2) + ij.ImageJ([], 2); end + + % make sure that the scripts are found + fiji.User_Plugins.installScripts(); end function add_to_classpath(classpath, directory) From cb9401a72d636019d84ddfdc1b3f97d05fed521e Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Sun, 6 Nov 2011 11:01:56 +0100 Subject: [PATCH 20/36] Miji: fix a bug in MATLAB java class path In Miji.m, there it a call to fiji.User_Plugins.installScripts(); Unfortunately, this causes a nasty bug with MATLAB: calling this static method modifies the static MATLAB java path, which is normally forbidden. The consequences of that are nasty: adding a class to the dynamic class path can be refused, because it would be falsy recorded in the static path. On top of that, the static path is fsck in a weird way, with file separator from Unix, causing a mess on Windows platform. So we give it up as now. Signed-off-by: Jean-Yves Tinevez --- scripts/Miji.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index f72dacc..88d0350 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,6 +1,7 @@ function Miji(open_imagej) %% This script sets up the classpath to Fiji and optionally starts MIJ - %% Author: Jacques Pecreaux & Johannes Schindelin + % Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez + if nargin < 1 open_imagej = true; end @@ -37,8 +38,16 @@ function Miji(open_imagej) ij.ImageJ([], 2); end - % make sure that the scripts are found - fiji.User_Plugins.installScripts(); + % Make sure that the scripts are found. + % Unfortunately, this causes a nasty bug with MATLAB: calling this + % static method modifies the static MATLAB java path, which is + % normally forbidden. The consequences of that are nasty: adding a + % class to the dynamic class path can be refused, because it would be + % falsy recorded in the static path. On top of that, the static + % path is fsck in a weird way, with file separator from Unix, causing a + % mess on Windows platform. + % So we give it up as now. + % % fiji.User_Plugins.installScripts(); end function add_to_classpath(classpath, directory) From ee880c98d1fc32ea6b249826652019c4c47c94cc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 13 Dec 2011 19:31:47 +0100 Subject: [PATCH 21/36] Let's call it ij.executable and ij.dir In the same vein as the previous commit... Signed-off-by: Johannes Schindelin --- scripts/Miji.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index 88d0350..a3552b7 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -25,7 +25,7 @@ function Miji(open_imagej) warning(warning_state) % Set the Fiji directory (and plugins.dir which is not Fiji.app/plugins/) - java.lang.System.setProperty('fiji.dir', fiji_directory); + java.lang.System.setProperty('ij.dir', fiji_directory); java.lang.System.setProperty('plugins.dir', fiji_directory); %% Maybe open the ImageJ window From 1d7fe8f53542157fbb2a4ccd11495fc370e7e480 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Mon, 8 Apr 2013 15:09:31 +0200 Subject: [PATCH 22/36] copytoImg & copytoImgPlus: MATLAB functions to copy arrays to ImgLib2 Interoperability for the win. Signed-off-by: Jean-Yves Tinevez --- scripts/copytoImg.m | 86 +++++++++++++++++++++++++ scripts/copytoImgPlus.m | 138 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 scripts/copytoImg.m create mode 100644 scripts/copytoImgPlus.m diff --git a/scripts/copytoImg.m b/scripts/copytoImg.m new file mode 100644 index 0000000..a198b23 --- /dev/null +++ b/scripts/copytoImg.m @@ -0,0 +1,86 @@ +function img = copytoImg(I) +%%COPYTOIMG Create a new ImgLib2 Img from a MATLAB image. +% +% img = copytoImg(I) returns a new ImgLib2 Img object, built from the +% specified MATLAB array, with the right type determined at construction. +% The target image has its X ans Y axis permuted so that both images have +% the same orientation in MATLAB and ImgLib2. +% +% This function supports all MATLAB primitive types but uint64 (unsigned +% long integers). +% +% We rely on Miji to set up classpath, so you would have to add Miji to +% your MATLAB path and call +% >> Miji(false); % or true +% prior to using this function. +% +% EXAMPLE +% +% Miji(false) +% load durer +% img = copytoImg(X); +% net.imglib2.img.display.imagej.ImageJFunctions.show(img); +% +% see also: copytoImgPlus +% Jean-Yves Tinevez - 2013 + +% Permute dim 0 and 1 (X & Y) so that we match MATLAB convention in ImgLib +I = permute(I, [2 1]); + +% Copy to ImgLib2 +try + + switch class(I) + + case 'uint8' + img = net.imglib2.img.array.ArrayImgs.unsignedBytes(I(:), size(I)); + + case 'int8' + img = net.imglib2.img.array.ArrayImgs.bytes(I(:), size(I)); + + case 'uint16' + img = net.imglib2.img.array.ArrayImgs.unsignedShorts(I(:), size(I)); + + case 'int16' + img = net.imglib2.img.array.ArrayImgs.shorts(I(:), size(I)); + + case 'uint32' + img = net.imglib2.img.array.ArrayImgs.unsignedInts(I(:), size(I)); + + case 'int32' + img = net.imglib2.img.array.ArrayImgs.ints(I(:), size(I)); + + case 'uint64' + error('MATLAB:copytoImg:UnsupportedType', ... + 'uint64 is not supported by Imglib2.'); + + case 'int64' + img = net.imglib2.img.array.ArrayImgs.shorts(I(:), size(I)); + + case 'single' + img = net.imglib2.img.array.ArrayImgs.floats(I(:), size(I)); + + case 'double' + img = net.imglib2.img.array.ArrayImgs.doubles(I(:), size(I)); + + case 'logical' + img = net.imglib2.img.array.ArrayImgs.bits(I(:), size(I)); + + otherwise + error('MATLAB:copytoImg:UnsupportedType', ... + '%s is not supported by Imglib2.', class(I)); + + end + +catch merr + + if strcmp(merr.identifier, 'MATLAB:undefinedVarOrClass') + error('MATLAB:copytoImg:undefinedVarOrClass', ... + 'Could not find ImgLib2 on the path. Did you forget to run ''Miji(false)'' before calling this function?'); + else + rethrow(merr); + end + +end + +end \ No newline at end of file diff --git a/scripts/copytoImgPlus.m b/scripts/copytoImgPlus.m new file mode 100644 index 0000000..d4b34fb --- /dev/null +++ b/scripts/copytoImgPlus.m @@ -0,0 +1,138 @@ +function imgplus = copytoImgPlus(I, calibration, name, axes) +%%COPYTOIMGPLUS Create a new ImgLib2 ImgPlus from a MATLAB image. +% +% Returns a new ImgLib2 ImgPlus object, which is basically an Img with some +% metadata added. This method allows specifying the spatial calibration, +% the target image name in ImageJ, and the desired axes type. Because of +% metadata limitation in ImageJ, only 3 dimensions are allowed, and they +% must be taken from X, Y, Z, T and C. +% +% img = copytoImgPlus(I) returns a new ImgPlus built from the specified +% MATLAB array, with the right type determined at construction. The target +% image has its X ans Y axis permuted so that both images have the same +% orientation in MATLAB and ImgLib2. The default spatial calibration is +% taken to 1 pixel in all dimensions. The default name is set to be +% 'matlab' and the default axes are tken from X, Y, Z, T, C in this order. +% +% img = copytoImgPlus(I, calibration) allows specifying the spatial +% calibration for each dimension of the source array. It must be a double +% array of ndims elements where ndims is the number of dimensions of the +% array I. +% +% img = copytoImgPlus(I, calibration, name) allows specifying the name of +% the ImgPlus image. +% +% img = copytoImgPlus(I, calibration, name, axes) allows specifying axes +% types for the target multidimensional image. They must be passed as a +% cell array of strings taken from { 'X', 'Y', 'Z', 'T', 'C' }. +% +% This function supports all MATLAB primitive types but uint64 (unsigned +% long integers). +% +% We rely on Miji to set up classpath, so you would have to add Miji to +% your MATLAB path and call +% >> Miji(false); % or true +% prior to using this function. +% +% EXAMPLE +% +% Miji(false) +% load durer +% imgplus = copytoImgPlus(X, [0.2 0.7], 'Dürer', { 'Y' , 'X' }); +% net.imglib2.img.display.imagej.ImageJFunctions.show(imgplus); +% +% see also: copytoImg +% Jean-Yves Tinevez - 2013 + + +%% CONTANTS + +DEFAULT_AXES = { 'X', 'Y', 'Z', 'T', 'C' }; + +try + MATCHING_AXES = [ + net.imglib2.meta.Axes.X + net.imglib2.meta.Axes.Y + net.imglib2.meta.Axes.Z + net.imglib2.meta.Axes.TIME + net.imglib2.meta.Axes.CHANNEL ]; +catch merr + if strcmp(merr.identifier, 'MATLAB:undefinedVarOrClass') + error('MATLAB:copytoImgPlus:undefinedVarOrClass', ... + 'Could not find ImgLib2 on the path. Did you forget to run ''Miji(false)'' before calling this function?'); + else + rethrow(merr); + end +end +DEFAULT_NAME = 'matlab'; + +%% Check input + +if ndims(I) > 5 + error('MATLAB:copytoImgPlus:TooManyDimensions', ... + 'Cannot deal with more that 5 dims, got %d.', ndims(I)) +end + + +if nargin < 2 + calibration = ones( ndims(I), 1); +else + if ~isfloat(calibration) + error('MATLAB:copytoImgPlus:IllegalArgument', ... + 'Second argument, calibration, must be a double array. Got a %s.', ... + class(calibration)) + end + if numel(calibration) ~= ndims(I) + error('MATLAB:copytoImgPlus:IllegalArgument', ... + 'Second argument, calibration, must must have one element per dimension of I. Got %d elements.', ... + numel(calibration)) + end + +end + +if nargin < 3 + name = DEFAULT_NAME; +else + if ~ischar(name) + error('MATLAB:copytoImgPlus:IllegalArgument', ... + 'Third argument, name, must be a string. Got a %s.', ... + class(name)) + end +end + +if nargin < 4 + axes = DEFAULT_AXES(1 : ndims(I)); +else + if ~iscell(axes) + error('MATLAB:copytoImgPlus:IllegalArgument', ... + 'Fourth argument, axes, must be a cell array of strings. Got a %s.', ... + class(axes)) + end + if numel(axes) ~= ndims(I) + error('MATLAB:copytoImgPlus:IllegalArgument', ... + 'Fourth argument, axes, must must have one element per dimension of I. Got %d elements.', ... + numel(axes)) + end +end + +%% Deal with metadata + +ax(ndims(I)) = MATCHING_AXES(1); +for i = 1 : ndims(I) + index = find(strcmp(DEFAULT_AXES, axes{i}), 1); + if isempty(index) || index < 0 + error('MATLAB:copytoImgPlus:IllegalArgument', ... + 'Unkown axis type: %s. Can only deal with X, Y, Z, T & C.', axes{i}) + end + + ax(i) = MATCHING_AXES(index); + +end + +%% Generate ImgLib2 img + +img = copytoImg(I); + +imgplus = net.imglib2.img.ImgPlus(img, name, ax, calibration); + +end \ No newline at end of file From 6aa0024b1722abf9a60427044ee5bcacb11afd25 Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Tue, 9 Apr 2013 16:27:46 +0200 Subject: [PATCH 23/36] copytoMatlab: MATLAB function to retrieve the content of an ImgLib2 ArrayImg Signed-off-by: Jean-Yves Tinevez --- scripts/copytoImg.m | 2 +- scripts/copytoImgPlus.m | 2 +- scripts/copytoMatlab.m | 81 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 scripts/copytoMatlab.m diff --git a/scripts/copytoImg.m b/scripts/copytoImg.m index a198b23..0400006 100644 --- a/scripts/copytoImg.m +++ b/scripts/copytoImg.m @@ -21,7 +21,7 @@ % img = copytoImg(X); % net.imglib2.img.display.imagej.ImageJFunctions.show(img); % -% see also: copytoImgPlus +% see also: copytoImgPlus, copytoMatlab % Jean-Yves Tinevez - 2013 % Permute dim 0 and 1 (X & Y) so that we match MATLAB convention in ImgLib diff --git a/scripts/copytoImgPlus.m b/scripts/copytoImgPlus.m index d4b34fb..0a9cf94 100644 --- a/scripts/copytoImgPlus.m +++ b/scripts/copytoImgPlus.m @@ -41,7 +41,7 @@ % imgplus = copytoImgPlus(X, [0.2 0.7], 'Dürer', { 'Y' , 'X' }); % net.imglib2.img.display.imagej.ImageJFunctions.show(imgplus); % -% see also: copytoImg +% see also: copytoImg, copytoMatlab % Jean-Yves Tinevez - 2013 diff --git a/scripts/copytoMatlab.m b/scripts/copytoMatlab.m new file mode 100644 index 0000000..3235acd --- /dev/null +++ b/scripts/copytoMatlab.m @@ -0,0 +1,81 @@ +function I = copytoMatlab(img) +%%COPYTOMATLAB Copy the content of an ImgLib2 image to MATLAB. +% +% I = copytoMatlab(img) returns a MATLAB copy of the array stored in the +% specified ImgLib2 Img object. This function only works for ImgLib2 images +% that are ArrayImgs, and whose types are native real or integer ones. +% +% We rely on Miji to set up classpath, so you would have to add Miji to +% your MATLAB path and call +% >> Miji(false); % or true +% prior to using this function. +% +% see also: copytoImgPlus, copytoImg +% Jean-Yves Tinevez - 2013 + + %% CONSTANTS + + ACCEPTED_TYPES = { + 'net.imglib2.type.numeric.integer.UnsignedByteType' + 'net.imglib2.type.numeric.integer.UnsignedShortType' + 'net.imglib2.type.numeric.integer.UnsignedIntType' + 'net.imglib2.type.numeric.integer.ByteType' + 'net.imglib2.type.numeric.integer.ShortType' + 'net.imglib2.type.numeric.integer.IntType' + 'net.imglib2.type.numeric.integer.LongType' + 'net.imglib2.type.numeric.integer.LongType' + 'net.imglib2.type.numeric.real.FloatType' + 'net.imglib2.type.numeric.real.DoubleType' + }; + + %% Check input + + if ~isa(img, 'net.imglib2.img.array.ArrayImg') + error('MATLAB:copytoMatlab:IllegalArgument', ... + 'Expected argument to be an ImgLib2 ArrayImg, got a %s.', ... + class(img) ) + end + + fel = img.firstElement; + knowType = false; + for i = 1 : numel(ACCEPTED_TYPES) + if isa(fel, ACCEPTED_TYPES{i}) + knowType = true; + break + end + end + + if ~knowType + error('MATLAB:copytoMatlab:IllegalArgument', ... + 'Can only deal with native real or integer types, got a %s.', ... + class(fel) ) + end + + + + %% Operate on source image + + % Retrieve dimensions + numDims = img.numDimensions(); + sizes = NaN(1, numDims); + for i = 1 : numDims + sizes(i) = img.dimension(i-1); + end + + % Retrieve array container + J = img.update([]).getCurrentStorageArray; + + % Deal with unsigned types + if isa(fel, 'net.imglib2.type.numeric.integer.UnsignedByteType') + J = typecast(J, 'uint8'); + elseif isa(fel, 'net.imglib2.type.numeric.integer.UnsignedShortType') + J = typecast(J, 'uint16'); + elseif isa(fel, 'net.imglib2.type.numeric.integer.UnsignedIntType') + J = typecast(J, 'uint32'); + end + + % Build MATLAB array + I = reshape(J, sizes); + I = permute(I, [2 1]); + +end \ No newline at end of file From d0b947f37ac9831ea64397755088f9a11768a408 Mon Sep 17 00:00:00 2001 From: Mark Hiner Date: Fri, 18 Jul 2014 09:37:35 -0500 Subject: [PATCH 24/36] Miji.m: add jars in subdirectories The Miji.m script adds Fiji.app/jars and Fiji.app/plugins to the MATLAB classpath by explicitly iterating over *.jar in these directories. Thus it was not recursing to subdirectories - notably /jars/bio-formats. The script will now recurse through subdirectories to find jars. Adapted from: http://stackoverflow.com/questions/2652630/ how-to-get-all-files-under-a-specific-directory-in-matlab --- scripts/Miji.m | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index a3552b7..2791029 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -52,11 +52,14 @@ function Miji(open_imagej) function add_to_classpath(classpath, directory) % Get all .jar files in the directory - test = dir(strcat([directory filesep '*.jar'])); + dirData = dir(directory); + dirIndex = [dirData.isdir]; + jarlist = dir(fullfile(directory,'*.jar')); path_= cell(0); - for i = 1:length(test) - if not_yet_in_classpath(classpath, test(i).name) - path_{length(path_) + 1} = strcat([directory filesep test(i).name]); + for i = 1:length(jarlist) + disp(jarlist(i).name); + if not_yet_in_classpath(classpath, jarlist(i).name) + path_{length(path_) + 1} = fullfile(directory,jarlist(i).name); end end @@ -64,6 +67,15 @@ function add_to_classpath(classpath, directory) if ~isempty(path_) javaaddpath(path_, '-end'); end + + %# Recurse over subdirectories + subDirs = {dirData(dirIndex).name}; + validIndex = ~ismember(subDirs,{'.','..'}); + + for iDir = find(validIndex) + nextDir = fullfile(directory,subDirs{iDir}); + add_to_classpath(classpath, nextDir); + end end function test = not_yet_in_classpath(classpath, filename) From 2dc8fda1da1351eb346d0ed80956bd3fc9af1d35 Mon Sep 17 00:00:00 2001 From: Mark Hiner Date: Fri, 18 Jul 2014 09:40:02 -0500 Subject: [PATCH 25/36] Miji.m convert strcat to fullfile calls Instead of manually adding filesep let's let MATLAB decide how to make a full file path. --- scripts/Miji.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index 2791029..c08b725 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -18,8 +18,8 @@ function Miji(open_imagej) % Switch off warning warning_state = warning('off'); - add_to_classpath(classpath, strcat([fiji_directory filesep 'jars'])); - add_to_classpath(classpath, strcat([fiji_directory filesep 'plugins'])); + add_to_classpath(classpath, fullfile(fiji_directory,'jars')); + add_to_classpath(classpath, fullfile(fiji_directory,'plugins')); % Switch warning back to initial settings warning(warning_state) From d8bc3a9c49dd7afbc75893488a8a8eeae820b511 Mon Sep 17 00:00:00 2001 From: Eric Barnhill Date: Thu, 28 May 2015 20:48:16 +0100 Subject: [PATCH 26/36] Update Miji.m This proposed change removes a bug I described on imagej-dev . Any other use of javaaddpath, say to develop one's other software, loses the instance of MIJ. This is because javaaddpath automatically calls clear java. By returning the MIJ instance as a workspace object, the MIJ instance will not be cleared. The only change for the user is to call: MIJ = Miji; instead of just Miji; Signed-off by: Mark Hiner --- scripts/Miji.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index c08b725..54a93fb 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,4 +1,4 @@ -function Miji(open_imagej) +function [mij] = Miji(open_imagej) %% This script sets up the classpath to Fiji and optionally starts MIJ % Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez @@ -32,7 +32,9 @@ function Miji(open_imagej) if open_imagej cd ..; fprintf('\n\nUse MIJ.exit to end the session\n\n'); - MIJ.start(); + % EB change May 2015 + mij = MIJ; + mij.start(); else % initialize ImageJ with the NO_SHOW flag (== 2) ij.ImageJ([], 2); From 6211ff883688ccfca8365c83e5ba2a18d5cc007c Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Thu, 17 Dec 2015 23:55:32 -0600 Subject: [PATCH 27/36] Use the SciJava fork of Java 3D 1.6 This changes all relevant scripts from javax.vecmath to org.scijava.vecmath, since we now ship vecmath as a standard dependency! --- scripts/Matlab3DViewerDemo_2.m | 6 +++--- scripts/Matlab3DViewerDemo_3.m | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_2.m b/scripts/Matlab3DViewerDemo_2.m index 75f609e..f853771 100644 --- a/scripts/Matlab3DViewerDemo_2.m +++ b/scripts/Matlab3DViewerDemo_2.m @@ -85,9 +85,9 @@ %% % It is possible to specify the color of the iso-surface at creation, % but the 3D viewer expects a |Color3f| object, which is part of the - % |javax.vecmath package|. We determine its actual color randomly - % again. - color = javax.vecmath.Color3f(rand, rand, rand); + % |org.scijava.vecmath package|. We determine its actual color + % randomly again. + color = org.scijava.vecmath.Color3f(rand, rand, rand); %% % Finally, we add the object to the 3D universe, in the shape of an diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m index cf1a010..83f31ad 100644 --- a/scripts/Matlab3DViewerDemo_3.m +++ b/scripts/Matlab3DViewerDemo_3.m @@ -53,9 +53,9 @@ %% % Feed it the previous ImagePlus, but render it as a surface plot, where % the intensity is encoded as height in a 3D space. -color = javax.vecmath.Color3f(240 / 255, 120 / 255, 20 / 255); +color = org.scijava.vecmath.Color3f(240 / 255, 120 / 255, 20 / 255); c = universe.addSurfacePlot(imp, ... - javax.vecmath.Color3f(), ... + org.scijava.vecmath.Color3f(), ... 'Matlab Peak in 3D', ... 1, ... [true true true], ... @@ -69,7 +69,7 @@ c.setTransform([1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1]); universe.fireContentChanged(c); universe.centerSelected(c); -universe.rotateUniverse(javax.vecmath.Vector3d(-1, -0.5, +0.2), +120 * pi / 180); +universe.rotateUniverse(org.scijava.vecmath.Vector3d(-1, -0.5, +0.2), +120 * pi / 180); %% % Et voilà! A beautiful monochrome MATLAB logo, rendered in an accelerated From b3f124ed1e2de64e232eb27e30b355622220a38e Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Thu, 17 Dec 2015 23:55:16 -0600 Subject: [PATCH 28/36] Matlab3DViewerDemo_3.m: add missing EOF newline --- scripts/Matlab3DViewerDemo_3.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m index 83f31ad..f0b3d7b 100644 --- a/scripts/Matlab3DViewerDemo_3.m +++ b/scripts/Matlab3DViewerDemo_3.m @@ -93,4 +93,4 @@ % _Johannes Schindelin \_ % % _- August 2011_ - \ No newline at end of file + From 8a30031e3e107a55ed843a0c23703673e61d3133 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Thu, 25 Feb 2016 15:39:53 -0600 Subject: [PATCH 29/36] Remove Java 3D auto-installation scripts Java 3D 1.6 now ships with Fiji. It does not need to be (and cannot be!) installed separately. --- scripts/Matlab3DViewerDemo_1.m | 7 ------- scripts/Matlab3DViewerDemo_2.m | 7 ------- scripts/Matlab3DViewerDemo_3.m | 8 -------- 3 files changed, 22 deletions(-) diff --git a/scripts/Matlab3DViewerDemo_1.m b/scripts/Matlab3DViewerDemo_1.m index 9dc9a4d..9f5c914 100644 --- a/scripts/Matlab3DViewerDemo_1.m +++ b/scripts/Matlab3DViewerDemo_1.m @@ -5,13 +5,6 @@ % computers are, these days). We use the well known 'mri' data distributed % with MATLAB, and render it in the ImageJ 3D viewer. -%% Make sure Java3D is installed -% If not, try to install it - -if ~IsJava3DInstalled(true) - return -end - %% Load and prepare data using MATLAB % Load an example of 3D data from standard Matlab load('mri.mat'); diff --git a/scripts/Matlab3DViewerDemo_2.m b/scripts/Matlab3DViewerDemo_2.m index f853771..f3d1ee8 100644 --- a/scripts/Matlab3DViewerDemo_2.m +++ b/scripts/Matlab3DViewerDemo_2.m @@ -10,13 +10,6 @@ % viewer to create an iso-surface (one for each object), and to display it % in the 3D universe, with a random color. -%% Make sure Java3D is installed -% If not, try to install it - -if ~IsJava3DInstalled(true) - return -end - %% % Let us define the size of our target image volume. Not too big to save % memory. diff --git a/scripts/Matlab3DViewerDemo_3.m b/scripts/Matlab3DViewerDemo_3.m index f0b3d7b..fe4caa5 100644 --- a/scripts/Matlab3DViewerDemo_3.m +++ b/scripts/Matlab3DViewerDemo_3.m @@ -10,14 +10,6 @@ % we try to use the surface plot mode of the 3D viewer with a surface well % known to MATLAB users. - -%% Make sure Java3D is installed -% If not, try to install it - -if ~IsJava3DInstalled(true) - return -end - %% Generate elevation data in MATLAB % This is the well known *membrane* dataset, that serves as a generator for % the MATLAB logo. From dff4ff317aa482db7c78c71362ce07da7cdc5442 Mon Sep 17 00:00:00 2001 From: Eric Barnhill Date: Fri, 8 Jul 2016 10:05:03 +0200 Subject: [PATCH 30/36] Added GNU Octave compatibility to Miji script --- scripts/Miji.m | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index 54a93fb..08fb24c 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,6 +1,7 @@ function [mij] = Miji(open_imagej) %% This script sets up the classpath to Fiji and optionally starts MIJ % Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez + % GNU Octave compatibility added by Eric Barnhill, Jul 2016 if nargin < 1 open_imagej = true; @@ -25,15 +26,14 @@ warning(warning_state) % Set the Fiji directory (and plugins.dir which is not Fiji.app/plugins/) - java.lang.System.setProperty('ij.dir', fiji_directory); - java.lang.System.setProperty('plugins.dir', fiji_directory); + javaMethod('setProperty', 'java.lang.System', 'ij.dir', fiji_directory); + javaMethod('setProperty', 'java.lang.System', 'plugins.dir', fiji_directory); %% Maybe open the ImageJ window if open_imagej cd ..; fprintf('\n\nUse MIJ.exit to end the session\n\n'); - % EB change May 2015 - mij = MIJ; + mij = javaObject('MIJ'); mij.start(); else % initialize ImageJ with the NO_SHOW flag (== 2) @@ -53,13 +53,16 @@ end function add_to_classpath(classpath, directory) + + isoctave = exist('octave_config_info') > 0; + % Get all .jar files in the directory dirData = dir(directory); dirIndex = [dirData.isdir]; jarlist = dir(fullfile(directory,'*.jar')); path_= cell(0); for i = 1:length(jarlist) - disp(jarlist(i).name); + %disp(jarlist(i).name); if not_yet_in_classpath(classpath, jarlist(i).name) path_{length(path_) + 1} = fullfile(directory,jarlist(i).name); end @@ -67,8 +70,17 @@ function add_to_classpath(classpath, directory) %% Add them to the classpath if ~isempty(path_) - javaaddpath(path_, '-end'); - end + if isoctave + for n = 1:numel(path_) + err_code = javaMethod('addClassPath', 'org.octave.ClassHelper', path_{n}); + if err_code == 0 + display(['Error importing ', path_{n}]); + end + end + else + javaaddpath(path_, '-end'); + end + end %# Recurse over subdirectories subDirs = {dirData(dirIndex).name}; @@ -85,3 +97,4 @@ function add_to_classpath(classpath, directory) expression = strcat([filesep filename '$']); test = isempty(cell2mat(regexp(classpath, expression))); end + From f3f48d7336cdb26cb29b25168383eef8d15258b8 Mon Sep 17 00:00:00 2001 From: bastelflp Date: Fri, 27 May 2016 16:48:52 +0200 Subject: [PATCH 31/36] Miji: Add a verbose flag, stay in same directory - Avoid command prompt spam of printing every artifact added to the classpath to the console. Related commit: imagej/imagej-matlab@2633d12c744e0d636cfe698edf4a53c471fd56d4 - Remove `cd ..` to stay in the same directory while loading Miji - Only turn warnings off for: `MATLAB:javaclasspath:jarAlreadySpecified` --- scripts/Miji.m | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/scripts/Miji.m b/scripts/Miji.m index 08fb24c..b07bee0 100644 --- a/scripts/Miji.m +++ b/scripts/Miji.m @@ -1,4 +1,4 @@ -function [mij] = Miji(open_imagej) +function [mij] = Miji(open_imagej, verbose) %% This script sets up the classpath to Fiji and optionally starts MIJ % Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez % GNU Octave compatibility added by Eric Barnhill, Jul 2016 @@ -7,6 +7,9 @@ open_imagej = true; end + if nargin < 2 + verbose = false; + end %% Get the Fiji directory fiji_directory = fileparts(fileparts(mfilename('fullpath'))); @@ -17,10 +20,10 @@ %% Add all libraries in jars/ and plugins/ to the classpath % Switch off warning - warning_state = warning('off'); + warning_state = warning('off','MATLAB:javaclasspath:jarAlreadySpecified'); - add_to_classpath(classpath, fullfile(fiji_directory,'jars')); - add_to_classpath(classpath, fullfile(fiji_directory,'plugins')); + add_to_classpath(classpath, fullfile(fiji_directory,'jars'), verbose); + add_to_classpath(classpath, fullfile(fiji_directory,'plugins'), verbose); % Switch warning back to initial settings warning(warning_state) @@ -31,7 +34,6 @@ %% Maybe open the ImageJ window if open_imagej - cd ..; fprintf('\n\nUse MIJ.exit to end the session\n\n'); mij = javaObject('MIJ'); mij.start(); @@ -52,7 +54,7 @@ % % fiji.User_Plugins.installScripts(); end -function add_to_classpath(classpath, directory) +function add_to_classpath(classpath, directory, verbose) isoctave = exist('octave_config_info') > 0; @@ -62,8 +64,10 @@ function add_to_classpath(classpath, directory) jarlist = dir(fullfile(directory,'*.jar')); path_= cell(0); for i = 1:length(jarlist) - %disp(jarlist(i).name); if not_yet_in_classpath(classpath, jarlist(i).name) + if verbose + disp(strcat(['Adding: ',jarlist(i).name])); + end path_{length(path_) + 1} = fullfile(directory,jarlist(i).name); end end @@ -88,7 +92,7 @@ function add_to_classpath(classpath, directory) for iDir = find(validIndex) nextDir = fullfile(directory,subDirs{iDir}); - add_to_classpath(classpath, nextDir); + add_to_classpath(classpath, nextDir, verbose); end end From d3ab86cce04fc7ef9417a50674b075fce426ac74 Mon Sep 17 00:00:00 2001 From: KOUICHI NAKAMURA Date: Mon, 2 Jul 2018 14:02:01 +0100 Subject: [PATCH 32/36] copytoImgPlus.m --- scripts/copytoImgPlus.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/copytoImgPlus.m b/scripts/copytoImgPlus.m index 0a9cf94..c452dc5 100644 --- a/scripts/copytoImgPlus.m +++ b/scripts/copytoImgPlus.m @@ -51,11 +51,11 @@ try MATCHING_AXES = [ - net.imglib2.meta.Axes.X - net.imglib2.meta.Axes.Y - net.imglib2.meta.Axes.Z - net.imglib2.meta.Axes.TIME - net.imglib2.meta.Axes.CHANNEL ]; + net.imagej.axis.Axes.X + net.imagej.axis.Axes.Y + net.imagej.axis.Axes.Z + net.imagej.axis.Axes.TIME + net.imagej.axis.Axes.CHANNEL ]; catch merr if strcmp(merr.identifier, 'MATLAB:undefinedVarOrClass') error('MATLAB:copytoImgPlus:undefinedVarOrClass', ... @@ -133,6 +133,6 @@ img = copytoImg(I); -imgplus = net.imglib2.img.ImgPlus(img, name, ax, calibration); +imgplus = net.imagej.ImgPlus(img, name, ax, calibration); end \ No newline at end of file From cef0c774df044c1212d5feae961bcdb5ca0a9e89 Mon Sep 17 00:00:00 2001 From: KOUICHI NAKAMURA Date: Mon, 2 Jul 2018 17:24:15 +0100 Subject: [PATCH 33/36] copytoImagePlus.m scripts --- scripts/copytoImagePlus.m | 262 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 scripts/copytoImagePlus.m diff --git a/scripts/copytoImagePlus.m b/scripts/copytoImagePlus.m new file mode 100644 index 0000000..687e95d --- /dev/null +++ b/scripts/copytoImagePlus.m @@ -0,0 +1,262 @@ +function imp = copytoImagePlus(I,varargin) +% copytoImagePlus allows you to open an array I with an instance of ImageJ +% within MATLAB with a proper data type and hyperstack dimensions. +% +% +% SYNTAX +% imp = copytoImagePlus(I) +% imp = copytoImagePlus(I,dimorder) +% imp = copytoImagePlus(____,'Param',value) +% +% +% REQUIREMENTS +% ImageJ-MATLAB as part of Fiji installation +% https://imagej.net/MATLAB_Scripting +% +% ijmshow assumes a net.imagej.matlab.ImageJMATLABCommands Java object +% named 'IJM' is made available in the base Workspace by ImageJ (part of +% ImageJ-MATLAB). +% +% +% INPUT ARGUMENTS +% I uint16 | uint8 | double | single +% An array of integers to be opened with ImageJ. This array can +% have from 2 to 5 dimensions. +% +% +% dimorder char row vector made of 'XYCZT' | 'YXCZT' (default) +% +% (Optional) A char row vector composed of 'X', 'Y', 'C' for +% channels, 'Z' for slices, and 'T' for frames. dimorder is +% case insensitive. You cannot repeat any of the five letters +% in dimorder. The first two letters must be either 'X' or 'Y'. +% The length of dimorder must be 5 or match the number of +% dimensions of the array specified by I. The third to the +% fifth letters must be chosen from 'C', 'Z', and 'T'. +% +% The default is set 'YXCZT' rather than 'XYZCT', because the X +% and Y axes of an MATLAB array is flipped over in ImageJ by +% IJM.show(). +% +% +% OPTIONAL PARAMETER/VALUE PAIRS +% NewName char row vector | 'new' (default) +% The window title of the new image in ImageJ +% +% FrameInterval +% scalar +% Time frame sampling interval in seconds +% +% OUTPUT ARGUMENTS +% imp ij.ImagePlus Java object +% +% EXAMPLES +% see https://github.com/kouichi-c-nakamura/copytoImagePlus +% +% +% Written by Kouichi C. Nakamura Ph.D. +% MRC Brain Network Dynamics Unit +% University of Oxford +% kouichi.c.nakamura@gmail.com +% 03-May-2018 04:57:24 +% +% See also +% ijmshow (this requires a net.imagej.matlab.ImageJMATLABCommands object IJM) +% https://github.com/kouichi-c-nakamura/ijmshow (repository for this function) +% +% ImageJ as part of ImageJ-MATLAB (https://github.com/imagej/imagej-matlab/) +% +% net.imagej.matlab.ImageJMATLABCommands +% evalin, assignin +% https://imagej.net/MATLAB_Scripting + + +import ij.process.ShortProcessor +import ij.process.ByteProcessor +import ij.process.FloatProcessor + +p = inputParser; +p.addRequired('I',@(x) isnumeric(x)); +p.addOptional('dimorder','YXCZT',@(x) ischar(x) && isrow(x) ... + && all(arrayfun(@(y) ismember(y,'XYCZT'),upper(x))) && length(x) >=2 ... + && all(arrayfun(@(y) ismember(y,'XY'),upper(x(1:2))))... + ); +p.addParameter('NewName','new',@(x) ischar(x) && isrow(x)); +p.addParameter('FrameInterval',[],@(x) isreal(x) && x > 0); + +p.parse(I,varargin{:}); + +dimorder = upper(p.Results.dimorder); +newname = p.Results.NewName; +frameinterval = p.Results.FrameInterval; + + + +switch dimorder(1:2) + case 'XY' + order1 = [1 2]; + case 'YX' + order1 = [2 1]; +end + + +switch dimorder(3:ndims(I)) + case 'CZT' + order2 = 3:5; + case 'CTZ' + order2 = [3 5 4]; + case 'ZCT' + order2 = [4 3 5]; + case 'ZTC' + order2 = [4 5 3]; + case 'TCZ' + order2 = [5 3 4]; + case 'TZC' + order2 = [5 4 3]; + case 'CZ' + order2 = [3 4]; + case 'CT' + order2 = [3 5 4]; + case 'ZC' + order2 = [4 3]; + case 'ZT' + order2 = [5 3 4]; + case 'TC' + order2 = [4 5 3]; + case 'TZ' + order2 = [5 4 3]; + case 'C' + order2 = [3 4 5]; + case 'Z' + order2 = [4 3 5]; + case 'T' + order2 = [4 5 3]; + otherwise + order2 = 3:5; +end + + +I0 = permute(I, [order1, order2]); + +nX = int32(size(I0,1)); +nY = int32(size(I0,2)); +nC = int32(size(I0,3)); +nZ = int32(size(I0,4)); +nT = int32(size(I0,5)); + +try + switch class(I0) + case 'uint8' + bitdepth = 8; + + case 'int8' + + bitdepth = 8; + + case 'uint16' + + bitdepth = 16; + + case 'int16' + + bitdepth = 8; + + case 'uint32' + + bitdepth = 32; + + case 'int32' + + bitdepth = 32; + + case 'uint64' + error('MATLAB:copytoImg:UnsupportedType', ... + 'uint64 is not supported.'); + case 'int64' + error('MATLAB:copytoImg:UnsupportedType', ... + 'uint64 is not supported.'); + case 'single' + + bitdepth = 32; + + case 'double' + + bitdepth = 32; + + case 'logical' + + bitdepth = 8; + + otherwise + error('MATLAB:copytoImg:UnsupportedType', ... + '%s is not supported.', class(I0)); + + end + +catch merr + if strcmp(merr.identifier, 'MATLAB:undefinedVarOrClass') + error('MATLAB:copytoImg:undefinedVarOrClass', ... + 'Could not find ImgLib2 on the path. Did you forget to run ''Miji(false)'' before calling this function?'); + else + rethrow(merr); + end +end + + +imp = ij.IJ.createHyperStack(newname,nX,nY,nC,nZ,nT,bitdepth); + +for t = 1:nT + imp.setT(t); + for z = 1:nZ + imp.setZ(z); + for c = 1:nC + imp.setC(c); + + XY = I0(:,:,c,z,t); + xy = XY(:)'; + + switch bitdepth + case 16 + ip = ShortProcessor(nX,nY); + ip.setPixels(xy); + imp.setProcessor(ip); + case 8 + ip = ByteProcessor(nX,nY); + ip.setPixels(xy); + imp.setProcessor(ip); + otherwise + ip = FloatProcessor(nX,nY); + ip.setPixels(single(xy)); + imp.setProcessor(ip); + end + end + end +end + +imp.setT(1); +imp.setZ(1); +imp.setC(1); +%imp.show(); +imp.setDisplayMode(ij.IJ.COLOR) %NOTE this is required to enable the next line +imp.setDisplayMode(ij.IJ.COMPOSITE) + +try + imp.resetDisplayRanges(); +catch mexc + if strcmpi(mexc.identifier,'MATLAB:UndefinedFunction') + warning('resetDisplayRanges did not work') + else + throw(mexc) + end +end + +if ~isempty(frameinterval) + + fi = imp.getFileInfo(); + fi.frameInterval = frameinterval; + imp.setFileInfo(fi); + %TODO Show Info... does not show the frameinterval +end + + +end From b87b3b98fa0c34a1cb7716587716feb741a56323 Mon Sep 17 00:00:00 2001 From: Jan Eglinger Date: Tue, 3 Jul 2018 16:56:21 +0200 Subject: [PATCH 34/36] Move migrated MATLAB scripts to script_templates --- .../resources/script_templates/MATLAB}/Matlab3DViewerDemo_1.m | 0 .../resources/script_templates/MATLAB}/Matlab3DViewerDemo_2.m | 0 .../resources/script_templates/MATLAB}/Matlab3DViewerDemo_3.m | 0 .../script_templates/MATLAB}/Matlab3DViewerIntroduction.m | 0 {scripts => src/main/resources/script_templates/MATLAB}/Miji.m | 0 .../main/resources/script_templates/MATLAB}/Miji_Test.m | 0 .../main/resources/script_templates/MATLAB}/copytoImagePlus.m | 0 .../main/resources/script_templates/MATLAB}/copytoImg.m | 0 .../main/resources/script_templates/MATLAB}/copytoImgPlus.m | 0 .../main/resources/script_templates/MATLAB}/copytoMatlab.m | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename {scripts => src/main/resources/script_templates/MATLAB}/Matlab3DViewerDemo_1.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/Matlab3DViewerDemo_2.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/Matlab3DViewerDemo_3.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/Matlab3DViewerIntroduction.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/Miji.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/Miji_Test.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/copytoImagePlus.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/copytoImg.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/copytoImgPlus.m (100%) rename {scripts => src/main/resources/script_templates/MATLAB}/copytoMatlab.m (100%) diff --git a/scripts/Matlab3DViewerDemo_1.m b/src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_1.m similarity index 100% rename from scripts/Matlab3DViewerDemo_1.m rename to src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_1.m diff --git a/scripts/Matlab3DViewerDemo_2.m b/src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_2.m similarity index 100% rename from scripts/Matlab3DViewerDemo_2.m rename to src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_2.m diff --git a/scripts/Matlab3DViewerDemo_3.m b/src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_3.m similarity index 100% rename from scripts/Matlab3DViewerDemo_3.m rename to src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_3.m diff --git a/scripts/Matlab3DViewerIntroduction.m b/src/main/resources/script_templates/MATLAB/Matlab3DViewerIntroduction.m similarity index 100% rename from scripts/Matlab3DViewerIntroduction.m rename to src/main/resources/script_templates/MATLAB/Matlab3DViewerIntroduction.m diff --git a/scripts/Miji.m b/src/main/resources/script_templates/MATLAB/Miji.m similarity index 100% rename from scripts/Miji.m rename to src/main/resources/script_templates/MATLAB/Miji.m diff --git a/scripts/Miji_Test.m b/src/main/resources/script_templates/MATLAB/Miji_Test.m similarity index 100% rename from scripts/Miji_Test.m rename to src/main/resources/script_templates/MATLAB/Miji_Test.m diff --git a/scripts/copytoImagePlus.m b/src/main/resources/script_templates/MATLAB/copytoImagePlus.m similarity index 100% rename from scripts/copytoImagePlus.m rename to src/main/resources/script_templates/MATLAB/copytoImagePlus.m diff --git a/scripts/copytoImg.m b/src/main/resources/script_templates/MATLAB/copytoImg.m similarity index 100% rename from scripts/copytoImg.m rename to src/main/resources/script_templates/MATLAB/copytoImg.m diff --git a/scripts/copytoImgPlus.m b/src/main/resources/script_templates/MATLAB/copytoImgPlus.m similarity index 100% rename from scripts/copytoImgPlus.m rename to src/main/resources/script_templates/MATLAB/copytoImgPlus.m diff --git a/scripts/copytoMatlab.m b/src/main/resources/script_templates/MATLAB/copytoMatlab.m similarity index 100% rename from scripts/copytoMatlab.m rename to src/main/resources/script_templates/MATLAB/copytoMatlab.m From 41533969aa516a18d76b697f34ba4ed53cc2495b Mon Sep 17 00:00:00 2001 From: KOUICHI NAKAMURA Date: Thu, 20 Sep 2018 19:29:19 +0100 Subject: [PATCH 35/36] ImageJ_javaaddpath.m src\scripts\update ImageJ.m src\scripts\update --- src/scripts/update/ImageJ.m | 130 +++++++-------------- src/scripts/update/ImageJ_javaaddpath.m | 147 ++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 87 deletions(-) create mode 100644 src/scripts/update/ImageJ_javaaddpath.m diff --git a/src/scripts/update/ImageJ.m b/src/scripts/update/ImageJ.m index 0d01eb6..d4a43e6 100644 --- a/src/scripts/update/ImageJ.m +++ b/src/scripts/update/ImageJ.m @@ -1,94 +1,50 @@ function ImageJ(open_imagej, verbose) - %% This script adds the ImageJ libraries to the MATLAB classpath. By default, - %% it will also start up an ImageJ instance within MATLAB. - %% Parameters: - %% open_imagej - If false, an ImageJ instance will not be launched. Default: true - %% verbose - If true, a confirmation message will be printed the first time - %% a jar is added to the MATLAB classpath. Default: false - %% Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez, Mark Hiner - - if nargin < 1 - open_imagej = true; - end - - if nargin < 2 - verbose = false; - end - - %% Get the ImageJ directory - imagej_directory = fileparts(fileparts(mfilename('fullpath'))); - - %% Get the Java classpath - classpath = javaclasspath('-all'); - - %% Add all libraries in jars/ and plugins/ to the classpath - - % Switch off warning - warning_state = warning('off','MATLAB:javaclasspath:jarAlreadySpecified'); - - add_to_classpath(classpath, fullfile(imagej_directory,'jars'), verbose); - add_to_classpath(classpath, fullfile(imagej_directory,'plugins'), verbose); - - % Switch warning back to initial settings - warning(warning_state) - - % Set the ImageJ directory (and plugins.dir which is not ImageJ.app/plugins/) - java.lang.System.setProperty('ij.dir', imagej_directory); - java.lang.System.setProperty('plugins.dir', imagej_directory); - - %% Maybe open the ImageJ window - import net.imagej.matlab.*; - if open_imagej - ImageJMATLAB.start(verbose); - else - % initialize ImageJ with the headless flag - ImageJMATLAB.start(verbose, '--headless'); - end - - % Make sure that the scripts are found. - % Unfortunately, this causes a nasty bug with MATLAB: calling this - % static method modifies the static MATLAB java path, which is - % normally forbidden. The consequences of that are nasty: adding a - % class to the dynamic class path can be refused, because it would be - % falsy recorded in the static path. On top of that, the static - % path is fsck in a weird way, with file separator from Unix, causing a - % mess on Windows platform. - % So we give it up as now. - % % imagej.User_Plugins.installScripts(); +% This script adds the ImageJ libraries to the MATLAB classpath. By default, +% it will also start up an ImageJ instance within MATLAB. +% Parameters: +% open_imagej - If false, an ImageJ instance will not be launched. Default: true +% verbose - If true, a confirmation message will be printed the first time +% a jar is added to the MATLAB classpath. Default: false +% Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez, Mark Hiner + +if nargin < 1 + open_imagej = true; end -function add_to_classpath(classpath, directory, verbose) - % Get all .jar files in the directory - dirData = dir(directory); - dirIndex = [dirData.isdir]; - jarlist = dir(fullfile(directory,'*.jar')); - path_= cell(0); - for i = 1:length(jarlist) - if not_yet_in_classpath(classpath, jarlist(i).name) - if verbose - disp(strcat(['Adding: ',jarlist(i).name])); - end - path_{length(path_) + 1} = fullfile(directory,jarlist(i).name); - end - end - - %% Add them to the classpath - if ~isempty(path_) - javaaddpath(path_, '-end'); - end - - %# Recurse over subdirectories - subDirs = {dirData(dirIndex).name}; - validIndex = ~ismember(subDirs,{'.','..'}); +if nargin < 2 + verbose = false; +end - for iDir = find(validIndex) - nextDir = fullfile(directory,subDirs{iDir}); - add_to_classpath(classpath, nextDir, verbose); - end +%% Get the ImageJ directory +imagej_directory = fileparts(fileparts(mfilename('fullpath'))); + +ImageJ_javaaddpath(imagej_directory,verbose); + +%% Maybe open the ImageJ window +import net.imagej.matlab.*; +if open_imagej + ImageJMATLAB.start(verbose); +else + % initialize ImageJ with the headless flag + % ImageJMATLAB.start(verbose, '--headless'); % this does not work + + % after ImageJ_javaaddpath, you have access to Java API of ImageJ + % Cant't we just call it "headless mode"? + % + % If not, what functionality is the API missing to be called as + % headless? + end -function test = not_yet_in_classpath(classpath, filename) -%% Test whether the library was already imported -expression = strcat([filesep filename '$']); -test = isempty(cell2mat(regexp(classpath, expression))); +% Make sure that the scripts are found. +% Unfortunately, this causes a nasty bug with MATLAB: calling this +% static method modifies the static MATLAB java path, which is +% normally forbidden. The consequences of that are nasty: adding a +% class to the dynamic class path can be refused, because it would be +% falsy recorded in the static path. On top of that, the static +% path is fsck in a weird way, with file separator from Unix, causing a +% mess on Windows platform. +% So we give it up as now. +% % imagej.User_Plugins.installScripts(); end + diff --git a/src/scripts/update/ImageJ_javaaddpath.m b/src/scripts/update/ImageJ_javaaddpath.m new file mode 100644 index 0000000..3a5eca5 --- /dev/null +++ b/src/scripts/update/ImageJ_javaaddpath.m @@ -0,0 +1,147 @@ +function ImageJ_javaaddpath(imagej_directory,varargin) +% ImageJ_javaaddpath will add the paths for ImageJ/Fiji jar files to +% dyanamic Java class path using javaaddpath, without launching ImageJ +% instance. +% +% When you want to use Fiji/ImageJ within parfor loop for +% parallel computation, you need to add Java class paths for Fiji/ImageJ +% within parfor loop in order to change dynamic Java class path for each +% MATLAB worker. +% +% Virtually all of the code is taken from ImageJ.m. +% +% SYNTAX +% ImageJ_javaaddpath(imagej_directory) +% ImageJ_javaaddpath(imagej_directory,verbose) +% +% INPUT ARGUMENTS +% imagej_directory +% char vector +% The directory path for 'Fiji.app'. +% +% Note that you can't include 'scripts' as 'Fiji.app\scripts' +% +% verbose true | false (default) | 1 | 0 +% (Optional) true or 1 will print added java paths to the +% Command Window. +% +% +% EXAMPLE +% +% fijipath = 'D\\Fiji.app\\'; +% +% addpath(fullfile(fijipath,'scripts')) +% ImageJ +% +% I = imread('corn.tif',3); % uint8 +% +% parfor i = 1:10 +% addpath(fullfile(fijipath,'scripts')) % to make MATLAB functions for ImageJ available +% ImageJ_javaaddpath(fijipath) % to make ImageJ Java class available +% +% I1 = I - uint8(10*i); +% imp = copytoImagePlus(I1); +% +% disp(imp) +% +% local_saveasTIFF(imp,i) % you cannot directly access ij.IJ in parfor loop +% end +% +% % a local function is needed to access ij.IJ +% function local_saveasTIFF(imp,i) +% ij.IJ.saveAsTiff(imp,sprintf('testimage%d.tif',i)) +% end +% +% +% Written by Kouichi C. Nakamura Ph.D. +% MRC Brain Network Dynamics Unit +% University of Oxford +% kouichi.c.nakamura@gmail.com +% 14-Aug-2018 10:34:01 +% +% +% See also +% ImageJ, javaaddpath, parfor +% +% %TODO ImageJ.m can wrap this function + + +p = inputParser; +p.addRequired('imagej_directory',@(x) isfolder(x)); +p.addOptional('verbose',false,@(x) isscalar(x) && x == 1 || x == 0); +p.parse(imagej_directory,varargin{:}); + +verbose = p.Results.verbose; + + +if endsWith(imagej_directory,[filesep,'scripts']) + + error('imagej_directory %s should not include ''scripts''',... + imagej_directory) + +end + +% +% %% Get the ImageJ directory +% % imagej_directory = fileparts(fileparts(mfilename('fullpath'))); +% +% imagej_directory = fileparts(FijiScriptFolder); + +%% Get the Java classpath +classpath = javaclasspath('-all'); + +%% Add all libraries in jars/ and plugins/ to the classpath + +% Switch off warning +warning_state = warning('off'); + +add_to_classpath(classpath, fullfile(imagej_directory,'jars'), verbose); +add_to_classpath(classpath, fullfile(imagej_directory,'plugins'), verbose); + +% Switch warning back to initial settings +warning(warning_state) + +% Set the ImageJ directory (and plugins.dir which is not ImageJ.app/plugins/) +java.lang.System.setProperty('ij.dir', imagej_directory); +java.lang.System.setProperty('plugins.dir', imagej_directory); +end + +%------------------------------------------------------------------ + +function add_to_classpath(classpath, directory, verbose) +% Get all .jar files in the directory +dirData = dir(directory); +dirIndex = [dirData.isdir]; +jarlist = dir(fullfile(directory,'*.jar')); +path_= cell(0); +for i = 1:length(jarlist) + if not_yet_in_classpath(classpath, jarlist(i).name) + if verbose + disp(strcat(['Adding: ',jarlist(i).name])); + end + path_{length(path_) + 1} = fullfile(directory,jarlist(i).name); + end +end + +%% Add them to the classpath +if ~isempty(path_) + javaaddpath(path_, '-end'); +end + +%# Recurse over subdirectories +subDirs = {dirData(dirIndex).name}; +validIndex = ~ismember(subDirs,{'.','..'}); + +for iDir = find(validIndex) + nextDir = fullfile(directory,subDirs{iDir}); + add_to_classpath(classpath, nextDir, verbose); +end +end + +%------------------------------------------------------------------ + +function test = not_yet_in_classpath(classpath, filename) +%% Test whether the library was already imported +expression = strcat([filesep filename '$']); +test = isempty(cell2mat(regexp(classpath, expression))); +end \ No newline at end of file From 2b6d471b7289702a39bf281402c4d14cab87b19d Mon Sep 17 00:00:00 2001 From: KOUICHI NAKAMURA Date: Thu, 20 Sep 2018 19:58:49 +0100 Subject: [PATCH 36/36] ImageJ_javaaddpath.m src\scripts\update no longer requires imagej_directory input --- src/scripts/update/ImageJ.m | 33 +++++++++++++++------ src/scripts/update/ImageJ_javaaddpath.m | 38 ++++++------------------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/scripts/update/ImageJ.m b/src/scripts/update/ImageJ.m index d4a43e6..06c4a10 100644 --- a/src/scripts/update/ImageJ.m +++ b/src/scripts/update/ImageJ.m @@ -1,11 +1,29 @@ function ImageJ(open_imagej, verbose) -% This script adds the ImageJ libraries to the MATLAB classpath. By default, -% it will also start up an ImageJ instance within MATLAB. -% Parameters: -% open_imagej - If false, an ImageJ instance will not be launched. Default: true -% verbose - If true, a confirmation message will be printed the first time -% a jar is added to the MATLAB classpath. Default: false +% This function ImageJ adds the ImageJ libraries to the MATLAB classpath. +% By default, it will also start up an ImageJ instance within MATLAB. +% +% SYNTAX +% +% ImageJ +% ImageJ(open_imagej) +% ImageJ(open_imagej,verbose) +% +% PARAMETERS +% +% open_imagej true (default) | false | 1 | 0 +% +% If false, an ImageJ instance will not be launched. +% +% verbose true | false (default) | 1 | 0 +% +% If true, a confirmation message will be printed the first time +% a jar is added to the MATLAB classpath. +% % Author: Jacques Pecreaux, Johannes Schindelin, Jean-Yves Tinevez, Mark Hiner +% +% See also +% ImageJ_javaaddpath + if nargin < 1 open_imagej = true; @@ -16,9 +34,8 @@ function ImageJ(open_imagej, verbose) end %% Get the ImageJ directory -imagej_directory = fileparts(fileparts(mfilename('fullpath'))); -ImageJ_javaaddpath(imagej_directory,verbose); +ImageJ_javaaddpath(verbose); %% Maybe open the ImageJ window import net.imagej.matlab.*; diff --git a/src/scripts/update/ImageJ_javaaddpath.m b/src/scripts/update/ImageJ_javaaddpath.m index 3a5eca5..dc7459e 100644 --- a/src/scripts/update/ImageJ_javaaddpath.m +++ b/src/scripts/update/ImageJ_javaaddpath.m @@ -1,4 +1,4 @@ -function ImageJ_javaaddpath(imagej_directory,varargin) +function ImageJ_javaaddpath(varargin) % ImageJ_javaaddpath will add the paths for ImageJ/Fiji jar files to % dyanamic Java class path using javaaddpath, without launching ImageJ % instance. @@ -11,15 +11,10 @@ function ImageJ_javaaddpath(imagej_directory,varargin) % Virtually all of the code is taken from ImageJ.m. % % SYNTAX -% ImageJ_javaaddpath(imagej_directory) -% ImageJ_javaaddpath(imagej_directory,verbose) +% ImageJ_javaaddpath +% ImageJ_javaaddpath(verbose) % % INPUT ARGUMENTS -% imagej_directory -% char vector -% The directory path for 'Fiji.app'. -% -% Note that you can't include 'scripts' as 'Fiji.app\scripts' % % verbose true | false (default) | 1 | 0 % (Optional) true or 1 will print added java paths to the @@ -28,16 +23,16 @@ function ImageJ_javaaddpath(imagej_directory,varargin) % % EXAMPLE % -% fijipath = 'D\\Fiji.app\\'; +% fijiscriptpath = 'D:\Fiji.app\scripts'; % -% addpath(fullfile(fijipath,'scripts')) +% addpath(fijiscriptpath) % ImageJ % % I = imread('corn.tif',3); % uint8 % % parfor i = 1:10 -% addpath(fullfile(fijipath,'scripts')) % to make MATLAB functions for ImageJ available -% ImageJ_javaaddpath(fijipath) % to make ImageJ Java class available +% addpath(fijiscriptpath) % to make MATLAB functions for ImageJ available +% ImageJ_javaaddpath % to make ImageJ Java class available % % I1 = I - uint8(10*i); % imp = copytoImagePlus(I1); @@ -62,30 +57,15 @@ function ImageJ_javaaddpath(imagej_directory,varargin) % % See also % ImageJ, javaaddpath, parfor -% -% %TODO ImageJ.m can wrap this function p = inputParser; -p.addRequired('imagej_directory',@(x) isfolder(x)); p.addOptional('verbose',false,@(x) isscalar(x) && x == 1 || x == 0); -p.parse(imagej_directory,varargin{:}); +p.parse(varargin{:}); verbose = p.Results.verbose; - -if endsWith(imagej_directory,[filesep,'scripts']) - - error('imagej_directory %s should not include ''scripts''',... - imagej_directory) - -end - -% -% %% Get the ImageJ directory -% % imagej_directory = fileparts(fileparts(mfilename('fullpath'))); -% -% imagej_directory = fileparts(FijiScriptFolder); +imagej_directory = fileparts(fileparts(mfilename('fullpath'))); %% Get the Java classpath classpath = javaclasspath('-all');