483 {
484 Plugins.MouseUp(ImageView.SelectedImage, e, buts);
485 PointD p = new PointD((float)e.X, (float)e.Y);
486 PointD MouseU = ImageView.SelectedImage.ToImageSpace(p);
487 PointD MouseD = ImageView.SelectedImage.ToImageSpace(new PointD(App.viewer.MouseDown.X,App.viewer.MouseDown.Y));
488 if (App.viewer == null || currentTool == null || ImageView.SelectedImage == null)
489 return;
490 Scripting.UpdateState(Scripting.State.GetUp(e, buts.Event.Button));
491 if (currentTool.type == Tool.Type.pan && buts.Event.Button == 2)
492 currentTool =
GetTool(Tool.Type.move);
493 if (currentTool.type == Tool.Type.point && buts.Event.Button == 1)
494 {
495 ROI an = new ROI();
496 an.AddPoint(new PointD(e.X, e.Y));
497 an.type = ROI.Type.Point;
498 an.coord = App.viewer.GetCoordinate();
499 an.Selected = true;
500 selectedROI = an;
501
502 AddROI(an);
503 }
504 else
505 if (Tools.currentTool.type == Tools.Tool.Type.bucket && buts.Event.Button == 1)
506 {
507 if (MouseU.X >= ImageView.SelectedImage.SizeX && MouseU.Y >= ImageView.SelectedImage.SizeY)
508 return;
509 floodFiller.FillColor = DrawColor;
510 floodFiller.Tolerance = new ColorS(0, 0, 0);
511 floodFiller.Bitmap = ImageView.SelectedBuffer;
512 floodFiller.FloodFill(new AForge.Point((int)MouseU.X, (int)MouseU.Y));
513 App.viewer.UpdateImages();
514 App.viewer.UpdateView();
515 }
516 else
517 if (Tools.currentTool.type == Tools.Tool.Type.dropper && buts.Event.Button == 1)
518 {
519 if (MouseU.X < ImageView.SelectedImage.SizeX && MouseU.Y < ImageView.SelectedImage.SizeY)
520 {
521 DrawColor = ImageView.SelectedBuffer.GetPixel((int)MouseU.X, (int)MouseU.Y);
522 }
523 }
524 else
525 if (Tools.currentTool.type == Tools.Tool.Type.magic && buts.Event.Button == 1)
526 {
527
528 RectangleD rectangle = new RectangleD(
529 App.viewer.MouseDown.X,
530 App.viewer.MouseDown.Y,
531 Math.Abs(App.viewer.MouseUp.X - App.viewer.MouseDown.X),
532 Math.Abs(App.viewer.MouseUp.Y - App.viewer.MouseDown.Y)
533 );
534
535
536 AForge.RectangleF rectInImageSpace = ImageView.SelectedImage.ToImageSpace(rectangle);
537 ZCT coord = App.viewer.GetCoordinate();
538 Bitmap bitmap;
539
540
541 if (ImageView.SelectedImage.Buffers[0].RGBChannelsCount > 1)
542 {
543 bitmap = ImageView.SelectedImage.GetFiltered(
544 coord,
545 new IntRange((int)ImageView.SelectedBuffer.Stats[0].Min, (int)ImageView.SelectedBuffer.Stats[0].Max),
546 new IntRange((int)ImageView.SelectedBuffer.Stats[1].Min, (int)ImageView.SelectedBuffer.Stats[1].Max),
547 new IntRange((int)ImageView.SelectedBuffer.Stats[2].Min, (int)ImageView.SelectedBuffer.Stats[2].Max)
548 );
549 }
550 else
551 {
552 bitmap = ImageView.SelectedImage.GetFiltered(
553 coord,
554 new IntRange((int)ImageView.SelectedBuffer.Stats[0].Min, (int)ImageView.SelectedBuffer.Stats[0].Max),
555 new IntRange((int)ImageView.SelectedBuffer.Stats[0].Min, (int)ImageView.SelectedBuffer.Stats[0].Max),
556 new IntRange((int)ImageView.SelectedBuffer.Stats[0].Min, (int)ImageView.SelectedBuffer.Stats[0].Max)
557 );
558 }
559
560
561 bitmap.Crop(rectInImageSpace.ToRectangleInt());
562
563 Statistics[] st = Statistics.FromBytes(bitmap.Bytes, bitmap.SizeX, bitmap.Height, bitmap.RGBChannelsCount, bitmap.BitsPerPixel, bitmap.Stride, bitmap.PixelFormat);
564 int threshold = magicSelect.Numeric ? magicSelect.Threshold : CalculateThreshold(magicSelect.Index, st[0]);
565 BlobCounter blobCounter = new BlobCounter();
566 OtsuThreshold th = new OtsuThreshold();
567 th.ApplyInPlace(bitmap);
568 blobCounter.FilterBlobs = true;
569 blobCounter.MinWidth = 2;
570 blobCounter.MinHeight = 2;
571 blobCounter.ProcessImage(bitmap);
572
573 Blob[] blobs = blobCounter.GetObjectsInformation();
574 double pixelSizeX = ImageView.SelectedImage.PhysicalSizeX;
575 double pixelSizeY = ImageView.SelectedImage.PhysicalSizeY;
576
577 foreach (Blob blob in blobs)
578 {
579 AForge.RectangleD blobRectangle = new AForge.RectangleD(
580 blob.Rectangle.X * pixelSizeX,
581 blob.Rectangle.Y * pixelSizeY,
582 blob.Rectangle.Width * pixelSizeX,
583 blob.Rectangle.Height * pixelSizeY
584 );
585
586 PointD location = new PointD(rectangle.X + blobRectangle.X, rectangle.Y + blobRectangle.Y);
587
588 ROI annotation = ROI.CreateRectangle(coord, location.X, location.Y, blobRectangle.W, blobRectangle.H);
589 ImageView.SelectedImage.Annotations.Add(annotation);
590 }
591 App.viewer.UpdateView(true);
592 }
593
594 if (selectedROI == null)
595 return;
596 if (currentTool.type == Tool.Type.line && selectedROI.type == ROI.Type.Line && buts.Event.Button == 1)
597 {
598 if (selectedROI.GetPointCount() > 0)
599 {
600 selectedROI.UpdatePoint(new PointD(e.X, e.Y), 1);
601 selectedROI = null;
602 }
603 }
604 else
605 if (currentTool.type == Tool.Type.rect && selectedROI.type == ROI.Type.Rectangle && buts.Event.Button == 1)
606 {
607 if (selectedROI.GetPointCount() == 4)
608 {
609 selectedROI = null;
610 }
611 }
612 else
613 if (currentTool.type == Tool.Type.ellipse && selectedROI.type == ROI.Type.Ellipse && buts.Event.Button == 1)
614 {
615 if (selectedROI.GetPointCount() == 4)
616 {
617 selectedROI = null;
618 }
619 }
620 else
621 if (currentTool.type == Tool.Type.freeform && selectedROI.type == ROI.Type.Freeform && buts.Event.Button == 1)
622 {
623 selectedROI = null;
624 }
625
626 }