public override ProcessResult Execute(ProcessExecutionParams p)
        {
            GrayscaleProcessor gpH = new ColorDeconvolution().Get1stStain(p.Bitmap, ColorDeconvolution.KnownStain.HaematoxylinEosin);

            GrayscaleProcessor gp = new GrayscaleProcessor(p.Bitmap, RgbToGrayscaleConversion.JustReds);

            GrayscaleProcessor gpSobel = (GrayscaleProcessor)gp.Clone();

            new MeanFilter().Execute(gp, new Size(3, 3));

            new SobelEdgeDetector().Execute(gpSobel);

            new MinimumFilter().Execute(gpH, new Size(3, 3));

            new PixelInverter().Execute(gpH);

            ObjectLayer l1stLevel = this.Execute1stLevelSegmentation(gp, gpSobel, gpH);

            float targetArea = this.GetTargetArea(l1stLevel);

            ObjectLayer l2ndLevel = this.Execute2ndLevelSegmentation(gp, gpSobel, gpH, targetArea);

            ObjectLayer l3rdLevel = this.Execute3rdLevelSegmentation(l2ndLevel, gpSobel, gpH, targetArea);

            gpSobel.WriteBack = false;
            gpH.WriteBack     = false;
            gp.WriteBack      = false;

            gpSobel.Dispose();
            gpH.Dispose();
            gp.Dispose();

            gpSobel.Bitmap.Dispose();
            gpH.Bitmap.Dispose();

            l3rdLevel.Name = "Image Analysis";

            return(new ProcessResult(new ObjectLayer[] { l3rdLevel }));
        }
        public override ProcessResult Execute(ProcessExecutionParams p)
        {
            GrayscaleProcessor gpH = new ColorDeconvolution().Get1stStain(p.Bitmap, ColorDeconvolution.KnownStain.HaematoxylinEosin);

              GrayscaleProcessor gp = new GrayscaleProcessor(p.Bitmap, RgbToGrayscaleConversion.JustReds);

              GrayscaleProcessor gpSobel = (GrayscaleProcessor)gp.Clone();

              new MeanFilter().Execute(gp, new Size(3, 3));

              new SobelEdgeDetector().Execute(gpSobel);

              new MinimumFilter().Execute(gpH, new Size(3, 3));

              new PixelInverter().Execute(gpH);

              ObjectLayer l1stLevel = this.Execute1stLevelSegmentation(gp, gpSobel, gpH);

              float targetArea = this.GetTargetArea(l1stLevel);

              ObjectLayer l2ndLevel = this.Execute2ndLevelSegmentation(gp, gpSobel, gpH, targetArea);

              ObjectLayer l3rdLevel = this.Execute3rdLevelSegmentation(l2ndLevel, gpSobel, gpH, targetArea);

              gpSobel.WriteBack = false;
              gpH.WriteBack = false;
              gp.WriteBack = false;

              gpSobel.Dispose();
              gpH.Dispose();
              gp.Dispose();

              gpSobel.Bitmap.Dispose();
              gpH.Bitmap.Dispose();

              l3rdLevel.Name = "Image Analysis";

              return new ProcessResult(new ObjectLayer[] { l3rdLevel });
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
              this.CreateTabContainer("StromaDetection");
              this.TabContainer.Enabled = true;

              (new Button
              {
            Text = "execute cell core segmentation",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.DisplayedImage) return;
            ProcessResult result = null;
            var progressDialog = new ProgressDialog { Message = "executing cell core segmentation", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false };
            progressDialog.BackgroundTask += () =>
            {
              var segmentation = new CellCoreSegmentation();
              var executionParams = new ProcessExecutionParams(this.DisplayedImage);
              result = segmentation.Execute(executionParams);
            };
            progressDialog.CenterToScreen();
            progressDialog.ShowDialog();
            this.SetLayers(result.Layers.ToArray());
              };

              (new Button
              {
            Text = "execute threshold segmentation",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.DisplayedImage) return;
            var m = new Map(this.DisplayedImage.Width, this.DisplayedImage.Height);
            using (var gp = new GrayscaleProcessor(this.DisplayedImage.Clone() as Bitmap, RgbToGrayscaleConversion.Mean))
            {
              for (var x = 0; x < this.DisplayedImage.Width; x++)
              {
            for (var y = 0; y < this.DisplayedImage.Height; y++)
            {
              m[x, y] = gp.GetPixel(x, y) < this.threshold.Value ? 1u : 0u;
            }
              }
            }
            var layer = new ConnectedComponentCollector().Execute(m);
            layer.Name = "threshold " + this.threshold.Value + " segmentation";
            var layers = this.GetLayers().ToList();
            layers.Add(layer);
            this.SetLayers(layers.ToArray());
              };

              this.threshold = new NumericUpDown
              {
            Parent = new GroupBox
            {
              Parent = this.TabContainer,
              Dock = DockStyle.Top,
              Text = "threshold",
              Height = 40
            },
            Dock = DockStyle.Fill,
            Minimum = 0,
            Maximum = 255,
            Increment = 16,
            Value = 128,
            DecimalPlaces = 0
              };

              (new Button
              {
            Text = "display edges",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            this.SetDisplayedImage(this.edges);
              };

              (new Button
              {
            Text = "execute edge detection",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.stainH || null == this.stainE) return;
            this.responseH = Filtering.ExecuteSobel(this.stainH);
            this.responseE = Filtering.ExecuteSobel(this.stainE);
            var substracted = new double[this.responseH.Size.Width, this.responseH.Size.Height];
            var substractedRange = new Range<double>();
            for (var x = 0; x < this.responseH.Size.Width; x++)
            {
              for (var y = 0; y < this.responseH.Size.Height; y++)
              {
            var value = Math.Max(0, this.responseE.Gradient[x, y] - this.responseH.Gradient[x, y]);
            substracted[x, y] = value;
            substractedRange.Add(value);
              }
            }
            this.nonMaximumSupression = Filtering.ExecuteNonMaximumSupression(substracted, this.responseE.Orientation);
            this.edges = Visualization.Visualize(this.nonMaximumSupression, Visualization.CreateColorizing(substractedRange.Maximum));
            this.SetDisplayedImage(this.edges);
              };

              (new Button
              {
            Text = "display haematoxylin",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            this.SetDisplayedImage(this.stainH);
              };

              (new Button {
            Text = "display eosin",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            this.SetDisplayedImage(this.stainE);
              };

              (new Button {
            Text = "display source",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            this.SetDisplayedImage(this.source);
              };

              (new Button {
            Text = "execute deconvolution",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.DisplayedImage) return;
            this.source = this.DisplayedImage;
            var gpE = new ColorDeconvolution().Get2ndStain(this.DisplayedImage, ColorDeconvolution.KnownStain.HaematoxylinEosin);
            gpE.Dispose();
            this.stainE = gpE.Bitmap;
            var gpH = new ColorDeconvolution().Get1stStain(this.DisplayedImage, ColorDeconvolution.KnownStain.HaematoxylinEosin);
            gpH.Dispose();
            this.stainH = gpH.Bitmap;
            this.SetDisplayedImage(this.stainE);
              };
        }
示例#4
0
    public override ProcessResult Execute(ProcessExecutionParams p)
    {
        GrayscaleProcessor gp = new GrayscaleProcessor(new Bitmap(p.Bitmap), RgbToGrayscaleConversion.None);

        BitmapProcessor bp = new BitmapProcessor(p.Bitmap.Clone() as Bitmap);

        for (int dy = 0; dy < gp.Height; dy++)
        {
            for (int dx = 0; dx < gp.Width; dx++)
            {
                int pixel = (int)bp.GetPixel(dx, dy);

                int r = (pixel & 0x00ff0000) >> 16;
                int g = (pixel & 0x0000ff00) >> 08;
                int b = (pixel & 0x000000ff) >> 00;

                int max = Math.Max(Math.Max(r, g), b);
                int min = Math.Min(Math.Min(r, g), b);

                int range = max - min;

                int level = (r + g + b) / 3;

                int val = level - range;

                if (val > 255)
                {
                    val = 255;
                }
                if (val < 0)
                {
                    val = 0;
                }

                gp.SetPixel(dx, dy, (UInt32)level);
            }
        }

        GrayscaleProcessor gpSobel = (GrayscaleProcessor)gp.Clone();

        new SobelEdgeDetector().Execute(gpSobel);

        ObjectLayer l1stLevel = Execute1stLevelSegmentation(gp, gpSobel);

        ObjectLayer l2ndLevel = Execute2ndLevelSegmentation(l1stLevel, gp);

        gpSobel.WriteBack = false;
        gp.WriteBack      = false;

        gpSobel.Dispose();
        gp.Dispose();
        bp.Dispose();

        gpSobel.Bitmap.Dispose();
        gp.Bitmap.Dispose();
        bp.Bitmap.Dispose();

        l1stLevel.Name = "1st Level";
        l2ndLevel.Name = "2nd Level";

        return(new ProcessResult(new ObjectLayer[] { l1stLevel, l2ndLevel }));
    }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
              this.CreateTabContainer("StromaDetection2");
              this.TabContainer.Enabled = true;
              RConnector.BinPath = @"C:\Program Files\R\R-3.1.0\bin\x64\";

              (new Button {
            Text = "cluster by roundness",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.SelectedLayer) return;
            try { RConnector.Engine.Evaluate("library(\"mclust\")"); }
            catch { throw new Exception("try install mclust"); }
            var progressDialog = new ProgressDialog {
              Message = "extracting features",
              ProgressBarStyle = ProgressBarStyle.Marquee,
              AllowCancel = false
            };
            progressDialog.BackgroundTask += () =>
            {
              var matrix = RConnector.Engine.CreateNumericMatrix(this.SelectedLayer.Objects.Count, 1);
              for (var i = 0; i < this.SelectedLayer.Objects.Count; i++)
              {
            matrix[i, 0] = this.SelectedLayer.Objects[i].Features["AspectRatioOfBoundingEllipsoid"].Value;
              }
              var result = this.cluster(matrix);
              this.drawClusters(result.Item1, result.Item2);
            };
            progressDialog.CenterToScreen();
            progressDialog.ShowDialog();
            this.RefreshBuffer();
              };

              (new Button {
            Text = "cluster by position",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.SelectedLayer) return;
            try { RConnector.Engine.Evaluate("library(\"mclust\")"); }
            catch { throw new Exception("try install mclust"); }
            var progressDialog = new ProgressDialog {
              Message = "extracting features",
              ProgressBarStyle = ProgressBarStyle.Marquee,
              AllowCancel = false
            };
            progressDialog.BackgroundTask += () =>
            {
              var matrix = RConnector.Engine.CreateNumericMatrix(this.SelectedLayer.Objects.Count, 2);
              for (var i = 0; i < this.SelectedLayer.Objects.Count; i++)
              {
            var io = this.SelectedLayer.Objects[i];
            matrix[i, 0] = io.Features["CentroidX"].Value;
            matrix[i, 1] = io.Features["CentroidY"].Value;
              }
              var result = this.cluster(matrix);
              this.drawClusters(result.Item1, result.Item2);
            };
            progressDialog.CenterToScreen();
            progressDialog.ShowDialog();
            this.RefreshBuffer();
              };

              this.clusters = new NumericUpDown {
            Parent = new GroupBox {
              Parent = this.TabContainer,
              Dock = DockStyle.Top,
              Text = "clusters",
              Height = 40
            },
            Dock = DockStyle.Fill,
            Minimum = 0,
            Maximum = 20,
            Increment = 1,
            Value = 0,
            DecimalPlaces = 0
              };

              (new Button {
            Text = "extract features",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.SelectedLayer) return;
            var progressDialog = new ProgressDialog {
              Message = "extracting features",
              ProgressBarStyle = ProgressBarStyle.Marquee,
              AllowCancel = false
            };
            progressDialog.BackgroundTask += () =>
            {
              Centroid.ProcessLayer(this.SelectedLayer);
              foreach (var io in this.SelectedLayer.Objects)
              {
            var boundingEllipsoid = BoundingEllipsoid.FromContour(io.Contour);
            if (!io.Features.Contains("AngleOfBoundingEllipsoid")) io.Features.Add(new AngleOfBoundingEllipsoid(boundingEllipsoid));
            if (!io.Features.Contains("AspectRatioOfBoundingEllipsoid")) io.Features.Add(new AspectRatioOfBoundingEllipsoid(boundingEllipsoid));
              }
            };
            progressDialog.CenterToScreen();
            progressDialog.ShowDialog();
              };

              (new Button {
            Text = "execute cell core segmentation",
            Parent = this.TabContainer,
            Dock = DockStyle.Top
              }).Click += delegate
              {
            if (null == this.DisplayedImage) return;
            ProcessResult result = null;
            var progressDialog = new ProgressDialog {
              Message = "executing cell core segmentation",
              ProgressBarStyle = ProgressBarStyle.Marquee,
              AllowCancel = false
            };
            progressDialog.BackgroundTask += () =>
            {
              var segmentation = new CellCoreSegmentation();
              var executionParams = new ProcessExecutionParams(this.DisplayedImage);
              result = segmentation.Execute(executionParams);
            };
            progressDialog.CenterToScreen();
            progressDialog.ShowDialog();
            this.SetLayers(result.Layers.ToArray());
              };
        }