private ObjectLayer Execute2ndLevelSegmentation(ObjectLayer l1stLevel, GrayscaleProcessor p) { ObjectLayer layer = l1stLevel.CreateAbove(obj => { return(true); }); double[] background = GetBackgroundHistogram(layer, p); double[] foreground = GetForegroundHistogram(layer, p); while (true) { bool removed = false; layer = layer.CreateAbove(obj => { int[] histogram = GetHistogram(obj, p); double ratioForeground = GetRatioForeground(histogram, foreground, background); if (ratioForeground > 0.5) { return(true); } for (int i = 0; i < 256; i++) { int val = histogram[i]; background[i] += val; foreground[i] -= val; } removed = true; return(false); }); if (!removed) { break; } } return(layer); }
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 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); }