/// <summary> /// Traces the edges of a given <see cref="Image"/>. /// </summary> /// <param name="source"> /// The source <see cref="Image"/>. /// </param> /// <param name="destination"> /// The destination <see cref="Image"/>. /// </param> /// <param name="threshold"> /// The threshold (between 0 and 255). /// </param> /// <returns> /// The a new instance of <see cref="Bitmap"/> traced. /// </returns> public static Bitmap Trace(Image source, Image destination, byte threshold = 0) { int width = source.Width; int height = source.Height; // Grab the edges converting to greyscale, and invert the colors. var filter = new ConvolutionFilter(new SobelEdgeFilter(), true); using (Bitmap temp = filter.Process2DFilter(source)) { destination = new InvertMatrixFilter().TransformImage(temp, destination); // Darken it slightly to aid detection destination = Adjustments.Brightness(destination, -5); } // Loop through and replace any colors more white than the threshold // with a transparent one. using (var destinationBitmap = new FastBitmap(destination)) { Parallel.For( 0, height, y => { for (int x = 0; x < width; x++) { // ReSharper disable AccessToDisposedClosure Color color = destinationBitmap.GetPixel(x, y); if (color.B >= threshold) { destinationBitmap.SetPixel(x, y, Color.Transparent); } // ReSharper restore AccessToDisposedClosure } }); } // Darken it again to average out the color. destination = Adjustments.Brightness(destination, -5); return((Bitmap)destination); }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; IMatrixFilter matrix = null; try { // Dont use an object initializer here. newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); newImage.Tag = image.Tag; switch ((string)this.DynamicParameter) { case "polaroid": matrix = new PolaroidMatrixFilter(); break; case "lomograph": matrix = new LomographMatrixFilter(); break; case "sepia": matrix = new SepiaMatrixFilter(); break; case "blackwhite": matrix = new BlackWhiteMatrixFilter(); break; case "greyscale": matrix = new GreyScaleMatrixFilter(); break; case "gotham": matrix = new GothamMatrixFilter(); break; case "invert": matrix = new InvertMatrixFilter(); break; case "hisatch": matrix = new HiSatchMatrixFilter(); break; case "losatch": matrix = new LoSatchMatrixFilter(); break; case "comic": matrix = new ComicMatrixFilter(); break; } if (matrix != null) { return(matrix.TransformImage(factory, image, newImage)); } } catch { if (newImage != null) { newImage.Dispose(); } } return(image); }