public static Bitmap GetColorSlice(object image, Color color, int treshold = 15) { Mat img = new Mat(); img = Stuff.ToMatAnyWay(image); Cv2.InRange(img, new Scalar(color.B - treshold, color.G - treshold, color.R - treshold), new Scalar(color.B + treshold, color.G + treshold, color.R + treshold), img); var bm = BitmapConverter.ToBitmap(img); return(bm); }
public void Process() { if (Bitmap == null) { return; } if (ColorsInput != null) { if (ColorsInput is Color[]) { var cols = ((Color[])ColorsInput).ToArray(); Colors.Clear(); Colors.AddRange(cols); } if (ColorsInput is object[]) { var cols = ((object[])ColorsInput).Where(z => z != null).Select(z => (Color)z).ToArray(); Colors.Clear(); Colors.AddRange(cols); } } if (Colors.Count == 0) { return; } Mat mat = new Mat(); Stopwatch sw2 = new Stopwatch(); sw2.Start(); var bmp = Stuff.ToBitmapAnyWay(Bitmap); var bytes = BmpToBytes_Unsafe(bmp); int bitsPerPixel = ((int)bmp.PixelFormat & 0xff00) >> 8; int bytesPerPixel = (bitsPerPixel + 7) / 8; int stride = 4 * ((bmp.Width * bytesPerPixel + 3) / 4); #region parallel version int ww = bmp.Width; int hh = bmp.Height; Parallel.For(0, hh, i => { for (int j = 0; j < ww; j++) { Color?nearest = null; float mindiff = 1e6f; int index = i * stride + j * bytesPerPixel; foreach (var color in Colors) { float diff = 0; diff += Math.Abs(color.R - bytes[index + 2]); diff += Math.Abs(color.G - bytes[index + 1]); diff += Math.Abs(color.B - bytes[index]); if (diff < mindiff) { mindiff = diff; nearest = color; } } bytes[index] = nearest.Value.B; bytes[index + 1] = nearest.Value.G; bytes[index + 2] = nearest.Value.R; } }); #endregion var retb = BytesToBmp(bytes, new Size(bmp.Width, bmp.Height), bmp.PixelFormat); var retm = Stuff.ToMatAnyWay(retb); if (retm.Channels() == 4) { retm = retm.CvtColor(ColorConversion.RGBA2RGB); } Output0 = retm; sw2.Stop(); var wel1 = sw2.Elapsed; var welt1 = sw2.ElapsedTicks; }
public void Process() { if (Colors == null) { return; } var colors = Colors; var mat = Stuff.ToMatAnyWay(Image, true); if (mat == null) { return; } List <BlobObject> ret = new List <BlobObject>(); Stopwatch sw = new Stopwatch(); if (UseParallel) { Parallel.ForEach(colors, color => { zip = new MinesweeperBot.ImageZipQuantificator(); zip.Image = GetColorSlice(mat.Clone(), color); zip.CellSize = 1; zip.Treshold = 0.5f; zip.Process(); be = new MinesweeperBot.BlobExtractor(); be.Net = zip.Output1; be.CellSize = 1; be.Inv = false; be.Process(); var aa = (BlobObject[])be.Output0; foreach (var blobObject in aa) { blobObject.Color = color; } lock (ret) { ret.AddRange(aa); } }); } else { foreach (var color in colors) { zip = new MinesweeperBot.ImageZipQuantificator(); zip.IsBitmapOutputEnable = true; zip.Image = GetColorSlice(mat.Clone(), color); zip.CellSize = 1; zip.Treshold = 0.5f; zip.Process(); be = new BlobExtractor(); be.Net = zip.Output1; be.CellSize = 1; be.Inv = false; be.Process(); var aa = (BlobObject[])be.Output0; foreach (var blobObject in aa) { blobObject.Color = color; } lock (ret) { ret.AddRange(aa); } } } foreach (var blobObject in ret) { blobObject.Id = NewObjectId++; } Output = ret.ToArray(); }