-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapplyVectorField.py
76 lines (52 loc) · 1.93 KB
/
applyVectorField.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# applyVectorField.py
# ===================
#
# This script demonstrates how to apply a
# warping to an image on the GPU
#
# Author: Robert Haase, [email protected]
# October 2019
#########################################
from ij import IJ;
from net.haesleinhuepf.clij2 import CLIJ2;
from net.imglib2.realtransform import AffineTransform2D;
from ij.gui import NewImage;
from ij.gui import OvalRoi;
from java.lang import Math;
IJ.run("Close All");
# load/create example images
imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
IJ.run(imp, "32-bit", "");
imp.show();
shiftX = NewImage.createFloatImage("", imp.getWidth(), imp.getHeight(), 1, NewImage.FILL_BLACK);
shiftY = NewImage.createFloatImage("", imp.getWidth(), imp.getHeight(), 1, NewImage.FILL_BLACK);
# define shift some of the pixels in X
shiftX.setRoi(OvalRoi(20, 98, 72, 68));
IJ.run(shiftX, "Add...", "value=25");
IJ.run(shiftX, "Select None", "");
IJ.run(shiftX, "Gaussian Blur...", "sigma=15");
# init GPU
clij2 = CLIJ2.getInstance();
# push image to GPU
input = clij2.push(imp);
shiftXgpu = clij2.push(shiftX);
rotatedShiftXgpu = clij2.create(shiftXgpu);
shiftYgpu = clij2.push(shiftY);
temp = clij2.create(input);
# reserve memory for output
output = clij2.create([input.getWidth(), input.getHeight(), 36], input.getNativeType());
for i in range(0, 36):
# change the shift from slice to slice
at = AffineTransform2D();
at.translate(-input.getWidth() / 2, -input.getHeight() / 2);
at.rotate(i * 10.0 / 180.0 * Math.PI);
at.translate(input.getWidth() / 2, input.getHeight() / 2);
clij2.affineTransform2D(shiftXgpu, rotatedShiftXgpu, at);
# apply transform
clij2.applyVectorField(input, rotatedShiftXgpu, shiftYgpu, temp);
# put resulting 2D image in the right plane
clij2.copySlice(temp, output, i);
# show result
clij2.pull(output).show();
IJ.setMinAndMax(0, 255);
clij2.clear();