Recently I'm planning to manipulate a stack of images and the goal is to extract a specific subset of slices from there, for example only even or odd or arbitrary indexes, and then save them into another dataset.
In DM, there are a number of helpful functions in the Volume menu but unfortunately, they cannot really fullfill what I want to do.
I am just wondering whether this idea can be realized via scripting.
Many thanks for your help in advance.
There are two ways you can go about it, one of them only suitable for data up to 3D and generally slower than the other, but more flexible. As you have been asking for arbitrary subsampling, I'm starting with that option, but it is more likely that the second option gives you what you want: orthogonal, regular subsampling.
If you are in a hurry, the short answer is: Use the
SliceN
command.1) Using expressions (arbitrary subsampling)
Note that even if this addresses a single number, the result is an expression of size 1x1 or 1x1x1 and not a scalar number, therefore you can not do:
number num = img[10,4]
However, you can use a little trick to use any of the functions that convert an expression to a single number like f.e. summation. So you can do:
number num = sum(img[10,4])
So how does this relate to your question? Well, in the expressions above, we used scalar values as
X
,Y
andZ
, and the resulting expressions were expressions of size 1x1 and 1x1x1, butThis will become clearer with the examples below. Starting out with a simple 1D example:
Our testdata (
img1D
) here is just a linear graph from 1000 to 1099 using theicol
expression which, at each pixel, represents that pixels X coordinate.The coordinate image (
coord
) is containing random integer values between 0 and 99.The 'magic' happens in the
subImg
. We use an expression with thecoord
image as X coordinates. That images is of size 10(x1), so the outcoming expression is of size 10(x1) which we assign to the imagesubImg
before showing it.Note, that the expression we have built is really just pointing to that data of the image. Instead of showing it as a new image, we could have use that expression to change these points in the data instead, using:
img1D[coord,0] = 0
Taking it from here, it is straight forward to extend the example to 2D:
...and 3D:
Unfortunately, it ends here.
2) Using SliceN (orthogonal subsampling)
The
SliceN
command is maybe one of my favourite commands in the language when dealing with data. It looks intimidating at first, but it is straight forward.Lets start with its simplified version for 1D extraction,
Slice1
.So a very simple example of extracting a 1D data of a 3D dataset would be:
This example showed a quite typical situation in analytical microscopy when dealing with "3D Spectrum Image" data: Extracting a "1D Spectrum" at a specific spatial position.
The example did that for the spatial point
px,py
. Starting at the point at that position (px,py,0
), it samples along the Z direction (2
) for all pixels of the data (sz
) with a step-size of1
.Note, that the command again returns an expression within the source data, and that you can use this to set values as well, just using f.e.:
Slice1( img3D, px,py,0, 2,sz,1 ) = 0
The extension for 2D and 3D data using the commands
Slice2
andSlice3
is straight forward. Instead of defining one output direction, you define two or three, respectively. Each with a triplet of numbers: direction, length, step-size.The following example extracts an "image plane" of a "3D Spectrum image":
And the following example "rotates" a 3D image:
It works exactly the same, but you need to specify both the dimensionality of the source data, and the dimensionality of the output expression. Then, the 'starting' point needs to be defined with as many coordinates as the source data dimension suggests, and you need one triplet of specification for each output dimension.
For a source data of
N
dimensions and want an output ofM
dimensions you need:2 + N + 3*M
parameters.As an example, lets extract the "plane" at specific spatial position from a "4D Diffraction image" data, which stores a 2D image at each spatial location of a 2D scan: