-
Notifications
You must be signed in to change notification settings - Fork 6
/
Transfer_Labels.ijm
executable file
·59 lines (48 loc) · 1.66 KB
/
Transfer_Labels.ijm
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
// Transfer Labels macro by Christophe Leterrier
// Allow to transfer label from one stack to another stack, changing one string into another in the slice labels. Useful if a process creates a new stack without carrying labels information
macro "Transfer_Labels" {
setBatchMode(true);
// We need at least two stacks
if (nImages < 1) exit("Not enough images open");
// Get titles of all opened images
TITLES = newArray(nImages);
for (i = 1; i< = nImages; i++) {
selectImage(i);
TITLES[i-1] = getTitle;
}
//Dialog
Dialog.create("Label Parameters");
Dialog.addChoice("Source stack", TITLES, TITLES[0]);
Dialog.addChoice("Destination stack", TITLES, TITLES[0]);
Dialog.addString("Replace source string", "", 6);
Dialog.addString("by new string", "", 6);
Dialog.show;
SOURCE = Dialog.getChoice;
DEST = Dialog.getChoice;
OLD = Dialog.getString;
NEW = Dialog.getString;
// Slice number for destination stack
selectImage(DEST);
nDEST = nSlices;
if (nDEST == 1) exit("Destination is not a stack");
// Slice number for destination stack
selectImage(SOURCE);
nSOURCE = nSlices;
// Exit if Slice number is different
if (nSOURCE != nDEST) exit("Source and destination stacks do not have the same number of slices!");
// Store all labels from source stack and modify them
LABELS = newArray(nSOURCE);
for (i = 0; i < nSOURCE; i++) {
setSlice(i+1);
LAB = getInfo("slice.label");
// Replace source string with new string in label
LABELS[i] = replace(LAB, OLD, NEW);
}
// Apply new labels to destination stack
selectImage(DEST);
for (i = 0; i < nSOURCE; i++) {
setSlice(i+1);
setMetadata("Label", LABELS[i]);
}
setBatchMode("exit and display");
}