public void ShouldThresholdTheSpecifiedChannel() { using (var image = new MagickImage(Files.MagickNETIconPNG)) { image.BlackThreshold(new Percentage(90), Channels.Green); ColorAssert.Equal(new MagickColor("#3400bd"), image, 43, 74); ColorAssert.Equal(new MagickColor("#a800f8"), image, 60, 74); } }
public void ShouldThresholdTheImage() { using (var image = new MagickImage(Files.MagickNETIconPNG)) { image.BlackThreshold(new Percentage(90)); ColorAssert.Equal(MagickColors.Black, image, 43, 74); ColorAssert.Equal(new MagickColor("#0000f8"), image, 60, 74); } }
private static byte[] PreprocessImage(IMagickImage sourceImage, Rectangle cropRegion, float scale, string cropRegionPath = "") { using var mImage = new MagickImage(MagickColors.White, cropRegion.Width, cropRegion.Height); mImage.Format = MagickFormat.Png; if (cropRegion.Width > 0 && cropRegion.Height > 0) { mImage.CopyPixels(sourceImage, new MagickGeometry(cropRegion.X, cropRegion.Y, cropRegion.Width, cropRegion.Height)); } else { mImage.CopyPixels(sourceImage); } mImage.Threshold(new Percentage(65f)); // 60 is OK mImage.Depth = 1; // mImage.GammaCorrect(0.20); //0.20 // mImage.BlackThreshold(new Percentage(50f), Channels.RGB); mImage.ColorSpace = ColorSpace.Gray; mImage.Negate(Channels.RGB); if (scale != 0f && !(Math.Abs(scale - 1f) < float.Epsilon)) { mImage.FilterType = FilterType.Cubic; mImage.Resize(new Percentage(scale * 100.0f)); } mImage.BlackThreshold(new Percentage(55f), Channels.RGB); mImage.GaussianBlur(0.85, 0.85); // mImage.BlackThreshold(new Percentage(50f), Channels.RGB); // mImage.MedianFilter(2); // mImage.BlackThreshold(new Percentage(20f), Channels.RGB); if (!string.IsNullOrEmpty(cropRegionPath)) { mImage.Write(cropRegionPath); } return(mImage.ToByteArray()); }