public EdgeCutter(bool replaceAll) { Console.WriteLine("replaceAll: " + replaceAll); _replaceAll = replaceAll; _backgroundColor = Color.FromArgb(255, 6, 208, 0).ToDoubleColor(); //_backgroundColor = Color.FromArgb(255, 0, 166, 255).ToDoubleColor(); }
public Bitmap Cut(string path) { Bitmap img = null; using (var image = new Bitmap(path)) { img = new Bitmap(image); image.Dispose(); var diffMax = 0.0; Func <DoubleColor, Double> getColorDiff = (col) => col.B - col.R; // get greedDifMax for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { var col = img.GetPixel(i, j).ToDoubleColor(); var diff = getColorDiff(col); if (diff > diffMax) { diffMax = diff; } } } // backgroundColor to Alpha for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { var col = img.GetPixel(i, j).ToDoubleColor(); // How much more green is there than blue in this specific pixel var diff = getColorDiff(col); // if there IS more green than blue in this pixel, it is a greenish pixel and // needs to be corrected. if (diff > 0) { // correcting greenish pixel in here. // - - calculate the alpha of the new pixel - - // - We already know that it will be a white (1,1,1) pixel. // - We also know that pixels that are (6, 208, 0) will need to get an // alpha of 1. var degreeInDifference = 1 / diffMax * diff; var alpha = degreeInDifference - 1; // since the algorithm which added the green edge to the // text depicted in the image data wasn't doing traditional alpha blending // the alpha needs to be distorted a little to get as close as possible // to the ideal result. alpha *= alpha; var newCol = new DoubleColor(Clamp01(alpha), 1, 1, 1).ToByteColor(); img.SetPixel(i, j, newCol); } } } } return(img); }