private Contour[] Sort(Contour[] contours, GrayscaleProcessor gpSobel, GrayscaleProcessor gpH, float targetArea)
        {
            List <ContourDataComposite <double> > valuedContours = new List <ContourDataComposite <double> >();

            Parallel.For(0, contours.Length, i =>
            {
                Contour c = contours[i];

                double value = this.GetContourValue(c, gpSobel, gpH, targetArea);

                if (value <= 0)
                {
                    return;
                }

                lock (valuedContours)
                {
                    valuedContours.Add(new ContourDataComposite <double>(c, value));
                }
            });

            valuedContours.Sort(delegate(ContourDataComposite <double> c1, ContourDataComposite <double> c2)
            {
                if (c1.Data > c2.Data)
                {
                    return(-1);
                }
                if (c1.Data < c2.Data)
                {
                    return(1);
                }

                return(0);
            });

            contours = new Contour[valuedContours.Count];

            for (int i = 0; i < valuedContours.Count; i++)
            {
                contours[i] = valuedContours[i].Contour;
            }

            return(contours);
        }
        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));
        }
        private ObjectLayer CreateLayer(int width, int height, Contour[] contours)
        {
            Map map = new Map(width, height);

            UInt32 id = 1;

            for (int i = 0; i < contours.Length; i++)
            {
                Contour c = contours[i];

                if (c.Length < MIN_CONTOURLENGTH)
                {
                    continue;
                }

                c.Fill(map, id, false);

                id++;
            }

            return(new ConnectedComponentCollector().Execute(map));
        }