private static void AddNoise(ImageBuffer source, double noiseLevel) { Parallel.For(0, source.Height, (y) => { for (int x = 0; x < source.Width; x++) { var px = source.SafeGetPixel(x, y); byte r = Noise.Add(px.R, noiseLevel), g = Noise.Add(px.G, noiseLevel), b = Noise.Add(px.B, noiseLevel); source.SafeSetPixel(x, y, Color.FromArgb(r, g, b)); } }); source.Save("noised.png", ImageFormat.Png); }
private static void RemoveNoise(NeuralComputation computation, string path, double noiseLevel) { var comps = Enumerable.Range(0, 15).Select(idx => new NoiseRemovalComputation(computation.Clone())).ToList(); comps.Add(new NoiseRemovalComputation(computation)); int compIdx = 0; ImageBuffer source, dest; using (var sourceBmp = new Bitmap(path)) { source = new ImageBuffer(sourceBmp); AddNoise(source, noiseLevel); dest = new ImageBuffer(source.Width, source.Height); } int size = 7, blockSize = 16; int w = source.Width, h = source.Height, soFar = 0, msTime = 0; SpinLock infoLock = new SpinLock(), compLock = new SpinLock(); Parallel.For(0, h / blockSize + 1, (idx, state) => { NoiseRemovalComputation comp; bool compTaken = false; try { compLock.Enter(ref compTaken); comp = comps[compIdx++ % comps.Count]; } finally { if (compTaken) compLock.Exit(); } //Console.WriteLine(compIdx); int pos = idx * blockSize; var rBytes = new byte[size * size]; var bBytes = new byte[size * size]; var gBytes = new byte[size * size]; Color[] sourceColors = new Color[size * size]; for (int y = pos; y < pos + blockSize && y < h; y++) { var sw = new Stopwatch(); for (int x = 0; x < w; x++) { ReadBytes(source, rBytes, gBytes, bBytes, size, x, y); sw.Start(); byte destByteR = comp.Compute(rBytes, noiseLevel); byte destByteG = comp.Compute(gBytes, noiseLevel); byte destByteB = comp.Compute(bBytes, noiseLevel); sw.Stop(); WriteByte(dest, x, y, destByteR, destByteG, destByteB); } bool infoTaken = false; try { infoLock.Enter(ref infoTaken); msTime += (int)sw.ElapsedMilliseconds; Console.WriteLine("Processing line: {0} / {1}", ++soFar, h); } finally { if (infoTaken) infoLock.Exit(); } } }); Console.WriteLine("Comp: {0}ms", msTime); dest.Save("result.png", ImageFormat.Png); }