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
CellImager.cs
Go to the documentation of this file.
1using AForge.Imaging.Filters;
2using AForge.Imaging;
3using AForge.Math.Geometry;
4using AForge;
5using BioImager;
6using System;
7using System.Collections.Generic;
8using System.ComponentModel;
9using System.Data;
10using System.Drawing;
11using System.Linq;
12using System.Text;
13using System.Threading.Tasks;
14using System.Windows.Forms;
15using BioLib;
16namespace BioImager
17{
18 public partial class CellImager : Form
19 {
20 ImageView viewer;
21 List<Blob> blobs = new List<Blob>();
22 List<BioImage> images = new List<BioImage>();
23 BioImage b;
24 System.Drawing.Bitmap bitmap;
25 public CellImager()
26 {
27 InitializeComponent();
28 viewer = new ImageView();
29 viewer.ShowControls = false;
30 imageSplitContainer.Panel2.Controls.Add(viewer);
31 foreach (var item in Microscope.Objectives.List)
32 {
33 objectivesBox.Items.Add(item.Name);
34 }
35 objectivesBox.SelectedIndex = Microscope.Objectives.Index;
36 maxWidthLabel.Text = "Max Width:" + maxWidthBar.Value;
37 maxHeightLabel.Text = "Max Height:" + maxHeightBar.Value;
38 minHeightLabel.Text = "Min Height:" + minHeightBar.Value;
39 minWidthLabel.Text = "Min Width:" + minWidthBar.Value;
40 foreach (Scripting.Script item in Scripting.Scripts.Values)
41 {
42 comboBox.Items.Add(item);
43 }
44 }
45
46 private void maxWidthBar_Scroll(object sender, EventArgs e)
47 {
48 maxWidthLabel.Text = "Max Width:" + maxWidthBar.Value;
49 }
50
51 private void maxHeightBar_Scroll(object sender, EventArgs e)
52 {
53 maxHeightLabel.Text = "Max Height:" + maxHeightBar.Value;
54 }
55
56 private void minHeightBar_Scroll(object sender, EventArgs e)
57 {
58 minHeightLabel.Text = "Min Height:" + minHeightBar.Value;
59 }
60
61 private void sizeBar_Scroll(object sender, EventArgs e)
62 {
63 minWidthLabel.Text = "Min Width:" + minWidthBar.Value;
64 }
65
66 private void thresholdBar_Scroll(object sender, EventArgs e)
67 {
68 thresholdLabel.Text = "Threshold:" + thresholdBar.Value;
69 }
70
71
72
73 private void helpToolStripMenuItem_Click(object sender, EventArgs e)
74 {
75 MessageBox.Show("Cell Imager allows for performing imaging routines on individual cells based on blob detection." +
76 " Set threshold and size range for detection. Also allows for an imaging script to be run on each cell.");
77 }
78
79 private void detectBut_Click(object sender, EventArgs e)
80 {
81 b = Microscope.TakeImage(false, false, false);
82 bitmap = new System.Drawing.Bitmap(b.SizeX, b.SizeY);
83
84 Statistics[] sts = Statistics.FromBytes(b.Buffers[0]);
85 Statistics st = sts[0];
86 Threshold th;
87 if (numericRBut.Checked)
88 {
89 th = new Threshold(thresholdBar.Value);
90 }
91 else
92 th = new Threshold((int)(st.Median));
93 AForge.Bitmap bf = b.GetBitmap(0, (int)chanBox.Value - 1, 0);
94 if (bf.RGBChannelsCount > 1)
95 {
96 ExtractChannel ch = new ExtractChannel((short)(rgbBox.Value - 1));
97 bf = ch.Apply(bf);
98 }
99 th.ApplyInPlace(bf);
100 Invert inv = new Invert();
101 inv.ApplyInPlace(bf);
102 AForge.Bitmap det;
103 if (bf.BitsPerPixel > 8)
104 det = AForge.Imaging.Image.Convert16bppTo8bpp(bf);
105 else
106 det = bf;
107 pictureBox.Image = new System.Drawing.Bitmap(det.Width, det.Height, det.Width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, det.GetRGBData());
108 BlobCounter blobCounter = new BlobCounter();
109 blobCounter.ProcessImage(det);
110 Blob[] bls = blobCounter.GetObjectsInformation();
111 blobs.Clear();
112 foreach (Blob blob in bls)
113 {
114 if (blob.Rectangle.Width < minWidthBar.Value || blob.Rectangle.Height < minHeightBar.Value || blob.Rectangle.Width > maxWidthBar.Value || blob.Rectangle.Height > maxHeightBar.Value)
115 continue;
116 blobs.Add(blob);
117 }
118 UpdateItems();
119 }
120
121 private void UpdateItems()
122 {
123 listView.Clear();
124 foreach (Blob blob in blobs)
125 {
126 ListViewItem item = new ListViewItem(blob.Rectangle.Width + "," + blob.Rectangle.Height);
127 item.Tag = blob;
128 listView.Items.Add(item);
129 }
130 }
131
132 public void Image(Blob[] blobs)
133 {
134 Point3D p = Microscope.GetPosition();
136 foreach (Blob blob in blobs)
137 {
138 if (blob.Rectangle.Width < minWidthBar.Value || blob.Rectangle.Height < minHeightBar.Value || blob.Rectangle.Width > maxWidthBar.Value || blob.Rectangle.Height > maxHeightBar.Value)
139 continue;
140 PointD loc = new PointD(b.StageSizeX + (blob.Rectangle.X * b.PhysicalSizeX), b.StageSizeY + (blob.Rectangle.Y * b.PhysicalSizeY));
141 Microscope.Objectives.SetPosition(objectivesBox.SelectedIndex);
143 if (tileBox.Checked)
144 {
145 for (int y = 0; y < tileYBox.Value; y++)
146 {
147 for (int x = 0; x < tileXBox.Value; x++)
148 {
149 if (comboBox.SelectedIndex != -1)
150 ((Scripting.Script)comboBox.SelectedItem).Run();
151 else
152 images.AddRange(Microscope.TakeTiles((int)tileXBox.Value, (int)tileYBox.Value));
153 }
154 }
155 }
156 else
157 {
158 if (comboBox.SelectedIndex != -1)
159 ((Scripting.Script)comboBox.SelectedItem).Run();
160 else
161 images.Add(Microscope.TakeImage(false, false, false));
162 }
163 }
166 viewer.Images.Clear();
167 viewer.Images.AddRange(images);
168 viewer.UpdateView();
169 }
170
171 private void clearBut_Click(object sender, EventArgs e)
172 {
173 viewer.RemoveImages();
174 }
175
176 private void pictureBox_Paint(object sender, PaintEventArgs e)
177 {
178 if (bitmap == null)
179 return;
180 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
181 int i = 0;
182 Pen bb = new Pen(Brushes.Blue, 2);
183 Pen r = new Pen(Brushes.Red, 2);
184 foreach (Blob b in blobs)
185 {
186 if (listView.Items[i].Selected)
187 g.DrawRectangle(bb, new System.Drawing.Rectangle(b.Rectangle.X, b.Rectangle.Y, b.Rectangle.Width, b.Rectangle.Height));
188 else
189 g.DrawRectangle(r, new System.Drawing.Rectangle(b.Rectangle.X, b.Rectangle.Y, b.Rectangle.Width, b.Rectangle.Height));
190 i++;
191 }
192 bb.Dispose();
193 r.Dispose();
194 g.Dispose();
195 e.Graphics.DrawImage(bitmap, 0, 0, pictureBox.Width, pictureBox.Height);
196 }
197
198 private void imageAllBut_Click(object sender, EventArgs e)
199 {
200 Image(blobs.ToArray());
201 }
202
203 private void imageCurBut_Click(object sender, EventArgs e)
204 {
205 List<Blob> blobs = new List<Blob>();
206 foreach (ListViewItem blob in listView.Items)
207 {
208 if (blob.Selected)
209 blobs.Add((Blob)blob.Tag);
210 }
211 Image(blobs.ToArray());
212 }
213
214 private void listView_SelectedIndexChanged(object sender, EventArgs e)
215 {
216 pictureBox.Invalidate();
217 }
218
219 private void CellImager_FormClosing(object sender, FormClosingEventArgs e)
220 {
221 this.Hide();
222 e.Cancel = true;
223 }
224 }
225}
System.Drawing.Image Image
Definition ImageView.cs:16
AForge.PointD PointD
BioLib.BioImage BioImage
Definition TabsView.cs:15
void Image(Blob[] blobs)
ImageView control for image stacks, pyramidal and whole-slide-images.
List< BioImage > Images
Definition ImageView.cs:323
void UpdateView(bool refreshImages=true)
static Objectives Objectives
static BioImage[] TakeTiles(int width, int height, bool save=true, bool addToImages=true, bool newTab=false)
static void SetPosition(Point3D p)
static Point3D GetPosition()
static BioImage TakeImage()
List< Objective > List
void SetPosition(int index)
static Dictionary< string, Script > Scripts
Definition Scripting.cs:15