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 double GetContourValue(Contour c, GrayscaleProcessor gpSobel, GrayscaleProcessor gpH, float targetArea)
        {
            double h = MeanIntensityOnContour.GetValue(c, gpH);

            ContourProperties cp = ContourProperties.FromContour(c);

            float area = Math.Min(cp.Area, targetArea) / targetArea;

            if (cp.Convexity < 0.9 && cp.Area < targetArea)
            {
                area = 1F / targetArea;
            }

            float convexity = cp.Convexity * cp.Convexity;

            double color = h / 255.0;

            return(color * area * convexity * ContourValue.GetValue(c, gpSobel));
        }