383 {
384 PointD p;
385 p = ImageView.SelectedImage.ToImageSpace(e);
386 if (App.viewer ==
null ||
currentTool ==
null || ImageView.SelectedImage ==
null || anno ==
null)
387 return;
388 Scripting.UpdateState(Scripting.State.GetUp(e, buts));
390 {
391 ROI an = new ROI();
392 an.AddPoint(new PointD(e.X, e.Y));
393 an.type = ROI.Type.Point;
394 an.coord = App.viewer.GetCoordinate();
395 ImageView.SelectedImage.Annotations.Add(an);
396 }
397 else
398 if (
currentTool.
type == Tool.Type.line && anno.type == ROI.Type.Line && buts == MouseButtons.Left)
399 {
400 if (anno.GetPointCount() > 0)
401 {
402 anno.UpdatePoint(new PointD(e.X, e.Y), 1);
403 anno = new ROI();
404 }
405 }
406 else
407 if (
currentTool.
type == Tool.Type.rect && anno.type == ROI.Type.Rectangle && buts == MouseButtons.Left)
408 {
409 if (anno.GetPointCount() == 4)
410 {
411 anno = new ROI();
412 }
413 }
414 else
415 if (
currentTool.
type == Tool.Type.ellipse && anno.type == ROI.Type.Ellipse && buts == MouseButtons.Left)
416 {
417 if (anno.GetPointCount() == 4)
418 {
419 anno = new ROI();
420 }
421 }
422 else
423 if (
currentTool.
type == Tool.Type.freeform && anno.type == ROI.Type.Freeform && buts == MouseButtons.Left)
424 {
425 anno = new ROI();
426 }
427 else
428 if (
currentTool.
type == Tool.Type.rectSel && buts == MouseButtons.Left)
429 {
430 ImageView.selectedAnnotations.Clear();
432 foreach (ROI an in App.viewer.AnnotationsRGB)
433 {
434 if (an.GetSelectBound(App.viewer.GetScale(), App.viewer.GetScale()).ToRectangleF().IntersectsWith(r.ToRectangleF()))
435 {
436 an.selectedPoints.Clear();
437 ImageView.selectedAnnotations.Add(an);
438 an.Selected = true;
440 for (int i = 0; i < sels.Length; i++)
441 {
442 if (sels[i].IntersectsWith(r))
443 {
444 an.selectedPoints.Add(i);
445 }
446 }
447 }
448 else
449 an.Selected = false;
450 }
452 }
453 else
454 if (
Tools.currentTool.type ==
Tools.Tool.Type.magic && buts == MouseButtons.Left)
455 {
456 PointD pf = new PointD(ImageView.mouseUp.X - ImageView.mouseDown.X, ImageView.mouseUp.Y - ImageView.mouseDown.Y);
457 ZCT coord = App.viewer.GetCoordinate();
458
459 Rectangle r = new Rectangle((int)ImageView.mouseDown.X, (int)ImageView.mouseDown.Y, (int)(ImageView.mouseUp.X - ImageView.mouseDown.X), (int)(ImageView.mouseUp.Y - ImageView.mouseDown.Y));
460 if (r.Width <= 2 || r.Height <= 2)
461 return;
462 Bitmap bf = ImageView.SelectedImage.Buffers[ImageView.SelectedImage.Coords[coord.Z, coord.C, coord.T]];
463 bf.Crop(r);
464 Statistics[] sts = Statistics.FromBytes(bf);
465 Statistics st = sts[0];
466 Threshold th;
468 {
469 th =
new Threshold((
int)magicSel.
Threshold);
470 }
471 else
472 if (magicSel.
Index == 2)
473 th = new Threshold((int)(st.Min + st.Mean));
474 else
475 if (magicSel.
Index == 1)
476 th = new Threshold((int)st.Median);
477 else
478 th = new Threshold((int)st.Min);
479 th.ApplyInPlace((Bitmap)bf.Image);
480 Invert inv = new Invert();
481 Bitmap det;
482 if (bf.BitsPerPixel > 8)
483 det = AForge.Imaging.Image.Convert16bppTo8bpp((Bitmap)bf.Image);
484 else
485 det = (Bitmap)bf.Image;
486 BlobCounter blobCounter = new BlobCounter();
487 blobCounter.ProcessImage(det);
488 Blob[] blobs = blobCounter.GetObjectsInformation();
489
490 GrahamConvexHull hullFinder = new GrahamConvexHull();
491
492
493 foreach (Blob blob in blobs)
494 {
495 if (blob.Rectangle.Width < magicSel.
Max && blob.Rectangle.Height < magicSel.
Max)
496 continue;
497 List<IntPoint> leftPoints = new List<IntPoint>();
498 List<IntPoint> rightPoints = new List<IntPoint>();
499 List<IntPoint> edgePoints = new List<IntPoint>();
500 List<IntPoint> hull = new List<IntPoint>();
501
502 blobCounter.GetBlobsLeftAndRightEdges(blob,
503 out leftPoints, out rightPoints);
504 edgePoints.AddRange(leftPoints);
505 edgePoints.AddRange(rightPoints);
506
507 hull = hullFinder.FindHull(edgePoints);
508 PointD[] pfs = new PointD[hull.Count];
509 for (int i = 0; i < hull.Count; i++)
510 {
511 pfs[i] = new PointD(r.X + hull[i].X, r.Y + hull[i].Y);
512 }
513 ROI an = ROI.CreateFreeform(coord, pfs);
514 ImageView.SelectedImage.Annotations.Add(an);
515 }
516 }
517 else
518 if (
Tools.currentTool.type ==
Tools.Tool.Type.bucket && buts == MouseButtons.Left)
519 {
520 ZCT coord = App.viewer.GetCoordinate();
523 floodFiller.Bitmap = ImageView.SelectedImage.Buffers[ImageView.SelectedImage.Coords[coord.C, coord.Z, coord.T]];
524 floodFiller.
FloodFill(
new AForge.Point((
int)p.X, (
int)p.Y));
525 App.viewer.UpdateImages();
526 }
527 else
528 if (
Tools.currentTool.type ==
Tools.Tool.Type.dropper && buts == MouseButtons.Left)
529 {
530 DrawColor = ImageView.SelectedBuffer.GetPixel((
int)p.X,(
int)p.Y);
531 UpdateGUI();
532 }
534 }