Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2d19c08
Add a convenience Matlab script to call MIJ
Oct 9, 2010
bbb461c
Miji: Add some rudimentary documentation
dscho Oct 9, 2010
c0d0fd4
Add a demonstration how to use Miji
Oct 9, 2010
0377a63
Miji: add an option not to start ImageJ
dscho Oct 9, 2010
5f2889b
Miji: avoid warnings
dscho Dec 8, 2010
25251b0
Miji: fix bug in startup path
tinevez Jul 19, 2011
01c1c90
Miji: turn off some warnings
tinevez Jul 21, 2011
670b646
Miji: updated Java3D installer, added 2 demo scripts
tinevez Jul 25, 2011
75587c8
Miji's 3D examples: auto-install Java3D
dscho Jul 25, 2011
8e45667
Miji: add the overview Matlab file for the 3D Viewer examples
tinevez Jul 25, 2011
25c5069
Miji 3D Viewer example: a few touchups
dscho Jul 25, 2011
dd2df8f
Miji demos: add links and abide my ego
tinevez Jul 26, 2011
be41c4a
Un-DOSify files
dscho Jul 28, 2011
8265462
Miji: update MATLAB java3D installer for Windows paths
tinevez Aug 4, 2011
28fc3f1
Miji: add a 3rd demo, using surface plot
tinevez Aug 11, 2011
a861e33
Rotate the Matlab Peak and give it a similar color
dscho Aug 11, 2011
fa95d1f
Miji: added comments on Johannes comments
tinevez Aug 13, 2011
25e4515
UTF-8ify Matlab scripts
dscho Aug 17, 2011
4b943bd
Miji: initialize all the plugins and scripts correctly
dscho Sep 15, 2011
cb9401a
Miji: fix a bug in MATLAB java class path
tinevez Nov 6, 2011
ee880c9
Let's call it ij.executable and ij.dir
dscho Dec 13, 2011
1d7fe8f
copytoImg & copytoImgPlus: MATLAB functions to copy arrays to ImgLib2
tinevez Apr 8, 2013
6aa0024
copytoMatlab: MATLAB function to retrieve the content of an ImgLib2 A…
tinevez Apr 9, 2013
d0b947f
Miji.m: add jars in subdirectories
hinerm Jul 18, 2014
2dc8fda
Miji.m convert strcat to fullfile calls
hinerm Jul 18, 2014
d8bc3a9
Update Miji.m
ericbarnhill May 28, 2015
6211ff8
Use the SciJava fork of Java 3D 1.6
ctrueden Dec 18, 2015
b3f124e
Matlab3DViewerDemo_3.m: add missing EOF newline
ctrueden Dec 18, 2015
8a30031
Remove Java 3D auto-installation scripts
ctrueden Feb 25, 2016
dff4ff3
Added GNU Octave compatibility to Miji script
ericbarnhill Jul 8, 2016
f3f48d7
Miji: Add a verbose flag, stay in same directory - Avoid command prom…
bastelflp May 27, 2016
d3ab86c
copytoImgPlus.m
kouichi-c-nakamura Jul 2, 2018
cef0c77
copytoImagePlus.m scripts
kouichi-c-nakamura Jul 2, 2018
b87b3b9
Move migrated MATLAB scripts to script_templates
imagejan Jul 3, 2018
4153396
ImageJ_javaaddpath.m src\scripts\update
kouichi-c-nakamura Sep 20, 2018
2b6d471
ImageJ_javaaddpath.m src\scripts\update no longer requires imagej_di…
kouichi-c-nakamura Sep 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
%% 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 (most
% computers are, these days). 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 simple 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à!
%%
%
% <<MRI-rendering.png>>
%
%%
%
% _Jean-Yves Tinevez \<jeanyves.tinevez at gmail.com\> - July 2011_
134 changes: 134 additions & 0 deletions src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
%% 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,
% 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.

%%
% 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 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
% |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
% 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 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.
%
% 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
% behind 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|.
%
%
%
% <<octahedrons-rendering.png>>
%%
%
% _Jean-Yves Tinevez \<jeanyves.tinevez at gmail.com\> - July 2011_

88 changes: 88 additions & 0 deletions src/main/resources/script_templates/MATLAB/Matlab3DViewerDemo_3.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
%% 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.

%% Generate elevation data in MATLAB
% This is the well known *membrane* 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 = org.scijava.vecmath.Color3f(240 / 255, 120 / 255, 20 / 255);
c = universe.addSurfacePlot(imp, ...
org.scijava.vecmath.Color3f(), ...
'Matlab Peak in 3D', ...
1, ...
[true true true], ...
1);

%%
% 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]);
universe.fireContentChanged(c);
universe.centerSelected(c);
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
% 3D viewer. You can try the _Fullscreen_ option in the _View_ menu, to
% 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.
%%
%
% <<MatlabLogoInFiji.png>>
%
%%
%
% _Jean-Yves Tinevez \<jeanyves.tinevez at gmail.com\>_
%
% _Johannes Schindelin \<johannes.schindelin@gmx.de\>_
%
% _- August 2011_

Loading