BioImager  5.0.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
Go to the documentation of this file.
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
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.UpdateCoords(1, 1, 1);
100 im.Resolutions.Add(res);
101 for (int c = 0; c < bm.Length; c++)
102 {
103 im.Channels.Add(new Channel(c, bm[c].BitsPerPixel, bm[c].RGBChannelsCount));
104 }
105 BioImage.AutoThreshold(im, false);
106 return im;
107 }
108 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)
109 {
110 string fil = Path.GetFileNameWithoutExtension(file);
111 width = (int)core.getImageWidth();
112 height = (int)core.getImageHeight();
113 depth = (int)core.getImageBitDepth();
114 bits = (int)core.getBytesPerPixel();
115 channels = (int)core.getNumberOfCameraChannels();
116 PixelFormat format = PixelFormat.Format8bppIndexed;
117 if (depth > 8 && bits == 2)
118 format = PixelFormat.Format16bppGrayScale;
119 else if(depth > 8 && bits == 6)
120 format = PixelFormat.Format48bppRgb;
121 else if (depth == 8 && bits == 3)
122 format = PixelFormat.Format24bppRgb;
123 else if (depth == 8 && bits == 4)
124 format = PixelFormat.Format32bppArgb;
125 bm = new AForge.Bitmap[channels];
126 core.snapImage();
127 for (int i = 0; i < channels; i++)
128 {
129 if(depth <= 8)
130 {
131 byte[] bytes = (byte[])core.getImage(i);
132 bm[i] = new AForge.Bitmap(width, height, format, bytes , new ZCT(), "");
133 }
134 else
135 {
136 ushort[] buf = (ushort[])core.getImage(i);
137 byte[] bts = new byte[buf.Length * 2];
138 for (int b = 0; b < buf.Length; b++)
139 {
140 byte[] pl = BitConverter.GetBytes(buf[b]);
141 bts[b] = pl[0];
142 bts[b+1] = pl[1];
143 }
144 bm[i] = new AForge.Bitmap(width, height, format, bts, new ZCT(), "");
145 }
146 }
147 return true;
148 }
149 public static void SetExposure(float f)
150 {
151 core.setExposure(f);
152 }
153 public static void SetPosition(Point3D p)
154 {
155 core.setXYPosition(p.X, p.Y);
156 core.setPosition(p.Z);
157 }
158 public static void SetPosition(PointD p)
159 {
160 core.setXYPosition(p.X, p.Y);
161 }
162 public static void GetPosition3D(out Point3D p, bool update)
163 {
164 var pos = core.getXYStagePosition();
165 var f = core.getPosition();
166 p = new Point3D(pos.x, pos.y, f);
167 }
168 public static void GetPosition(out PointD p, bool update)
169 {
170 var pos = core.getXYStagePosition();
171 p = new PointD(pos.x, pos.y);
172 }
173 public static class Objectives
174 {
178 public static int InstalledCount
179 {
180 get
181 {
182 return GetConfigs("ConfigGroup", "Objective").Length;
183 }
184 }
185 public static List<Objective> List = new List<Objective>();
186 public class Objective
187 {
188 public string Name { get; set; }
189 public int Index { get; set; }
190 public int Magnification { get; set; }
191 public Objective(string name, int index, int mag)
192 {
193 Name = name;
194 Index = index;
195 Magnification = mag;
196 }
197 }
198 internal static void Initialize()
199 {
200 foreach (Conf obj in GetConfigs("ConfigGroup", "Objective"))
201 {
202 List.Add(new Objective(obj.Values[0], int.Parse(obj.Values[3]), int.Parse(obj.Values[0].Replace("X", ""))));
203 }
204 }
205
206 public static Conf GetObjective(int index)
207 {
208 Conf[] cfs = GetConfigs("ConfigGroup", "Objective");
209 return cfs[index];
210 }
211 public static void SetPosition(int i)
212 {
213 core.setConfig(TurretName, List[i].Name);
214 }
215 public static int GetPosition()
216 {
217 string s = core.getCurrentConfig(TurretName);
218 int i = 0;
219 foreach(Objective obj in Objectives.List)
220 {
221 if (obj.Name == s)
222 return i;
223 }
224 return -1;
225 }
226 }
227 public static class Shutters
228 {
229 public class Shutter
230 {
231 public string Name { get; set; }
232 public Shutter(string name)
233 {
234 Name = name;
235 }
236 }
237 public static List<Shutter> List = new List<Shutter>();
238 public static void Initialize()
239 {
240 if (Config.ContainsKey("Device"))
241 {
242 //We get the name of the shutters.
243 foreach (Conf item in Config["Device"])
244 {
245 if (item.Values.Last().Contains("Shutter"))
246 {
247 List.Add(new Shutter(item.Type));
248 }
249 }
250 }
251 }
252 public static int GetPosition(string shutterName)
253 {
254 if (core.getShutterOpen())
255 return 1;
256 else
257 return 0;
258 }
259 public static void SetPosition(string shutterName, int state)
260 {
261 if(state == 1)
262 core.setShutterOpen(shutterName, true);
263 else
264 core.setShutterOpen(shutterName, false);
265 }
266 }
267
268
269 }
270}
AForge.RectangleD RectangleD
AForge.PointD PointD
AForge.PixelFormat PixelFormat
BioLib.BioImage BioImage
Definition TabsView.cs:15
AForge.ZCT ZCT
Definition TabsView.cs:17
Objective(string name, int index, int mag)
static List< Objective > List
static Conf GetObjective(int index)
static int InstalledCount
Number of Installed Objectives.
static int GetPosition(string shutterName)
static void SetPosition(string shutterName, int state)
static List< Shutter > List
static void SetExposure(float f)
static void SetPosition(PointD p)
static void SetPosition(Point3D p)
static bool Initialize(string config)
static string TurretName
static MMStudio studio
static PointD location
static void GetPosition(out PointD p, bool update)
static Dictionary< string, List< Conf > > Config
static Conf[] GetConfigs(string Class, string Type)
static mmcorej.CMMCore core
static void GetPosition3D(out Point3D p, bool update)
static BioImage TakeImage(string name)
static RectangleD GetViewRectangle(bool update)
static Point3D GetPosition()
override string ToString()
Conf(string Class, string Type, string[] Values)