BioImager  4.9.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
MicroManager.cs
1
2using AForge;
3using System;
4using System.Collections.Generic;
5using System.Diagnostics;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using RectangleD = AForge.RectangleD;
10using javax.swing;
11using BioLib;
12using System.Runtime.InteropServices;
13using org.micromanager.@internal;
14using mmcorej;
15
16namespace BioImager
17{
18 public static class MicroManager
19 {
20 public static mmcorej.CMMCore core;
21 public static MMStudio studio;
22 public static PointD location;
23 public static double focus;
24 public static Dictionary<string, List<Conf>> Config = new Dictionary<string, List<Conf>>();
25 public struct Conf
26 {
27 public string Class { get; set; }
28 public string Type { get; set; }
29 public string[] Values { get; set; }
30 public Conf(string Class, string Type, string[] Values)
31 {
32 this.Class = Class;
33 this.Type = Type;
34 this.Values = Values;
35 }
36 public override string ToString()
37 {
38 string s = Class + "," + Type + ",";
39 foreach (string item in Values)
40 {
41 s += item + ",";
42 }
43 return s;
44 }
45 }
46 public static Conf[] GetConfigs(string Class, string Type)
47 {
48 List<Conf> configs = new List<Conf>();
49 foreach (var conf in Config[Class])
50 {
51 if(conf.Type == Type) configs.Add(conf);
52 }
53 return configs.ToArray();
54 }
55 public static bool initialized = false;
56 public static string TurretName = "";
57 public static bool Initialize(string config)
58 {
59 try
60 {
61 Directory.SetCurrentDirectory("C:/Program Files/Micro-Manager-2.0/");
62 java.lang.System.setProperty("force.annotation.index", "true");
63 // Set the library path (adjust the path as needed)
64 java.lang.System.setProperty("org.micromanager.corej.path", "C:/Program Files/Micro-Manager-2.0");
65 java.lang.System.setProperty("user.dir", "C:/Program Files/Micro-Manager-2.0/");
66 MMStudio.main(new string[] { });
67 studio = MMStudio.getInstance();
68 core = (CMMCore)studio.core();
69 Objectives.Initialize();
70 Directory.SetCurrentDirectory(Path.GetDirectoryName(Environment.ProcessPath));
71 return true;
72 }
73 catch (Exception e)
74 {
75 // Log the exception message
76 Console.WriteLine("Error during Micro-Manager initialization: " + e.Message);
77 return false; // Return false on failure
78 }
79 }
80
81 public static BioImage TakeImage(string name)
82 {
83 int width, height, depth, bits, channels = 0;
84 AForge.Bitmap[] bm;
85 TakeImage(name,out width,out height,out depth,out bits,out channels, out bm);
86 BioImage im = new BioImage(name);
87 foreach (AForge.Bitmap item in bm)
88 {
89 item.Stats = Statistics.FromBytes(item);
90 }
91 im.Buffers.AddRange(bm);
92 //Set the physical size based on objective view
93 RectangleD rec = Microscope.GetViewRectangle(false);
94 Point3D p = Microscope.GetPosition();
95 Resolution res = new Resolution(width, height, im.Buffers[0].PixelFormat, rec.W / im.Buffers[0].SizeX, rec.H / im.Buffers[0].SizeY, 1, p.X, p.Y, p.Z);
96 im.Volume.Location = p;
97 im.Volume.Width = rec.W;
98 im.Volume.Height = rec.H;
99 im.bitsPerPixel = im.Buffers[0].BitsPerPixel;
100 im.UpdateCoords(1, 1, 1);
101 im.Resolutions.Add(res);
102 for (int c = 0; c < bm.Length; c++)
103 {
104 im.Channels.Add(new Channel(c, bm[c].BitsPerPixel, bm[c].RGBChannelsCount));
105 }
106 BioImage.AutoThreshold(im, false);
107 return im;
108 }
109 private static bool TakeImage(string file, out int width, out int height, out int depth, out int bits, out int channels, out AForge.Bitmap[] bm)
110 {
111 string fil = Path.GetFileNameWithoutExtension(file);
112 width = (int)core.getImageWidth();
113 height = (int)core.getImageHeight();
114 depth = (int)core.getImageBitDepth();
115 bits = (int)core.getBytesPerPixel();
116 channels = (int)core.getNumberOfCameraChannels();
117 PixelFormat format = PixelFormat.Format8bppIndexed;
118 if (depth > 8 && bits == 2)
119 format = PixelFormat.Format16bppGrayScale;
120 else if(depth > 8 && bits == 6)
121 format = PixelFormat.Format48bppRgb;
122 else if (depth == 8 && bits == 3)
123 format = PixelFormat.Format24bppRgb;
124 else if (depth == 8 && bits == 4)
125 format = PixelFormat.Format32bppArgb;
126 bm = new AForge.Bitmap[channels];
127 core.snapImage();
128 for (int i = 0; i < channels; i++)
129 {
130 if(depth <= 8)
131 {
132 byte[] bytes = (byte[])core.getImage(i);
133 bm[i] = new AForge.Bitmap(width, height, format, bytes , new ZCT(), "");
134 }
135 else
136 {
137 ushort[] buf = (ushort[])core.getImage(i);
138 byte[] bts = new byte[buf.Length * 2];
139 for (int b = 0; b < buf.Length; b++)
140 {
141 byte[] pl = BitConverter.GetBytes(buf[b]);
142 bts[b] = pl[0];
143 bts[b+1] = pl[1];
144 }
145 bm[i] = new AForge.Bitmap(width, height, format, bts, new ZCT(), "");
146 }
147 }
148 return true;
149 }
150 public static void SetExposure(float f)
151 {
152 core.setExposure(f);
153 }
154 public static void SetPosition(Point3D p)
155 {
156 core.setXYPosition(p.X, p.Y);
157 core.setPosition(p.Z);
158 }
159 public static void SetPosition(PointD p)
160 {
161 core.setXYPosition(p.X, p.Y);
162 }
163 public static void GetPosition3D(out Point3D p, bool update)
164 {
165 var pos = core.getXYStagePosition();
166 var f = core.getPosition();
167 p = new Point3D(pos.x, pos.y, f);
168 }
169 public static void GetPosition(out PointD p, bool update)
170 {
171 var pos = core.getXYStagePosition();
172 p = new PointD(pos.x, pos.y);
173 }
174 public static class Objectives
175 {
179 public static int InstalledCount
180 {
181 get
182 {
183 return GetConfigs("ConfigGroup", "Objective").Length;
184 }
185 }
186 public static List<Objective> List = new List<Objective>();
187 public class Objective
188 {
189 public string Name { get; set; }
190 public int Index { get; set; }
191 public int Magnification { get; set; }
192 public Objective(string name, int index, int mag)
193 {
194 Name = name;
195 Index = index;
196 Magnification = mag;
197 }
198 }
199 internal static void Initialize()
200 {
201 foreach (Conf obj in GetConfigs("ConfigGroup", "Objective"))
202 {
203 List.Add(new Objective(obj.Values[0], int.Parse(obj.Values[3]), int.Parse(obj.Values[0].Replace("X", ""))));
204 }
205 }
206
207 public static Conf GetObjective(int index)
208 {
209 Conf[] cfs = GetConfigs("ConfigGroup", "Objective");
210 return cfs[index];
211 }
212 public static void SetPosition(int i)
213 {
214 core.setConfig(TurretName, List[i].Name);
215 }
216 public static int GetPosition()
217 {
218 string s = core.getCurrentConfig(TurretName);
219 int i = 0;
220 foreach(Objective obj in Objectives.List)
221 {
222 if (obj.Name == s)
223 return i;
224 }
225 return -1;
226 }
227 }
228 public static class Shutters
229 {
230 public class Shutter
231 {
232 public string Name { get; set; }
233 public Shutter(string name)
234 {
235 Name = name;
236 }
237 }
238 public static List<Shutter> List = new List<Shutter>();
239 public static void Initialize()
240 {
241 if (Config.ContainsKey("Device"))
242 {
243 //We get the name of the shutters.
244 foreach (Conf item in Config["Device"])
245 {
246 if (item.Values.Last().Contains("Shutter"))
247 {
248 List.Add(new Shutter(item.Type));
249 }
250 }
251 }
252 }
253 public static int GetPosition(string shutterName)
254 {
255 if (core.getShutterOpen())
256 return 1;
257 else
258 return 0;
259 }
260 public static void SetPosition(string shutterName, int state)
261 {
262 if(state == 1)
263 core.setShutterOpen(shutterName, true);
264 else
265 core.setShutterOpen(shutterName, false);
266 }
267 }
268
269
270 }
271}