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
HistogramControl.cs
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using AForge;
11using Bitmap = System.Drawing.Bitmap;
12using Color = System.Drawing.Color;
13using PointF = System.Drawing.PointF;
14using Size = System.Drawing.Size;
15using SizeF = System.Drawing.SizeF;
16using RectangleF = System.Drawing.RectangleF;
17using RectangleD = AForge.RectangleD;
18using Rectangle = System.Drawing.Rectangle;
19using Point = System.Drawing.Point;
20namespace BioImager
21{
22 public partial class HistogramControl : UserControl
23 {
24 public HistogramControl(Channel c)
25 {
26 channel = c;
27 this.Dock = DockStyle.Fill;
28 InitializeComponent();
29 if (c.BitsPerPixel == 8)
30 {
31 graphMax = 255;
32 Bin = 1;
33 }
34 else
35 {
36 graphMax = ushort.MaxValue;
37 Bin = 10;
38 }
39
40 }
41 private Channel channel = null;
42 private float bin = 10;
43 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
44 public float Bin
45 {
46 get
47 {
48 return bin;
49 }
50 set
51 {
52 bin = value;
53 }
54 }
55 private int min = 0;
56 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
57 public float Min
58 {
59 get { return min; }
60 set
61 {
62 if(channel!=null)
63 channel.range[App.channelsTool.SelectedSample].Min = (int)value;
64 min = (int)value;
65 }
66 }
67 private int max = 0;
68 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
69 public float Max
70 {
71 get { return max; }
72 set
73 {
74 if (channel != null)
75 channel.range[App.channelsTool.SelectedSample].Max = (int)value;
76 max = (int)value;
77 }
78 }
79 private int graphMax = ushort.MaxValue;
80 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
81 public int GraphMax
82 {
83 get { return graphMax; }
84 set { graphMax = value; }
85 }
86 private int graphMin = 0;
87 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
88 public int GraphMin
89 {
90 get { return graphMin; }
91 set { graphMin = value; }
92 }
93 private bool stackHistogram = true;
94 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
95 public bool StackHistogram
96 {
97 get
98 {
99 return stackHistogram;
100 }
101 set
102 {
103 stackHistogram = value;
104 }
105 }
106 private int mouseX = 0;
107 private int mouseY = 0;
108 public int MouseX
109 {
110 get
111 {
112 return mouseX;
113 }
114 }
115 public int MouseY
116 {
117 get
118 {
119 return mouseX;
120 }
121 }
122 private float mouseValX = 0;
123 private float mouseValY = 0;
124 public float MouseValX
125 {
126 get
127 {
128 return mouseValX;
129 }
130 }
131 public float MouseValY
132 {
133 get
134 {
135 return mouseValY;
136 }
137 }
138
139 private bool axisNumbers = true;
140 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
141 public bool AxisNumbers
142 {
143 get { return axisNumbers;}
144 set { axisNumbers = value; }
145 }
146
147 private bool axisTicks = true;
148 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
149 public bool AxisTicks
150 {
151 get { return axisTicks; }
152 set { axisTicks = value; }
153 }
154
155 private float fx = 0;
156 private float fy = 0;
157 private Bitmap bm;
158 private System.Drawing.Graphics g;
159
163 public void UpdateChannel(Channel c)
164 {
165 channel = c;
166 }
168 public void UpdateView()
169 {
170 this.Invalidate();
171 }
176 private void HistogramControl_Paint(object sender, PaintEventArgs e)
177 {
178 if (graphMax == 0)
179 graphMax = ushort.MaxValue;
180 g.Clear(Color.LightGray);
181 g.ResetTransform();
182 g.TranslateTransform(-graphMin, 0);
183 string st = "";
184 fx = ((float)this.Width) / ((float)graphMax);
185 float maxmedian = 0;
186 int maxChan = 0;
187 for (int cc = 0; cc < ImageView.SelectedImage.Channels.Count; cc++)
188 {
189 for (int i = 0; i < ImageView.SelectedImage.Channels[cc].stats.Length; i++)
190 {
191 float f = ImageView.SelectedImage.Channels[cc].stats[i].StackMedian;
192 if (maxmedian < f)
193 {
194 maxmedian = f;
195 maxChan = cc;
196 }
197 }
198 }
199 fy = ((float)this.Height) / maxmedian;
200 for (int c = 0; c < ImageView.SelectedImage.Channels.Count; c++)
201 {
202 Channel channel = ImageView.SelectedImage.Channels[c];
203 for (int i = 0; i < channel.range.Length; i++)
204 {
205 Statistics stat;
206 if (ImageView.SelectedImage.RGBChannelCount == 1)
207 stat = channel.stats[0];
208 else
209 stat = channel.stats[i];
210
211 Pen black = new Pen(Color.FromArgb(35, 0, 0, 0), bin * fx);
212 Pen blackd = new Pen(Color.FromArgb(150, 0, 0, 0), bin * fx);
213 Pen pen = null;
214 Pen pend = null;
215 int dark = 200;
216 int light = 50;
217 if (channel.Emission != 0)
218 {
219 pen = new Pen(SpectralColor(channel.Emission), bin * fx);
220 pen.Color = Color.FromArgb(light, pen.Color);
221 pend = new Pen(SpectralColor(channel.Emission), bin * fx);
222 pend.Color = Color.FromArgb(dark, pen.Color);
223 }
224 else
225 {
226 if (i == 0)
227 {
228 pen = new Pen(Color.FromArgb(light, 255, 0, 0), bin * fx);
229 pend = new Pen(Color.FromArgb(dark, 255, 0, 0), bin * fx);
230 }
231 else if (i == 1)
232 {
233 pen = new Pen(Color.FromArgb(light, 0, 255, 0), bin * fx);
234 pend = new Pen(Color.FromArgb(dark, 0, 255, 0), bin * fx);
235 }
236 else
237 {
238 pen = new Pen(Color.FromArgb(light, 0, 0, 255), bin * fx);
239 pend = new Pen(Color.FromArgb(dark, 0, 0, 255), bin * fx);
240 }
241 }
242
243 g.DrawLine(Pens.Black, new PointF(mouseX, 0), new PointF(mouseX, this.Height));
244 int gmax = graphMax;
245 if (App.Image.bitsPerPixel <= 8)
246 gmax = 255;
247
248
249 float sumbins = 0;
250 float sumbin = 0;
251 int binind = 0;
252 int bininds = 0;
253 PointF? prevs = null;
254 PointF? prev = null;
255 for (float x = 0; x < gmax; x++)
256 {
257 if (StackHistogram && c == ImageView.SelectedImage.Channels.Count - 1)
258 {
259 //Lets draw the stack histogram.
260 float val = (float)ImageView.SelectedImage.Statistics.StackValues[(int)x];
261 sumbin += val;
262 if (binind == bin)
263 {
264 float v = sumbin / binind;
265 float yy = this.Height - (fy * v);
266 if (prevs != null)
267 {
268 g.DrawLine(blackd, prevs.Value, new PointF(fx * x, yy));
269 }
270 g.DrawLine(black, new PointF(fx * x, this.Height), new PointF(fx * x, yy));
271 prevs = new PointF(fx * x, yy);
272 binind = 0;
273 sumbin = 0;
274 }
275 }
276 //Lets draw the channel histogram on top of the stack histogram.
277 float rv = stat.StackValues[(int)x];
278 sumbins += rv;
279 if (bininds == bin)
280 {
281 g.DrawLine(pen, new PointF(fx * x, this.Height), new PointF(fx * x, this.Height - (fy * (sumbins / bininds))));
282 if (prev != null)
283 {
284 g.DrawLine(pend, prev.Value, new PointF(fx * x, this.Height - (fy * (sumbins / bininds))));
285 }
286 prev = new PointF(fx * x, this.Height - (fy * (sumbins / bininds)));
287 bininds = 0;
288 sumbins = 0;
289 }
290 binind++;
291 bininds++;
292
293 }
294
295 g.DrawLine(pend, new PointF((fx * channel.range[i].Max), 0), new PointF((fx * channel.range[i].Max), this.Height));
296 g.DrawLine(pend, new PointF(fx * channel.range[i].Min, 0), new PointF(fx * channel.range[i].Min, this.Height));
297
298 black.Dispose();
299 blackd.Dispose();
300 pen.Dispose();
301 pend.Dispose();
302 }
303
304 }
305
306 float tick = 6;
307 if (axisTicks)
308 {
309 if (axisNumbers)
310 {
311 for (float x = 0; x < graphMax; x += 2000)
312 {
313 SizeF s = g.MeasureString(x.ToString(), SystemFonts.DefaultFont);
314 g.DrawString(x.ToString(), SystemFonts.DefaultFont, Brushes.Black, (fx * x) - (s.Width/2), tick + 6);
315 g.DrawLine(Pens.Black, new PointF((fx * x), 0), new PointF((fx * x), tick + 3));
316 }
317 for (float x = 0; x < graphMax; x += 1000)
318 {
319 g.DrawLine(Pens.Black, new PointF((fx * x), 0), new PointF((fx * x), tick));
320 }
321 }
322 if (graphMax <= 16383)
323 for (float x = 0; x < graphMax; x += 100)
324 {
325 g.DrawLine(Pens.Black, new PointF((fx * x), 0), new PointF((fx * x), tick));
326 }
327 if (graphMax <= 255)
328 {
329 for (float x = 0; x < graphMax; x += 50)
330 {
331 g.DrawLine(Pens.Black, new PointF((fx * x), 0), new PointF((fx * x), 4));
332 }
333 for (float x = 0; x < graphMax; x += 10)
334 {
335 g.DrawLine(Pens.Black, new PointF((fx * x), 0), new PointF((fx * x), 2));
336 }
337 }
338 }
339
340 if (ImageView.SelectedImage.RGBChannelCount == 3)
341 {
342 if (ImageView.SelectedImage.Channels.Count > 2)
343 {
344 st =
345 "(" + (mouseX / fx).ToString() +
346 ",R:" + ImageView.SelectedImage.Channels[2].stats[2].StackValues[(int)(mouseX / fx)].ToString() +
347 ",G:" + ImageView.SelectedImage.Channels[1].stats[1].StackValues[(int)(mouseX / fx)].ToString() +
348 ",B:" + ImageView.SelectedImage.Channels[0].stats[0].StackValues[(int)(mouseX / fx)].ToString() + ")";
349 }
350 else
351 {
352 st =
353 "(" + (mouseX / fx).ToString() +
354 ",R:" + ImageView.SelectedImage.Channels[0].stats[2].StackValues[(int)(mouseX / fx)].ToString() +
355 ",G:" + ImageView.SelectedImage.Channels[0].stats[1].StackValues[(int)(mouseX / fx)].ToString() +
356 ",B:" + ImageView.SelectedImage.Channels[0].stats[0].StackValues[(int)(mouseX / fx)].ToString() + ")";
357 }
358 }
359 else
360 {
361 int x = (int)(mouseX / fx);
362 if (ImageView.SelectedImage.bitsPerPixel < 8 && ImageView.SelectedImage.Channels[0].stats[0].StackValues.Length < 255)
363 st = "(" + (mouseX / fx).ToString() + "," + ImageView.SelectedImage.Channels[0].stats[0].StackValues[x].ToString() + ")";
364
365 }
366 SizeF sf = g.MeasureString(st, SystemFonts.DefaultFont);
367 g.DrawString(st, SystemFonts.DefaultFont, Brushes.Black, mouseX, mouseY + sf.Height);
368
369 e.Graphics.DrawImage(bm, 0, 0);
370 }
371
376 private void HistogramControl_MouseDown(object sender, MouseEventArgs e)
377 {
378 mouseX = e.X;
379 mouseY = e.Y;
380 Invalidate();
381 }
382
388 private void HistogramControl_MouseMove(object sender, MouseEventArgs e)
389 {
390 mouseValX = e.X / fx;
391 mouseValY = e.Y / fy;
392 }
393
398 private void HistogramControl_SizeChanged(object sender, EventArgs e)
399 {
400 InitGraphics();
401 }
406 private void InitGraphics()
407 {
408 if (Width == 0 || Height == 0)
409 return;
410 bm = new Bitmap(Width, Height);
411 if (g != null)
412 g.Dispose();
413 g = System.Drawing.Graphics.FromImage(bm);
414 }
415
420 private void copyViewToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
421 {
422 Clipboard.SetImage(bm);
423 }
424
430 private void setMaxToolStripMenuItem_Click(object sender, EventArgs e)
431 {
432 App.channelsTool.SelectedChannel.range[App.channelsTool.SelectedSample].Max = (int)MouseValX;
433 Invalidate();
434 App.viewer.UpdateImage();
435 }
436
442 private void setMinToolStripMenuItem_Click(object sender, EventArgs e)
443 {
444 App.channelsTool.SelectedChannel.range[App.channelsTool.SelectedSample].Min = (int)MouseValX;
445 Invalidate();
446 App.viewer.UpdateImage();
447 }
448
453 private void setMaxAllToolStripMenuItem_Click(object sender, EventArgs e)
454 {
455 foreach (Channel c in ImageView.SelectedImage.Channels)
456 {
457 for (int i = 0; i < c.range.Length; i++)
458 {
459 c.range[i].Max = (int)MouseValX;
460 }
461 }
462 }
463
468 private void setMinAllToolStripMenuItem_Click(object sender, EventArgs e)
469 {
470 foreach (Channel c in ImageView.SelectedImage.Channels)
471 {
472 for (int i = 0; i < c.range.Length; i++)
473 {
474 c.range[i].Min = (int)MouseValX;
475 }
476 }
477 }
483 Color SpectralColor(double l) // RGB <0,1> <- lambda l <400,700> [nm]
484 {
485 double t;
486 double r = 0;
487 double g = 0;
488 double b = 0;
489 if ((l >= 400.0) && (l < 410.0)) { t = (l - 400.0) / (410.0 - 400.0); r = +(0.33 * t) - (0.20 * t * t); }
490 else if ((l >= 410.0) && (l < 475.0)) { t = (l - 410.0) / (475.0 - 410.0); r = 0.14 - (0.13 * t * t); }
491 else if ((l >= 545.0) && (l < 595.0)) { t = (l - 545.0) / (595.0 - 545.0); r = +(1.98 * t) - (t * t); }
492 else if ((l >= 595.0) && (l < 650.0)) { t = (l - 595.0) / (650.0 - 595.0); r = 0.98 + (0.06 * t) - (0.40 * t * t); }
493 else if ((l >= 650.0) && (l < 700.0)) { t = (l - 650.0) / (700.0 - 650.0); r = 0.65 - (0.84 * t) + (0.20 * t * t); }
494 if ((l >= 415.0) && (l < 475.0)) { t = (l - 415.0) / (475.0 - 415.0); g = +(0.80 * t * t); }
495 else if ((l >= 475.0) && (l < 590.0)) { t = (l - 475.0) / (590.0 - 475.0); g = 0.8 + (0.76 * t) - (0.80 * t * t); }
496 else if ((l >= 585.0) && (l < 639.0)) { t = (l - 585.0) / (639.0 - 585.0); g = 0.84 - (0.84 * t); }
497 if ((l >= 400.0) && (l < 475.0)) { t = (l - 400.0) / (475.0 - 400.0); b = +(2.20 * t) - (1.50 * t * t); }
498 else if ((l >= 475.0) && (l < 560.0)) { t = (l - 475.0) / (560.0 - 475.0); b = 0.7 - (t) + (0.30 * t * t); }
499 r *= 255;
500 g *= 255;
501 b *= 255;
502 return Color.FromArgb(255, (int)r, (int)g, (int)b);
503 }
504 }
505}
ImageView control for image stacks, pyramidal and whole-slide-images.