private ObjectLayer Execute1stLevelSegmentation(GrayscaleProcessor gp, GrayscaleProcessor gpSobel, GrayscaleProcessor gpH)
        {
            ContourBasedSegmentation cbs = new ContourBasedSegmentation();

            cbs.CreatePrimarySegmentation(gp, MAX_CONTOURLENGTH);

            cbs.EvaluateContours(c =>
            {
                if (ContourProperties.FromContour(c).Convexity < 0.95)
                {
                    return(-1);
                }

                return(ContourValue.GetValue(c, gpSobel));
            });

            ObjectLayer layer = cbs.CreateLayer(MIN_CONTOURLENGTH, int.MaxValue);

            layer = new ContourOptimizer().RemoveNonCompactPixels(layer, 3);

            layer = layer.CreateAbove(obj =>
            {
                return(this.GetContourGradient(obj, gp) < 0);
            });

            //layer=new ConcaveObjectSeparation().Execute(layer, 0.33, true);

            return(layer);
        }
Пример #2
0
    private ObjectLayer Execute1stLevelSegmentation(GrayscaleProcessor p, GrayscaleProcessor pSobel)
    {
        ContourBasedSegmentation cbs = new ContourBasedSegmentation();

        cbs.CreatePrimarySegmentation(p, MAX_CONTOURLENGTH);

        cbs.EvaluateContours(pSobel);

        ObjectLayer layer = cbs.CreateLayer();

        layer = layer.CreateAbove(obj =>
        {
            return(GetContourGradient(obj, p) > 0);
        });

        return(layer);
    }
        private static ObjectLayer createLayer(Bitmap source)
        {
            var minContourLength         = 20;
            var maxContourLength         = 200;
            var contourBasedSegmentation = new ContourBasedSegmentation();

            using (var grayscaleProcessor = new GrayscaleProcessor(source, RgbToGrayscaleConversion.Mean)
            {
                WriteBack = false
            }){
                contourBasedSegmentation.CreatePrimarySegmentation(grayscaleProcessor, maxContourLength, true);
                new SobelEdgeDetector().Execute(grayscaleProcessor);
                contourBasedSegmentation.EvaluateContours(grayscaleProcessor);
            }
            var layer = contourBasedSegmentation.CreateLayer(minContourLength, maxContourLength);

            layer.Name = "Contour Based Segmentation (minContourLength=" + minContourLength + ", maxContourLength=" + maxContourLength + ")";
            calculateFeatures(layer, source);
            return(layer);
        }
        private ObjectLayer Execute1stLevelSegmentation(GrayscaleProcessor gp, GrayscaleProcessor gpSobel, GrayscaleProcessor gpH)
        {
            ContourBasedSegmentation cbs = new ContourBasedSegmentation();

              cbs.CreatePrimarySegmentation(gp, MAX_CONTOURLENGTH);

              cbs.EvaluateContours(c =>
              {
            if (ContourProperties.FromContour(c).Convexity < 0.95) return -1;

            return ContourValue.GetValue(c, gpSobel);
              });

              ObjectLayer layer = cbs.CreateLayer(MIN_CONTOURLENGTH, int.MaxValue);

              layer = new ContourOptimizer().RemoveNonCompactPixels(layer, 3);

              layer = layer.CreateAbove(obj =>
              {
            return this.GetContourGradient(obj, gp) < 0;
              });

              layer = new ConcaveObjectSeparation().Execute(layer, 0.33, true);

              return layer;
        }
        private ObjectLayer Execute2ndLevelSegmentation(GrayscaleProcessor gp, GrayscaleProcessor gpSobel, GrayscaleProcessor gpH, float targetArea)
        {
            ContourBasedSegmentation cbs = new ContourBasedSegmentation();

              cbs.CreatePrimarySegmentation(gp, MAX_CONTOURLENGTH);

              cbs.EvaluateContours(c =>
              {
            return this.GetContourValue(c, gpSobel, gpH, targetArea);
              });

              ObjectLayer layer = cbs.CreateLayer(MIN_CONTOURLENGTH, int.MaxValue);

              layer = layer.CreateAbove(obj =>
              {
            return this.GetContourGradient(obj, gp) < 0;
              });

              layer = new ContourOptimizer().RemoveNonCompactPixels(layer, 3);

              layer = new ConcaveObjectSeparation().Execute(layer, 0.33, true);

              double[] hBackground = this.GetBackgroundHistogram(layer, gpH);
              double[] hForeground = this.GetForegroundHistogram(layer, gpH);

              bool isFirst = true;

              ObjectLayer firstStep = null;

              while (true)
              {
            bool removed = false;

            layer = layer.CreateAbove(obj =>
            {
              int[] hHistogram = this.GetHistogram(obj, gpH);

              double hRatioForeground = this.GetRatioForeground(hHistogram, hForeground, hBackground);

              if (hRatioForeground > 0.5) return true;

              for (int i = 0; i < 256; i++)
              {
            int val = hHistogram[i];

            hForeground[i] -= val;
            hBackground[i] += val;
              }

              removed = true;

              return false;
            });

            if (isFirst)
            {
              firstStep = layer;
              isFirst = false;
            }

            if (!removed) break;
              }

              if (layer.Objects.Count == 0) layer = firstStep;

              double minArea = Math.Max(0.1 * targetArea, MIN_AREA);

              layer = layer.CreateAbove(obj =>
              {
            float area = ContourProperties.FromContour(obj.Contour).Area;

            return area >= minArea;
              });

              layer = this.RefillContours(layer);

              return layer;
        }
        private ObjectLayer Execute2ndLevelSegmentation(GrayscaleProcessor gp, GrayscaleProcessor gpSobel, GrayscaleProcessor gpH, float targetArea)
        {
            ContourBasedSegmentation cbs = new ContourBasedSegmentation();

            cbs.CreatePrimarySegmentation(gp, MAX_CONTOURLENGTH);

            cbs.EvaluateContours(c =>
            {
                return(this.GetContourValue(c, gpSobel, gpH, targetArea));
            });

            ObjectLayer layer = cbs.CreateLayer(MIN_CONTOURLENGTH, int.MaxValue);

            layer = layer.CreateAbove(obj =>
            {
                return(this.GetContourGradient(obj, gp) < 0);
            });

            layer = new ContourOptimizer().RemoveNonCompactPixels(layer, 3);

            //layer=new ConcaveObjectSeparation().Execute(layer, 0.33, true);

            double[] hBackground = this.GetBackgroundHistogram(layer, gpH);
            double[] hForeground = this.GetForegroundHistogram(layer, gpH);

            bool isFirst = true;

            ObjectLayer firstStep = null;

            while (true)
            {
                bool removed = false;

                layer = layer.CreateAbove(obj =>
                {
                    int[] hHistogram = this.GetHistogram(obj, gpH);

                    double hRatioForeground = this.GetRatioForeground(hHistogram, hForeground, hBackground);

                    if (hRatioForeground > 0.5)
                    {
                        return(true);
                    }

                    for (int i = 0; i < 256; i++)
                    {
                        int val = hHistogram[i];

                        hForeground[i] -= val;
                        hBackground[i] += val;
                    }

                    removed = true;

                    return(false);
                });

                if (isFirst)
                {
                    firstStep = layer;
                    isFirst   = false;
                }

                if (!removed)
                {
                    break;
                }
            }

            if (layer.Objects.Count == 0)
            {
                layer = firstStep;
            }

            double minArea = Math.Max(0.1 * targetArea, MIN_AREA);

            layer = layer.CreateAbove(obj =>
            {
                float area = ContourProperties.FromContour(obj.Contour).Area;

                return(area >= minArea);
            });

            layer = this.RefillContours(layer);

            return(layer);
        }