-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_stacks_by_time.ijm
93 lines (77 loc) · 2.86 KB
/
merge_stacks_by_time.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This macro merges single-frame images into multi-frame images.
// It assumes the input images are 8-bit XYZ stacks named with the pattern "originalname_frameXXX.tif".
// The resulting multi-frame images are stored in a subfolder named 'merged_frames'.
// Set batch mode to true to speed up the macro by disabling the display of images during processing.
setBatchMode(true);
// Define input and output directories.
inputDir = getDirectory("Select input directory containing single frames");
outputDir = inputDir + File.separator + "merged_frames" + File.separator;
File.makeDirectory(outputDir);
// Get a list of all files in the input directory
list = getFileList(inputDir);
// Sort the list to ensure frames are in order
Array.sort(list);
// Initialize variables
previousBaseName = "";
frameCount = 0;
firstFrame = "";
for (i = 0; i < list.length; i++) {
if (endsWith(list[i], ".tif")) {
// Extract the base name (original name without frame number)
baseName = substring(list[i], 0, lastIndexOf(list[i], "_frame"));
if (baseName != previousBaseName && previousBaseName != "") {
// We've reached a new set of frames, so merge the previous set
mergeFrames(previousBaseName, frameCount, firstFrame);
frameCount = 0;
}
if (frameCount == 0) {
firstFrame = list[i];
}
frameCount++;
previousBaseName = baseName;
}
}
// Merge the last set of frames
if (frameCount > 0) {
mergeFrames(previousBaseName, frameCount, firstFrame);
}
setBatchMode(false);
function mergeFrames(baseName, frameCount, firstFrame) {
// Open the first frame to get dimensions
filePath = inputDir + firstFrame;
if (!File.exists(filePath)) {
print("Error: File not found - " + filePath);
return;
}
open(filePath);
if (nImages == 0) {
print("Error: Failed to open image - " + filePath);
return;
}
getDimensions(width, height, channels, slices, frames);
close();
// Create a new stack with the correct number of frames
newImage(baseName + "_merged", "8-bit", width, height, channels, slices, frameCount);
// Add each frame to the new stack
for (f = 1; f <= frameCount; f++) {
frameName = baseName + "_frame" + IJ.pad(f, 3) + ".tif";
filePath = inputDir + frameName;
if (!File.exists(filePath)) {
print("Error: File not found - " + filePath);
continue;
}
open(filePath);
if (nImages == 0) {
print("Error: Failed to open image - " + filePath);
continue;
}
run("Copy");
close();
selectImage(baseName + "_merged");
Stack.setFrame(f);
run("Paste");
}
// Save the merged stack
saveAs("Tiff", outputDir + baseName + "_merged.tif");
close();
}