BioLib  4.3.0
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
BioLib.ImageJ Class Reference

Static Public Member Functions

static void RunMacro (string file, string param)
 The function "RunMacro" runs a macro file with specified parameters using the ImageJ software.
 
static async void RunString (BioImage b, string con, string param, bool headless)
 The function RunString executes an ImageJ macro script with the given parameters, either in headless mode or with a graphical interface.
 
static void RunOnImage (BioImage bi, string con, int index, bool headless, bool onTab, bool bioformats, bool resultInNewTab, string outputStem=null)
 The function "RunOnImage" takes a BioImage object, a string of commands, an index, and several boolean parameters, and performs various operations on the image.
 
static async Task< BioImage > RunOnPyramidalImage (BioImage b, string con, int level, bool headless, bool onTab, bool bioformats, bool resultInNewTab, bool currentViewOnly=false)
 Runs an ImageJ command on a pyramidal BioImage by processing the requested level as a single-resolution image for Fiji.
 
static async Task< BioImage > RunOnAllPyramidLevels (BioImage b, string con, bool headless, bool onTab, bool bioformats, bool resultInNewTab)
 Runs an ImageJ command on every pyramid level and returns a single pyramidal BioImage assembled from the processed resolution levels.
 
static void RunOnImage (BioImage b, string con, bool headless, bool onTab, bool bioformats, bool resultInNewTab)
 The function "RunOnImage" takes in a BioImage object, a string, and several boolean values as parameters and executes a specific action based on those parameters.
 
static void Initialize (string path)
 The Initialize function sets the ImageJPath variable to the specified path.
 
static bool SetImageJPath (string? path)
 

Static Public Attributes

static List< Process > processes = new List<Process>()
 

Properties

static string ImageJPath [get, set]
 

Member Function Documentation

◆ Initialize()

static void BioLib.ImageJ.Initialize ( string path)
static

The Initialize function sets the ImageJPath variable to the specified path.

Parameters
pathThe path parameter is a string that represents the file path where the ImageJ software is located.
1002 {
1003 SetImageJPath(path);
1004 }
static bool SetImageJPath(string? path)
Definition ImageJ.cs:1009

◆ RunMacro()

static void BioLib.ImageJ.RunMacro ( string file,
string param )
static

The function "RunMacro" runs a macro file with specified parameters using the ImageJ software.

Parameters
fileThe "file" parameter is a string that represents the path to the macro file that you want to run. It should include the file extension (e.g., ".ijm" for ImageJ macro files).
paramThe "param" parameter is a string that represents the additional parameters or arguments that you want to pass to the macro file specified by the "file" parameter. These parameters can be used by the macro file to modify its behavior or perform specific actions.
70 {
71 file.Replace("/", "\\");
72 Process pr = new Process();
73 pr.StartInfo.FileName = ImageJPath;
74 pr.StartInfo.Arguments = "-macro " + file + " " + param;
75 pr.Start();
76 processes.Add(pr);
77 }
static string ImageJPath
Definition ImageJ.cs:21
static List< Process > processes
Definition ImageJ.cs:31

◆ RunOnAllPyramidLevels()

static async Task< BioImage > BioLib.ImageJ.RunOnAllPyramidLevels ( BioImage b,
string con,
bool headless,
bool onTab,
bool bioformats,
bool resultInNewTab )
static

Runs an ImageJ command on every pyramid level and returns a single pyramidal BioImage assembled from the processed resolution levels.

925 {
926 if (b == null)
927 return null;
928
929 if (!b.isPyramidal || b.Resolutions.Count == 0)
930 {
931 RunOnImage(b, con, headless, onTab, bioformats, resultInNewTab);
932 Recorder.Record($"ImageJ.RunOnImage({b}, \"{con}\", {headless.ToString().ToLower()}, {onTab.ToString().ToLower()}, {bioformats.ToString().ToLower()}, {resultInNewTab.ToString().ToLower()});");
933 return b;
934 }
935
936 string effectiveCommand = await TryParameterizePyramidalCommand(b, con, headless, resultInNewTab).ConfigureAwait(false);
937 List<BioImage> results = new List<BioImage>();
938 for (int level = 0; level < b.Resolutions.Count; level++)
939 {
940 ReportProgress(0, $"Processing pyramid level {level + 1}/{b.Resolutions.Count}");
941 BioImage result = await RunOnPyramidalImageInternal(b, effectiveCommand, level, headless, onTab, bioformats, resultInNewTab, false, false).ConfigureAwait(false);
942 if (result != null)
943 results.Add(result);
944 }
945
946 if (results.Count == 0)
947 return null;
948
949 string tempDir = Path.GetDirectoryName(Environment.ProcessPath);
950 string tempFile = Path.Combine(tempDir, Guid.NewGuid().ToString("N") + "-pyr.zarr");
951 ReportProgress(0, $"Saving processed pyramid ({results.Count}/{b.Resolutions.Count} levels)");
952 await BioImage.SaveOMEZarr(results.ToArray(), tempFile).ConfigureAwait(false);
953
954 ReportProgress(0, "Opening processed pyramid");
955 BioImage pyramidal = await BioImage.OpenFileAsync(tempFile, 0, false, false).ConfigureAwait(false);
956 pyramidal = ApplyProcessedResultName(b, pyramidal);
957 ReportProgress(100, "Completed pyramid command");
958 Recorder.Record($"ImageJ.RunOnAllPyramidLevels({b}, \"{con}\", {headless.ToString().ToLower()}, {onTab.ToString().ToLower()}, {bioformats.ToString().ToLower()}, {resultInNewTab.ToString().ToLower()});");
959 return pyramidal;
960 }
System.IO.Path Path
Definition Bio.cs:57
return b
Definition Bio.cs:7393
static void RunOnImage(BioImage bi, string con, int index, bool headless, bool onTab, bool bioformats, bool resultInNewTab, string outputStem=null)
The function "RunOnImage" takes a BioImage object, a string of commands, an index,...
Definition ImageJ.cs:164

◆ RunOnImage() [1/2]

static void BioLib.ImageJ.RunOnImage ( BioImage b,
string con,
bool headless,
bool onTab,
bool bioformats,
bool resultInNewTab )
static

The function "RunOnImage" takes in a BioImage object, a string, and several boolean values as parameters and executes a specific action based on those parameters.

Parameters
BioImageThe BioImage parameter is an object that represents an image in a bioinformatics context. It likely contains information about the image, such as its dimensions, pixel values, and metadata.
conThe "con" parameter is a string that represents the command to be executed on the image. It could be any valid ImageJ command or macro.
headlessThe "headless" parameter is a boolean value that determines whether the image processing operation should be performed in headless mode. Headless mode means that the operation will be performed without any graphical user interface (GUI) being displayed.
onTabThe "onTab" parameter determines whether the image processing should be performed on the current active tab or on a new tab. If "onTab" is set to true, the processing will be performed on the current active tab. If "onTab" is set to false, a new tab will
bioformatsThe "bioformats" parameter is a boolean value that determines whether to use the Bio-Formats plugin for opening the image. If set to true, the Bio-Formats plugin will be used to open the image, allowing for support of various file formats. If set to false, the default image opening
resultInNewTabA boolean value indicating whether the result should be displayed in a new tab or not.
986 {
987 if (b != null && b.isPyramidal)
988 {
989 RunOnAllPyramidLevels(b, con, headless, onTab, bioformats, resultInNewTab).Wait();
990 return;
991 }
992 RunOnImage(b, con,0,headless,onTab,bioformats,resultInNewTab);
993 Recorder.AddLine("ImageJ.RunOnImage(Images.GetImage(\"" + b.Filename + "\"),\"" + con + "\"," + 0 + "," + headless + "," + onTab + "," + bioformats + "," + resultInNewTab + ");",false);
994 }
static async Task< BioImage > RunOnAllPyramidLevels(BioImage b, string con, bool headless, bool onTab, bool bioformats, bool resultInNewTab)
Runs an ImageJ command on every pyramid level and returns a single pyramidal BioImage assembled from ...
Definition ImageJ.cs:924

◆ RunOnImage() [2/2]

static void BioLib.ImageJ.RunOnImage ( BioImage bi,
string con,
int index,
bool headless,
bool onTab,
bool bioformats,
bool resultInNewTab,
string outputStem = null )
static

The function "RunOnImage" takes a BioImage object, a string of commands, an index, and several boolean parameters, and performs various operations on the image.

Parameters
BioImageThe BioImage object represents an image file. It contains information about the image file such as the file path, filename, and ID.
conThe "con" parameter is a string that represents the ImageJ macro code that you want to run on the image. It is passed as an argument to the "run" function in the ImageJ macro language.
indexThe index parameter is used to specify the index of the image series to be processed. It is used when opening multi-series images, such as multi-channel or time-lapse images, where each series represents a different set of images. By specifying the index, you can select a specific series to be processed
headlessThe "headless" parameter is a boolean value that determines whether the image processing should be performed in headless mode. Headless mode means that the processing is done without any user interface or graphical display.
onTabThe "onTab" parameter determines whether the image processing should be performed on the current tab or on a new tab. If it is set to true, the processing will be done on the current tab. If it is set to false, a new tab will be created for the processed image.
bioformatsA boolean flag indicating whether to use Bio-Formats for importing and exporting the image. If set to true, Bio-Formats will be used. If set to false, the image will be opened and saved using the default ImageJ functions.
resultInNewTabA boolean value indicating whether the result should be displayed in a new tab or not.
Returns
The method does not return any value. It is a void method.
165 {
166 if (OperatingSystem.IsMacOS())
167 bioformats = false;
168 Console.WriteLine($"[ImageJ.RunOnImage] input={bi?.ID} index={index} headless={headless} onTab={onTab} bioformats={bioformats} resultInNewTab={resultInNewTab} outputStem={outputStem}");
169 string filename = "";
170 string dir = Path.GetDirectoryName(bi.file);
171 dir = dir.Replace("\\", "/");
172 string inputPath = dir + "/" + bi.ID;
173 bool inputIsZarr = bi.ID != null && bi.ID.EndsWith(".zarr", StringComparison.OrdinalIgnoreCase);
174 if (bi.ID.EndsWith(".ome.tif"))
175 {
176 filename = Path.GetFileNameWithoutExtension(bi.ID);
177 filename = filename.Remove(filename.Length - 4, 4);
178 }
179 else
180 filename = Path.GetFileNameWithoutExtension(bi.ID);
181 string file = inputIsZarr ? inputPath : dir + "/" + filename + ".ome.tif";
182 if (!bioformats && !inputIsZarr)
183 file = dir + "/" + filename + ".tif";
184 string outputName = string.IsNullOrWhiteSpace(outputStem) ? filename : outputStem;
185 string exportPath = bioformats
186 ? dir + "/" + outputName + "-temp.ome.tif"
187 : dir + "/" + outputName + ".tif";
188 string donepath = Path.GetDirectoryName(Environment.ProcessPath);
189 donepath = donepath.Replace("\\", "/");
190 string st =
191 "run(\"Bio-Formats Importer\", \"open=" + file + " autoscale color_mode=Default open_all_series display_rois rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT\"); " + con +
192 "run(\"Bio-Formats Exporter\", \"save=" + exportPath + " export compression=Uncompressed\"); " +
193 "dir = \"" + donepath + "\"" +
194 "File.saveString(\"done\", dir + \"/done.txt\");";
195 if (!bioformats)
196 st =
197 "open(getArgument); " + con +
198 "saveAs(\"Tiff\",\"" + exportPath + "\"); " +
199 "dir = \"" + donepath + "\"" +
200 "File.saveString(\"done\", dir + \"/done.txt\");";
201 Console.WriteLine($"[ImageJ.RunOnImage] inputFile={file}");
202 RunString(bi,st, dir + "/" + bi.ID, headless);
203 string sourcePath = bioformats ? file : inputPath;
204 if (!File.Exists(sourcePath))
205 {
206 Console.WriteLine($"[ImageJ.RunOnImage] input file missing after run: {sourcePath}");
207 return;
208 }
209
210 string f = ResolveExportedImagePath(exportPath);
211 if (f == null)
212 {
213 Console.WriteLine($"[ImageJ.RunOnImage] output file not found for exportPath={exportPath}");
214 return;
215 }
216
217 Console.WriteLine($"[ImageJ.RunOnImage] resolvedOutput={f}");
218
219 string fn = Path.GetFileName(f);
220 //If not in images we add it to a new tab.
221 if (Images.GetImage(fn) == null)
222 {
223 BioImage bm = BioImage.OpenFileAsync(f, index, false, false).Result;
224 Console.WriteLine($"[ImageJ.RunOnImage] opened new image {bm?.ID} filename={bm?.Filename}");
225 bm.Filename = fn;
226 bm.ID = fn;
227 bm.file = dir + "/" + fn;
228 Images.AddImage(bm);
229 lastRunOutput = bm;
230 }
231 else
232 {
233 BioImage b = BioImage.OpenFileAsync(f, index, false, false).Result;
234 Console.WriteLine($"[ImageJ.RunOnImage] updating existing image with {b?.ID} filename={b?.Filename}");
235 b.ID = bi.ID;
236 b.Filename = bi.ID;
237 b.file = dir + "/" + fn;
238 Images.UpdateImage(b);
239 lastRunOutput = b;
240 }
241 //If using bioformats we delete the temp file.
242 if(bioformats)
243 File.Delete(f);;
244 //Recorder.AddLine("ImageJ.RunOnImage(\"" + con + "\"," + headless + "," + onTab + "," + bioformats + "," + resultInNewTab + ");");
245 }
static Bitmap bm
Definition Bio.cs:8524
static async void RunString(BioImage b, string con, string param, bool headless)
The function RunString executes an ImageJ macro script with the given parameters, either in headless ...
Definition ImageJ.cs:93

◆ RunOnPyramidalImage()

static async Task< BioImage > BioLib.ImageJ.RunOnPyramidalImage ( BioImage b,
string con,
int level,
bool headless,
bool onTab,
bool bioformats,
bool resultInNewTab,
bool currentViewOnly = false )
static

Runs an ImageJ command on a pyramidal BioImage by processing the requested level as a single-resolution image for Fiji.

916 {
917 return await RunOnPyramidalImageInternal(b, con, level, headless, onTab, bioformats, resultInNewTab, true, currentViewOnly).ConfigureAwait(false);
918 }

◆ RunString()

static async void BioLib.ImageJ.RunString ( BioImage b,
string con,
string param,
bool headless )
static

The function RunString executes an ImageJ macro script with the given parameters, either in headless mode or with a graphical interface.

Parameters
conThe "con" parameter is a string that represents the content of a file. It will be written to a temporary file and used as input for the ImageJ macro.
paramThe "param" parameter is a string that represents the additional arguments or parameters that you want to pass to the ImageJ application when running the macro. These arguments can be used to customize the behavior of the macro or provide input data to it.
headlessThe "headless" parameter is a boolean value that determines whether the ImageJ process should run in headless mode or not. Headless mode means that the ImageJ interface will not be displayed, and the process will run in the background without any user interaction. If the "headless" parameter
94 {
95 Process pr = new Process();
96 pr.StartInfo.FileName = ImageJPath;
97 string te = rng.Next(0, 9999999).ToString();
98 string p = Path.GetDirectoryName(Environment.ProcessPath) + "/" + te;
99 if (OperatingSystem.IsMacOS())
100 {
101 Console.WriteLine(p);
102 pr.StartInfo.UseShellExecute = true;
103 }
104 File.WriteAllText(p, con);
105 if (headless)
106 pr.StartInfo.Arguments = "--headless -macro " + p + " " + param;
107 else
108 pr.StartInfo.Arguments = "-macro " + p + " " + param;
109 pr.Start();
110 string donedir = Path.GetDirectoryName(Environment.ProcessPath);
111 donedir = donedir.Replace("\\", "/");
112 File.Delete(Path.GetDirectoryName(Environment.ProcessPath) + "/done.txt");
113 processes.Add(pr);
114 do
115 {
116 if (File.Exists(donedir + "/done.txt"))
117 {
118 do
119 {
120 try
121 {
122 File.Delete(donedir + "/done.txt");
123 }
124 catch (Exception)
125 {
126
127 }
128 } while (File.Exists(donedir + "/done.txt"));
129 pr.Kill();
130 break;
131 }
132 } while (!pr.HasExited);
133 File.Delete(p);
134 }

◆ SetImageJPath()

static bool BioLib.ImageJ.SetImageJPath ( string? path)
static

This function creates a file chooser dialog that allows the user to select the location of the ImageJ executable

Returns
A boolean value.
1010 {
1011 if (path == null || path == "")
1012 {
1013 string st = Settings.GetSettings("ImageJPath");
1014 if (st != "")
1015 {
1016 ImageJPath = Settings.GetSettings("ImageJPath");
1017 return true;
1018 }
1019 return false;
1020 }
1021 else
1022 {
1023 if (Settings.GetSettings("ImageJPath") == "")
1024 {
1025 Settings.AddSettings("ImageJPath", path);
1026 Settings.Save();
1027 }
1028 ImageJPath = path;
1029 return true;
1030 }
1031 }

Member Data Documentation

◆ processes

List<Process> BioLib.ImageJ.processes = new List<Process>()
static

Property Documentation

◆ ImageJPath

string BioLib.ImageJ.ImageJPath
staticgetset
21 {
22 get
23 {
24 return imjpath;
25 }
26 set
27 {
28 imjpath = value.Replace("\\", "/");
29 }
30 }

The documentation for this class was generated from the following file: