protected ImageBuffer GetBitmap() { string key = "BMP:" + SourcePath; var bmp = bmpCache[key] as ImageBuffer; if (bmp == null) { bmp = new ImageBuffer(new Bitmap(SourcePath)); bmpCache.Add(key, bmp); } return bmp; }
protected ImageBuffer GetBitmap() { string key = CacheKeyPrefix + SourcePath; var bmp = MemoryCache.Default[key] as ImageBuffer; if (bmp == null) { bmp = new ImageBuffer(new Bitmap(SourcePath)); MemoryCache.Default[key] = bmp; Console.WriteLine("Added to cache: " + SourcePath); } return bmp; }
protected ImageBuffer GetBitmap() { string key = CacheKeyPrefix + SourcePath; var bmp = MemoryCache.Default[key] as ImageBuffer; if (bmp == null) { bmp = new ImageBuffer(new Bitmap(SourcePath)); MemoryCache.Default.Add(key, bmp, new DateTimeOffset(DateTime.UtcNow.AddSeconds(60))); Console.WriteLine("Added to cache: " + SourcePath); } return bmp; }
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(NeuralNetwork neuralNetwork, string imagePath, double noiseLevel) { ImageBuffer source, dest; using (var sourceBmp = new Bitmap(imagePath)) { source = new ImageBuffer(sourceBmp); AddNoise(source, noiseLevel); dest = new ImageBuffer(source.Width, source.Height); } var rem = new RecurrentNoiseRemoval(neuralNetwork, source, noiseLevel); rem.RemoveNoise((row) => Console.WriteLine("Processing: {0}/{1}", row, source.Height)); Console.WriteLine("Remove Time: {0}ms, Computation Time: {1}ms", rem.LastRemoveTime.TotalMilliseconds, rem.LastComputationTime.TotalMilliseconds); rem.Dest.Save("result.png", ImageFormat.Png); }
public RecurrentNoiseRemoval(NeuralNetwork network, ImageBuffer source, double noiseLevel) { Contract.Requires(network != null); Contract.Requires(source != null); Contract.Requires(noiseLevel >= 0.0 && noiseLevel <= 1.0); Network = network; VerifyNetwork(); Source = source; NoiseLevel = noiseLevel; Dest = new ImageBuffer(source.Width, source.Height); rNetwork = Network; gNetwork = Network.Clone(); bNetwork = Network.Clone(); rReset = new ResetHandler(rNetwork, false); gReset = new ResetHandler(gNetwork, false); bReset = new ResetHandler(bNetwork, false); size2 = Size / 2; swComp = new Stopwatch(); swRemove = new Stopwatch(); this.noiseLevel = ImgNoise.Features.Helpers.GetNoiseLevel(NoiseLevel); }
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); }
private static Color ReadColor(ImageBuffer source, int x, int y) { x = CutCoord(source.Width, x); y = CutCoord(source.Height, y); return source.SafeGetPixel(x, y); }
private static void ReadBytes(ImageBuffer source, byte[] rBytes, byte[] gBytes, byte[] bBytes, int size, int x, int y) { int size2 = size / 2; int xs = x - size2, xe = x + size2 + 1; int ys = y - size2, ye = y + size2 + 1; int dx = 0, dy = 0; for (int ry = ys; ry < ye; ry++, dy++) { for (int rx = xs; rx < xe; rx++, dx++) { int addr = dy * size + dx; var color = ReadColor(source, rx, ry); rBytes[addr] = color.R; gBytes[addr] = color.G; bBytes[addr] = color.B; } dx = 0; } }
private static void WriteByte(ImageBuffer dest, int x, int y, byte r, byte g, byte b) { if (x < dest.Width && y < dest.Height) { dest.SafeSetPixel(x, y, Color.FromArgb(r, g, b)); } }
private static ImageBuffer GetBitmap(string path) { string key = "BMP:" + path; var bmp = bmpCache[key] as ImageBuffer; if (bmp == null) { bmp = new ImageBuffer(new Bitmap(path)); bmpCache.Add(key, bmp); } return bmp; }