private static ImageUniverse DoSegmentation(E[] e, int w, int h, float t) { Array.Sort(e, (e1, e2) => { if (e1.D > e2.D) { return(1); } if (e1.D < e2.D) { return(-1); } return(0); }); var nv = w * h; var T = new float[nv]; for (var i = 0; i < nv; i++) { T[i] = Th(t, 1); } var u = new ImageUniverse(nv); for (var i = 0; i < e.Length; i++) { var l = e[i]; var p0 = u.Parent(l.P0.Index(w)); var p1 = u.Parent(l.P1.Index(w)); if (p0 == p1) { continue; } if (l.D <= T[p0] && l.D <= T[p1]) { u.Join(p0, p1); var np = u.Parent(p0); T[np] = l.D + Th(t, u.Size(np)); } } return(u); }
public static Result DoSegmentation(BImage img, float smoothSigma, bool smoothMultitread, int segMinSize, float segTheshold, bool quadroPixel) { var w = img.W; var h = img.H; using (var tracker = new TimeTracker()) { img = GaussSmooth.Smooth(img, smoothSigma, smoothMultitread); //Program.ShowData.SmoothTime = tracker.ElapsedSec; } ImageUniverse universe = null; using (var tracker = new TimeTracker()) { universe = DoSegmentation(img, segTheshold, segMinSize, quadroPixel); //Program.ShowData.SegmentationTime = tracker.ElapsedSec; } var segments = new Dictionary <Pixel, Segment>(); for (var i = 0; i < universe.Count; i++) { var pixel = Pixel.CreateByI(i, w); var parent = Pixel.CreateByI(universe.Parent(i), w); if (!segments.TryGetValue(parent, out var segment)) { segment = new Segment(img, parent); segments.Add(parent, segment); } segment.AddPixel(pixel); } var averageImage = new BImage(w, h); foreach (var s in segments.Values) { s.Realize(); s.DrawTo(averageImage); } var result = new Result(); result.Image = averageImage; result.ImageUniverse = universe; result.Segments = segments; return(result); }
public void DrawBoundaryTo(BImage img, ImageUniverse u, Color color) { var w = _img.W; var h = _img.H; foreach (var p in Pixels) { var N = PixelNeighbours(p, w, h); if (N.All(n => u.Parent(n.Index(w)) == RootPixel.Index(w))) { continue; } img.Set(color, p); } }