protected override void OnLoad(EventArgs e)
 {
     base.OnLoad(e);
     Accessory.RedirectAssemblyBinding(@"W:\_Uni\TissueClassification\packages\PicNetML.0.0.16\lib\net45\");
     this.CreateTabContainer("Weka-PicNet");
     this.TabContainer.Enabled = true;
     (new Button {
         Text = "apply classifier", Parent = this.TabContainer, Dock = DockStyle.Top
     }).Click += delegate {
         var progressDialog = new ProgressDialog {
             Message = "executing", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false, BackgroundTask = () => {
                 if (null == this.classifier || null == this.dataSet)
                 {
                     if (!this.loadClassifier())
                     {
                         return;
                     }
                 }
                 if (null == this.SelectedLayer)
                 {
                     if (null == this.DisplayedImage)
                     {
                         return;
                     }
                     this.addLayer(createLayer(this.DisplayedImage), true);
                 }
                 var layer = this.SelectedLayer.CreateAbove(io => true);
                 foreach (var io in layer.Objects)
                 {
                     this.classifier.Classify(this.dataSet, io, n => this.classes[n]);
                 }
                 layer.Name = layer.Name + " classified";
                 this.addLayer(layer, true);
             }
         };
         progressDialog.CenterToScreen();
         progressDialog.ShowDialog();
     };
     (new Button {
         Text = "load classifier", Parent = this.TabContainer, Dock = DockStyle.Top
     }).Click += delegate { this.loadClassifier(); };
     (new Button {
         Text = "save and clear data set", Parent = this.TabContainer, Dock = DockStyle.Top
     }).Click += delegate {
         if (null == this.dataSet)
         {
             return;
         }
         var progressDialog = new ProgressDialog {
             Message = "executing", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false, BackgroundTask = () => {
                 using (var saveFileDialog = new SaveFileDialog {
                     Filter = "WEKA Attribute-Relation File (*.arff)|*.arff"
                 }) if (DialogResult.OK == saveFileDialog.ShowDialog())
                     {
                         this.dataSet.SaveAsWekaArff(saveFileDialog.FileName);
                     }
                 this.dataSet = null;
             }
         };
         progressDialog.CenterToScreen();
         progressDialog.ShowDialog();
     };
     (new Button {
         Text = "add ground truth to data set", Parent = this.TabContainer, Dock = DockStyle.Top
     }).Click += delegate {
         if (null == this.SelectedLayer)
         {
             return;
         }
         var progressDialog = new ProgressDialog {
             Message = "executing", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false, BackgroundTask = () => {
                 if (null == this.dataSet)
                 {
                     this.dataSet = Util.CreateInstances(this.classes.Keys, this.featureNames);
                 }
                 this.dataSet.AddImageObjects(this.SelectedLayer);
             }
         };
         progressDialog.CenterToScreen();
         progressDialog.ShowDialog();
     };
     (new Button {
         Text = "classify layer by ground truth", Parent = this.TabContainer, Dock = DockStyle.Top
     }).Click += delegate {
         if (null == this.DisplayedImage)
         {
             return;
         }
         if (null == this.SelectedLayer)
         {
             return;
         }
         var roiFileName = this.ImageFile.Url + ".roi";
         if (!File.Exists(roiFileName))
         {
             return;
         }
         var progressDialog = new ProgressDialog {
             Message = "executing", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false, BackgroundTask = () => {
                 var learningSampleFile   = LearningSampleFile.FromFile(roiFileName);
                 var groundTruthPositives = learningSampleFile.Samples.Select(sample => new Point((int)sample.Features["x0"].Value, (int)sample.Features["y0"].Value)).ToList();
                 var io2GroundTruth       = new Dictionary <uint, int>();
                 foreach (var p in groundTruthPositives)
                 {
                     var id = this.SelectedLayer.Map[p.X, p.Y];
                     if (!io2GroundTruth.ContainsKey(id))
                     {
                         io2GroundTruth.Add(id, 0);
                     }
                     io2GroundTruth[id]++;
                 }
                 foreach (var io in this.SelectedLayer.Objects)
                 {
                     if (io2GroundTruth.ContainsKey(io.Id))
                     {
                         io.Class = 1 == io2GroundTruth[io.Id]?this.classes["nuclei"]:this.classes["conglomerate"];
                     }
                     else
                     {
                         io.Class = this.classes["void"];
                     }
                 }
             }
         };
         progressDialog.CenterToScreen();
         progressDialog.ShowDialog();
         this.RefreshBuffer();
     };
     (new Button {
         Text = "create layer", Parent = this.TabContainer, Dock = DockStyle.Top
     }).Click += delegate {
         if (null == this.DisplayedImage)
         {
             return;
         }
         var progressDialog = new ProgressDialog {
             Message = "executing", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false, BackgroundTask = () => this.addLayer(createLayer(this.DisplayedImage), true)
         };
         progressDialog.CenterToScreen();
         progressDialog.ShowDialog();
     };
 }
        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);
              };
        }
Ejemplo n.º 3
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CreateTabContainer("Segmentation");
            TabContainer.Enabled = true;
            _debugBox            = new RichTextBox {
                Text = "Debug Output\n==============================================", Parent = TabContainer, Dock = DockStyle.Fill, Enabled = true, ReadOnly = true
            };
            (new Button {
                Text = "execute segmentations", Parent = TabContainer, Dock = DockStyle.Top
            }).Click += delegate {
                if (null == DisplayedImage)
                {
                    return;
                }
                var progressDialog = new ProgressDialog {
                    Message = "executing segmentations", ProgressBarStyle = ProgressBarStyle.Marquee, AllowCancel = false, BackgroundTask = () => {
                        ClearOutput();
                        LogOutput("Calc Layers:");
                        _cellCores = _createCoreObjectLayer();
                        //_lumina = _createLuminaObjectLayer();
                        _simpleLumina = _createSimpleLuminaObjectLayer();

                        LogOutput(ObjectLayerrUtils.GetFeatureValueList(_cellCores, "area").Count.ToString());
                        LogOutput("Calc center point cores...");
                        _newCellCores = ObjectLayerrUtils.SetClass(ObjectLayerrUtils.AddFeatureCenterPoint(ObjectLayerrUtils.AddFeatureFormFactor(_cellCores)), new Class("cellCores", Color.Red));
                        LogOutput("Calc cores in near for lumina");
                        _newLumina = (ObjectLayerrUtils.AddFeatureCoresInNear(ObjectLayerrUtils.AddFeatureFormFactor(_simpleLumina), _newCellCores));

                        SetLayers(new [] { _cellCores, /*_lumina,*/ _simpleLumina, _newCellCores, _newLumina, ObjectLayerrUtils.MergeObjectLayers(_newLumina, _newCellCores) });
                        LogOutput("FINISH");
                    }
                };
                progressDialog.CenterToScreen();
                progressDialog.ShowDialog();
            };

            _createCoreObjectLayer = () => {
                LogOutput("Create Layer Cores");
                var process       = new CellCoresHE();
                var processResult = process.Execute(new ProcessExecutionParams(DisplayedImage));
                var layer         = processResult.Layers.Last();
                LogOutput("Add area and range");
                layer      = ObjectLayerrUtils.AddFeatureRange(ObjectLayerrUtils.AddFeatureArea(layer));
                layer.Name = "cores";
                return(layer);
            };
            _createLuminaObjectLayer = () => {
                LogOutput("Create Layer Lumina");
                var process       = new LuminaSegmentation();
                var processResult = process.Execute(new ProcessExecutionParams(DisplayedImage));
                var layer         = processResult.Layers.Last();
                LogOutput("Add area and range");
                layer      = ObjectLayerrUtils.AddFeatureRange(ObjectLayerrUtils.AddFeatureArea(layer));
                layer.Name = "lumina";
                return(layer);
            };
            _createSimpleLuminaObjectLayer = () => {
                LogOutput("Create Layer simple Lumina");
                var segmentation = new SimpleLuminaSegmentation();
                var layer        = segmentation.Execute(DisplayedImage);
                LogOutput("Add area and range");
                layer = ObjectLayerrUtils.AddFeatureRange(ObjectLayerrUtils.AddFeatureArea(layer));
                LogOutput("Improve Segmentaion Lumina");
                layer      = segmentation.ImprObjectLayer(layer);
                layer.Name = "simple lumina";
                return(layer);
            };
        }
        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());
              };
        }