BioGTK  6.5.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
ROIManager.cs
1using AForge;
2using BioGTK;
3using CSScripting;
4using Gtk;
5using System;
6using System.Collections.Generic;
7using System.ComponentModel;
8using System.Data;
9using System.IO;
10using System.Linq;
11using System.Text;
12using System.Threading.Tasks;
13
14namespace Bio
15{
16 public partial class ROIManager : Window
17 {
18 #region Properties
19
21 private Builder _builder;
22
23#pragma warning disable 649
24 [Builder.Object]
25 private TreeView roiView;
26 [Builder.Object]
27 private SpinButton xBox;
28 [Builder.Object]
29 private SpinButton yBox;
30 [Builder.Object]
31 private SpinButton wBox;
32 [Builder.Object]
33 private SpinButton hBox;
34 [Builder.Object]
35 private SpinButton zBox;
36 [Builder.Object]
37 private SpinButton cBox;
38 [Builder.Object]
39 private SpinButton tBox;
40 [Builder.Object]
41 private SpinButton widthBox;
42 [Builder.Object]
43 private SpinButton rBox;
44 [Builder.Object]
45 private SpinButton gBox;
46 [Builder.Object]
47 private SpinButton bBox;
48 [Builder.Object]
49 private Label typeLabel;
50 [Builder.Object]
51 private Entry textBox;
52 [Builder.Object]
53 private Entry idBox;
54 [Builder.Object]
55 private SpinButton pointBox;
56 [Builder.Object]
57 private SpinButton pointXBox;
58 [Builder.Object]
59 private SpinButton pointYBox;
60 [Builder.Object]
61 private SpinButton selBox;
62 [Builder.Object]
63 private FontButton fontBut;
64 [Builder.Object]
65 private CheckButton boundsBox;
66 [Builder.Object]
67 private CheckButton showTextBox;
68 [Builder.Object]
69 private CheckButton showRBox;
70 [Builder.Object]
71 private CheckButton showGBox;
72 [Builder.Object]
73 private CheckButton showBBox;
74 [Builder.Object]
75 private Label imageNameLabel;
76 [Builder.Object]
77 private CheckButton showMasksBox;
78 [Builder.Object]
79 private Menu menu;
80 [Builder.Object]
81 private MenuItem menuDelete;
82 [Builder.Object]
83 private MenuItem menuCopy;
84 [Builder.Object]
85 private MenuItem menuPaste;
86#pragma warning restore 649
87
88 #endregion
89 private static Random rng = new Random();
90 #region Constructors / Destructors
91 public BioImage image;
92 private static Dictionary<string, ROI> rts = new Dictionary<string, ROI>();
93 public static Dictionary<string, ROI> rois
94 {
95 get
96 {
97 foreach (var item in ImageView.SelectedImage.Annotations)
98 {
99 if(!rts.ContainsKey(item.id))
100 {
101 if (item.id == "" || item.id == null)
102 item.id = rng.Next(0,999999).ToString();
103 rts.Add(item.id, item);
104 }
105 }
106 return rts;
107 }
108 set
109 {
110 rts = value;
111 }
112 }
113 public static ROIManager Create()
114 {
115 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/ROIManager.glade", FileMode.Open));
116 return new ROIManager(builder, builder.GetObject("roiManager").Handle);
117 }
118 /* The above code is initializing the ROIManager class. */
119 protected ROIManager(Builder builder, IntPtr handle) : base(handle)
120 {
121 _builder = builder;
122 builder.Autoconnect(this);
123 xBox.ValueChanged += xBox_ValueChanged;
124 yBox.ValueChanged += yBox_ValueChanged;
125 wBox.ValueChanged += wBox_ValueChanged;
126 hBox.ValueChanged += hBox_ValueChanged;
127 rBox.ValueChanged += rBox_ValueChanged;
128 gBox.ValueChanged += gBox_ValueChanged;
129 bBox.ValueChanged += bBox_ValueChanged;
130 zBox.ValueChanged += zBox_ValueChanged;
131 cBox.ValueChanged += cBox_ValueChanged;
132 tBox.ValueChanged += tBox_ValueChanged;
133 widthBox.ValueChanged += strokeWBox_ValueChanged;
134 pointXBox.ValueChanged += pointXBox_ValueChanged;
135 pointYBox.ValueChanged += pointYBox_ValueChanged;
136 pointBox.ValueChanged += pointBox_ValueChanged;
137 selBox.ValueChanged += selectBoxSize_ValueChanged;
138 widthBox.ValueChanged += strokeWBox_ValueChanged;
139 textBox.Changed += textBox_TextChanged;
140 idBox.Changed += idBox_TextChanged;
141 boundsBox.Clicked += showBoundsBox_ActiveChanged;
142 showTextBox.Clicked += showTextBox_ActiveChanged;
143 showRBox.Clicked += showRBox_ActiveChanged;
144 showGBox.Clicked += showGBox_ActiveChanged;
145 showBBox.Clicked += showBBox_ActiveChanged;
146 showMasksBox.Clicked += ShowMasksBox_Clicked;
147 this.DeleteEvent += ROIManager_DeleteEvent;
148 menuDelete.ButtonPressEvent += MenuDelete_ButtonPressEvent;
149 menuCopy.ButtonPressEvent += MenuCopy_ButtonPressEvent;
150 menuPaste.ButtonPressEvent += MenuPaste_ButtonPressEvent;
151
152 //roiView.Selection.Changed += roiView_SelectedIndexChanged;
153 roiView.RowActivated += RoiView_RowActivated;
154 this.FocusInEvent += ROIManager_FocusInEvent;
155 this.FocusActivated += ROIManager_Activated;
156 roiView.ButtonPressEvent += RoiView_ButtonPressEvent;
157
158 xBox.Adjustment.Upper = PointD.MaxX;
159 xBox.Adjustment.StepIncrement= 0.1;
160 xBox.Adjustment.PageIncrement= 1;
161 yBox.Adjustment.Upper = PointD.MaxY;
162 yBox.Adjustment.StepIncrement = 0.1;
163 yBox.Adjustment.PageIncrement = 1;
164 wBox.Adjustment.Upper = PointD.MaxX;
165 wBox.Adjustment.StepIncrement = 0.1;
166 wBox.Adjustment.PageIncrement = 1;
167 hBox.Adjustment.Upper = PointD.MaxY;
168 xBox.Adjustment.StepIncrement = 0.1;
169 xBox.Adjustment.PageIncrement = 1;
170 rBox.Adjustment.Upper = byte.MaxValue;
171 rBox.Adjustment.StepIncrement = 1;
172 rBox.Adjustment.PageIncrement = 1;
173 gBox.Adjustment.Upper = byte.MaxValue;
174 gBox.Adjustment.StepIncrement = 1;
175 gBox.Adjustment.PageIncrement = 1;
176 bBox.Adjustment.Upper = byte.MaxValue;
177 bBox.Adjustment.StepIncrement = 1;
178 bBox.Adjustment.PageIncrement = 1;
179 zBox.Adjustment.Upper = 10000;
180 zBox.Adjustment.StepIncrement = 1;
181 zBox.Adjustment.PageIncrement = 1;
182 cBox.Adjustment.Upper = 10000;
183 cBox.Adjustment.StepIncrement = 1;
184 cBox.Adjustment.PageIncrement = 1;
185 tBox.Adjustment.Upper = 10000;
186 tBox.Adjustment.StepIncrement = 1;
187 tBox.Adjustment.PageIncrement = 1;
188 widthBox.Adjustment.Upper = 100;
189 widthBox.Adjustment.StepIncrement = 1;
190 widthBox.Adjustment.PageIncrement = 1;
191 pointBox.Adjustment.Upper = 100000;
192 pointBox.Adjustment.StepIncrement = 1;
193 pointBox.Adjustment.PageIncrement = 1;
194 pointXBox.Adjustment.Upper = PointD.MaxX;
195 pointXBox.Adjustment.StepIncrement = 0.1;
196 pointXBox.Adjustment.PageIncrement = 1;
197 pointYBox.Adjustment.Upper = PointD.MaxY;
198 pointYBox.Adjustment.StepIncrement = 0.1;
199 pointYBox.Adjustment.PageIncrement = 1;
200 selBox.Value = ROI.selectBoxSize;
201 selBox.Adjustment.Upper = 100;
202 selBox.Adjustment.StepIncrement = 1;
203 roiView.ActivateOnSingleClick = true;
204 InitItems();
205 App.ApplyStyles(this);
206 }
207
208 private void RoiView_ButtonPressEvent(object o, ButtonPressEventArgs args)
209 {
210 if (args.Event.Button == 3)
211 menu.Popup();
212 }
213
214 private List<ROI> copys = new List<ROI>();
216 public void CopySelection()
217 {
218 copys.Clear();
219 string s = "";
220 List<ROI> rois = new List<ROI>();
221 rois.AddRange(ImageView.SelectedImage.AnnotationsR);
222 rois.AddRange(ImageView.SelectedImage.AnnotationsG);
223 rois.AddRange(ImageView.SelectedImage.AnnotationsB);
224 foreach (ROI item in rois)
225 {
226 if (item.Selected)
227 {
228 copys.Add(item);
229 s += BioImage.ROIToString(item);
230 }
231 }
232 Clipboard clipboard = Clipboard.Get(Gdk.Selection.Clipboard);
233 clipboard.Text = s;
234 }
237 public void PasteSelection()
238 {
239 Clipboard clipboard = Clipboard.Get(Gdk.Selection.Clipboard);
240 string text = clipboard.WaitForText();
241 string[] sts = text.Split(BioImage.NewLine);
242 foreach (string line in sts)
243 {
244 if (line.Length > 8)
245 {
246 ROI an = BioImage.StringToROI(line);
247 //We set the coordinates of the ROI's we are pasting
248 an.coord = App.viewer.GetCoordinate();
249 ImageView.SelectedImage.Annotations.Add(an);
250 }
251 }
252 UpdateView();
253 }
254
255
256 private void MenuPaste_ButtonPressEvent(object o, ButtonPressEventArgs args)
257 {
259 }
260
261 private void MenuCopy_ButtonPressEvent(object o, ButtonPressEventArgs args)
262 {
264 }
265
266 private void MenuDelete_ButtonPressEvent(object o, ButtonPressEventArgs args)
267 {
268 if (anno != null && ImageView.SelectedImage != null)
269 {
270 // Remove the selected annotation
271 ImageView.SelectedImage.Annotations.Remove(anno);
273 }
274 }
275
276 private void ShowMasksBox_Clicked(object sender, EventArgs e)
277 {
278 App.viewer.ShowMasks = showMasksBox.Active;
279 UpdateView();
280 }
281
282 private void ROIManager_DeleteEvent(object o, DeleteEventArgs args)
283 {
284 args.RetVal = true;
285 Hide();
286 }
287
292
293 private void ROIManager_FocusInEvent(object o, FocusInEventArgs args)
294 {
296 }
297
298
299 #endregion
300
301 public ROI anno = new ROI();
302
311 private void RoiView_RowActivated(object o, RowActivatedArgs args)
312 {
313 TreeIter iter;
314 if (roiView.Model.GetIter(out iter, args.Path))
315 {
316 string type = (string)roiView.Model.GetValue(iter, 0);
317 ROI.Type t;
318 if (Enum.TryParse<ROI.Type>(type,out t))
319 {
320 string id = (string)roiView.Model.GetValue(iter, 1);
321 string text = (string)roiView.Model.GetValue(iter, 2);
322 string bounds = (string)roiView.Model.GetValue(iter, 3);
323 if (rois.ContainsKey(id))
324 {
325 anno = rois[id];
326 if (App.viewer != null)
327 App.viewer.SetCoordinate(anno.coord.Z, anno.coord.C, anno.coord.T);
328 xBox.Value = anno.X;
329 yBox.Value = anno.Y;
330 wBox.Value = anno.W;
331 hBox.Value = anno.H;
332 zBox.Value = anno.coord.Z;
333 cBox.Value = anno.coord.C;
334 tBox.Value = anno.coord.T;
335 rBox.Value = anno.strokeColor.R;
336 gBox.Value = anno.strokeColor.G;
337 bBox.Value = anno.strokeColor.B;
338 widthBox.Value = anno.strokeWidth;
339 idBox.Text = anno.id;
340 textBox.Text = anno.Text;
341 typeLabel.Text = anno.type.ToString();
342 imageNameLabel.Text = ImageView.SelectedImage.Filename;
344 if (ImageView.SelectedImage.isPyramidal)
345 {
346 App.viewer.PyramidalOrigin = new PointD((int)(anno.X), (int)(anno.Y));
347 App.viewer.UpdateView(true);
348 }
349 }
350 else return;
351 }
352 else return;
353 }
354 else
355 return;
356 }
359 public void InitItems()
360 {
361 Gtk.TreeViewColumn typeCol = new Gtk.TreeViewColumn();
362 typeCol.Title = "Type";
363 Gtk.CellRendererText typeCell = new Gtk.CellRendererText();
364 typeCol.PackStart(typeCell, true);
365
366 Gtk.TreeViewColumn idCol = new Gtk.TreeViewColumn();
367 idCol.Title = "ID";
368 Gtk.CellRendererText idCell = new Gtk.CellRendererText();
369 idCol.PackStart(idCell, true);
370
371 Gtk.TreeViewColumn textCol = new Gtk.TreeViewColumn();
372 textCol.Title = "Text";
373 Gtk.CellRendererText textCell = new Gtk.CellRendererText();
374 textCol.PackStart(textCell, true);
375
376 Gtk.TreeViewColumn rectCol = new Gtk.TreeViewColumn();
377 rectCol.Title = "Bounds";
378 Gtk.CellRendererText rectCell = new Gtk.CellRendererText();
379 rectCol.PackStart(rectCell, true);
380
381
382 roiView.AppendColumn(typeCol);
383 roiView.AppendColumn(idCol);
384 roiView.AppendColumn(textCol);
385 roiView.AppendColumn(rectCol);
386
387 typeCol.AddAttribute(typeCell, "text", 0);
388 idCol.AddAttribute(idCell, "text", 1);
389 textCol.AddAttribute(textCell, "text", 2);
390 rectCol.AddAttribute(rectCell, "text", 3);
391
392 Gtk.TreeStore store = new Gtk.TreeStore(typeof(string), typeof(string), typeof(string), typeof(string));
393
394 foreach (BioImage b in Images.images)
395 {
396 Gtk.TreeIter iter = store.AppendValues(System.IO.Path.GetFileName(b.Filename));
397 foreach (ROI r in b.Annotations)
398 {
399 store.AppendValues(iter, r.type.ToString(), r.id, r.Text, r.BoundingBox.ToString());
400 }
401 }
402 roiView.Model = store;
403
404 }
409 {
410 rois.Clear();
411 Gtk.TreeStore store = new Gtk.TreeStore(typeof(string), typeof(string), typeof(string), typeof(string));
412 foreach (BioImage b in Images.images)
413 {
414 Gtk.TreeIter iter = store.AppendValues(System.IO.Path.GetFileName(b.Filename));
415 foreach (ROI r in b.Annotations)
416 {
417 if (r.id == "")
418 {
419 store.AppendValues(iter, r.type.ToString(), rng.Next(), r.Text, r.BoundingBox.ToString());
420 }
421 else
422 {
423 if(!rois.ContainsKey(r.id))
424 rois.Add(r.id, r);
425 store.AppendValues(iter, r.type.ToString(), r.id, r.Text, r.BoundingBox.ToString());
426 }
427 }
428 }
429 roiView.Model = store;
430 }
431
433 private void UpdateView()
434 {
435 App.viewer.UpdateView();
436 }
443 public void updateROI(int index, ROI an)
444 {
445 if (ImageView.SelectedImage == null)
446 return;
447 ImageView.SelectedImage.Annotations[index] = an;
448 UpdateView();
449 }
456 private void xBox_ValueChanged(object sender, EventArgs e)
457 {
458 if (anno == null)
459 return;
460 anno.BoundingBox = new RectangleD(xBox.Value, anno.Y, anno.W, anno.H);
461 UpdateView();
462 }
470 private void yBox_ValueChanged(object sender, EventArgs e)
471 {
472 if (anno == null)
473 return;
474 anno.BoundingBox = new RectangleD(anno.X, yBox.Value, anno.W, anno.H);
475 UpdateView();
476 }
483 private void wBox_ValueChanged(object sender, EventArgs e)
484 {
485 if (anno == null)
486 return;
487 if(anno.type == ROI.Type.Rectangle || anno.type == ROI.Type.Ellipse)
488 anno.BoundingBox = new RectangleD(anno.X, anno.Y, wBox.Value, anno.H);
489 UpdateView();
490 }
497 private void hBox_ValueChanged(object sender, EventArgs e)
498 {
499 if (anno == null)
500 return;
501 if (anno.type == ROI.Type.Rectangle || anno.type == ROI.Type.Ellipse)
502 anno.BoundingBox = new RectangleD(anno.X, anno.Y, anno.W, hBox.Value);
503 UpdateView();
504 }
511 private void sBox_ValueChanged(object sender, EventArgs e)
512 {
513 if (anno == null)
514 return;
515 //anno.serie = (int)sBox.Value;
516 UpdateView();
517 }
525 private void zBox_ValueChanged(object sender, EventArgs e)
526 {
527 if (anno == null)
528 return;
529 anno.coord.Z = (int)zBox.Value;
530 UpdateView();
531 }
539 private void cBox_ValueChanged(object sender, EventArgs e)
540 {
541 if (anno == null)
542 return;
543 anno.coord.C = (int)cBox.Value;
544 UpdateView();
545 }
553 private void tBox_ValueChanged(object sender, EventArgs e)
554 {
555 if (anno == null)
556 return;
557 anno.coord.T = (int)cBox.Value;
558 UpdateView();
559 }
567 private void rBox_ValueChanged(object sender, EventArgs e)
568 {
569 if (anno == null)
570 return;
571 anno.strokeColor = Color.FromArgb((byte)rBox.Value, anno.strokeColor.G, anno.strokeColor.B);
572 UpdateView();
573 }
581 private void gBox_ValueChanged(object sender, EventArgs e)
582 {
583 if (anno == null)
584 return;
585 anno.strokeColor = Color.FromArgb(anno.strokeColor.R, (byte)gBox.Value, anno.strokeColor.B);
586 UpdateView();
587 }
595 private void bBox_ValueChanged(object sender, EventArgs e)
596 {
597 if (anno == null)
598 return;
599 anno.strokeColor = Color.FromArgb(anno.strokeColor.R, anno.strokeColor.G, (byte)bBox.Value);
600 UpdateView();
601 }
609 private void textBox_TextChanged(object sender, EventArgs e)
610 {
611 if (anno == null)
612 return;
613 anno.Text = textBox.Text;
614 UpdateView();
615 }
623 private void idBox_TextChanged(object sender, EventArgs e)
624 {
625 if (anno == null)
626 return;
627 anno.id = idBox.Text;
628 UpdateView();
629 }
637 private void ROIManager_Activated(object sender, EventArgs e)
638 {
639 if (ImageView.SelectedImage == null)
640 return;
641 string n = System.IO.Path.GetFileName(ImageView.SelectedImage.ID);
642 if (imageNameLabel.Text != n)
643 imageNameLabel.Text = n;
645 }
646
651 private void addButton_Click(object sender, EventArgs e)
652 {
653 ImageView.SelectedImage.Annotations.Add(anno);
654 UpdateView();
655 }
656 public static bool showBounds = true;
657 public static bool showText = false;
658 public static bool showR = true;
659 public static bool showG = true;
660 public static bool showB = true;
665 private void showBoundsBox_ActiveChanged(object sender, EventArgs e)
666 {
667 showBounds = boundsBox.Active;
668 UpdateView();
669 }
674 private void showTextBox_ActiveChanged(object sender, EventArgs e)
675 {
676 showText = showTextBox.Active;
677 UpdateView();
678 }
687 private void pointXBox_ValueChanged(object sender, EventArgs e)
688 {
689 if (anno == null)
690 return;
691 if (anno.type == ROI.Type.Rectangle || anno.type == ROI.Type.Ellipse)
692 return;
693 anno.UpdatePoint(new PointD((double)pointXBox.Value, (double)pointYBox.Value),(int)pointBox.Value);
694 UpdateView();
695 }
703 private void pointYBox_ValueChanged(object sender, EventArgs e)
704 {
705 if (anno == null)
706 return;
707 if (anno.type == ROI.Type.Rectangle || anno.type == ROI.Type.Ellipse)
708 return;
709 anno.UpdatePoint(new PointD((double)pointXBox.Value, (double)pointYBox.Value), (int)pointBox.Value);
710 UpdateView();
711 }
712 public bool autoUpdate = true;
716 public void UpdatePointBox()
717 {
718 if (anno == null)
719 return;
720 PointD d = anno.GetPoint((int)pointBox.Value);
721 pointXBox.Value = (int)d.X;
722 pointYBox.Value = (int)d.Y;
723 }
728 private void pointBox_ValueChanged(object sender, EventArgs e)
729 {
731 }
739 private void strokeWBox_ValueChanged(object sender, EventArgs e)
740 {
741 if (anno == null)
742 return;
743 anno.strokeWidth = (int)widthBox.Value;
744 UpdateView();
745 }
751 private void selectBoxSize_ValueChanged(object sender, EventArgs e)
752 {
753 ROI.selectBoxSize = (int)widthBox.Value;
754 UpdateView();
755 }
762 private void showRBox_ActiveChanged(object sender, EventArgs e)
763 {
764 if (App.viewer == null)
765 return;
766 App.viewer.showRROIs = showRBox.Active;
767 UpdateView();
768 }
775 private void showGBox_ActiveChanged(object sender, EventArgs e)
776 {
777 if (App.viewer == null)
778 return;
779 App.viewer.showGROIs = showGBox.Active;
780 UpdateView();
781 }
789 private void showBBox_ActiveChanged(object sender, EventArgs e)
790 {
791 if (App.viewer == null)
792 return;
793 App.viewer.showBROIs = showBBox.Active;
794 UpdateView();
795 }
796
801 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
802 {
803
804 }
805
806 }
807}
void PasteSelection()
void UpdateAnnotationList()
void CopySelection()
It takes the selected ROIs and copies them to the clipboard.
void UpdatePointBox()
void updateROI(int index, ROI an)
Definition ML.cs:17