-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmotionCorrection.ijm
More file actions
76 lines (57 loc) · 1.73 KB
/
motionCorrection.ijm
File metadata and controls
76 lines (57 loc) · 1.73 KB
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
// CLIJ example macro: motionCorrection,ijm
//
// This macro shows how to do motion correction
// image stack where slices might be shifted to
// each other in the GPU.
//
// Author: Robert Haase
// December 2018
// ---------------------------------------------
run("Close All");
// define move to correct
file = "https://github.com/clij/clij-docs/raw/master/src/main/resources/motion_correction_Drosophila_DSmanila1.tif";
//file = "C:/structure/code/clij-docs/src/main/resources/motion_correction_Drosophila_DSmanila1.tif";
// define identifiers for intermediate results in the GPU
inputStack = "inputStack";
slice = "slice";
shifted = "shifted";
binary = "binary";
// define a threshold to differentiate object and background
threshold = 50;
// open image
open(file);
rename(inputStack);
run("32-bit");
// collect info about it
getDimensions(width, height, channels, slices, frames);
// Init GPU
run("CLIJ Macro Extensions", "cl_device=");
Ext.CLIJ_clear();
Ext.CLIJ_push(inputStack);
formerX = 0;
formerY = 0;
// process all slices; only the first stays where it is
for (z = 0; z < slices; z++) {
IJ.log("z: " + z);
Ext.CLIJ_copySlice(inputStack, slice, z);
// determine center of mass
Ext.CLIJ_threshold(slice, binary, threshold);
Ext.CLIJ_centerOfMass(binary);
x = getResult("MassX", nResults() - 1);
y = getResult("MassY", nResults() - 1);
if (z > 0) {
// determine shift
deltaX = x - formerX;
deltaY = y - formerY;
// apply translation transformation
Ext.CLIJ_affineTransform2D(slice, shifted, "translatex=" + deltaX + " translatey=" + deltaY);
// copy result back
Ext.CLIJ_copySlice(shifted, inputStack, z);
} else {
formerX = x;
formerY = y;
}
}
// show result
Ext.CLIJ_pull(inputStack);
rename("outputStack");