public static UBitmap FromImage(Image image) { var bmp = new Bitmap(image); var ubmp = new UBitmap(image.Width, image.Height); for (var y = 0; y < image.Height - 1; y++) { for (var x = 0; x < image.Width - 1; x++) { ubmp.SetPixel(x, y, bmp.GetPixel(x, y)); } } return(ubmp); }
/// <summary> /// Detect the edges in the image /// </summary> /// <param name="image"></param> /// <returns></returns> public static Image DetectEdges(this Image image) { //Downscale or Upscale //image = image.Resize(500); //image = image.MakeGrayscale3(); var bmp = UBitmap.FromImage(image); var target = new UBitmap(bmp.Width, bmp.Height); //var pixels = new List<Point>(); //Iterate through each pixel in the image row by row //y is row for (var y = 0; y < bmp.Height - 1; y++) { //x is column for (var x = 0; x < bmp.Width - 1; x++) { //Get the pixel represented at the (x,y) coordinates var pixelColor = bmp.GetPixel(x, y); //get the argb color code of the pixel var pixel = pixelColor.ToArgb(); //make sure the pixel is not the first one on either of X and Y planes, because we need to process neighbouring pixels if (x != 0 && y != 0) { //Pixel directly above this one, the names should be self explanatory, so not going into details of those. var top = bmp.GetPixel(x, y - 1).ToArgb(); var left = bmp.GetPixel(x - 1, y).ToArgb(); var bottom = bmp.GetPixel(x, y + 1).ToArgb(); var right = bmp.GetPixel(x + 1, y).ToArgb(); var topLeft = bmp.GetPixel(x - 1, y - 1).ToArgb(); var topRight = bmp.GetPixel(x + 1, y - 1).ToArgb(); var bottomLeft = bmp.GetPixel(x - 1, y + 1).ToArgb(); var bottomRight = bmp.GetPixel(x + 1, y + 1).ToArgb(); //Take all of the neighbouring pixels into an array for easier processing var colors = new int[] { top, left, bottom, right, topLeft, topRight, bottomLeft, bottomRight }; //check whether this pixel is worthy of being added to the 'edge' detection var shouldAdd = colors.Select(color => { //the pixels to compare //pixel is this pixel, color is the neighbouring one var compare = new int[] { pixel, color }; //Find the difference between the two var difference = compare.Max() - compare.Min(); //Find the percentage difference between the two //Also we need a positive number, whether they turn negative or positive, //so we convert any negatives into positives and leave positives untouched //If that didn't make any sense, you should read about 'Math.Abs' (Absolute) function //There should be tonnes of articles,answers and explanations on the internet var percentageDifference = Math.Abs(((double)difference / -1.0d) * 100d); //Check if the difference is atleast 50%, if so, we assume it as an edge return(percentageDifference >= 50.0d); }).Any(b => b); if (shouldAdd) { //Add the pixel as an edge target.SetPixel(x, y, pixelColor); } } } } //We cannot have any messy values in the image's pixels so we convert all of them into white color (-1 is white) for (var i = 0; i < target.Bits.Length; i++) { if (target.Bits[i] == 0) { target.Bits[i] = -1; } } //return the edge detected image return(target.Bitmap); }