public void ShouldThresholdTheImage() { using (var image = new MagickImage(Files.MagickNETIconPNG)) { image.AdaptiveThreshold(10, 10); ColorAssert.Equal(MagickColors.White, image, 50, 75); } }
public void Compress() { using (MagickImage image = new MagickImage(_filePath)) { image.Quality = 3; image.Contrast(true); image.Grayscale(PixelIntensityMethod.Lightness); image.AdaptiveThreshold(320, 240); image.Write(Destination); } }
public void ShouldUseTheCorrectDefaultValueForBiasWithChannels() { using (var imageA = new MagickImage(Files.MagickNETIconPNG)) { using (var imageB = imageA.Clone()) { imageA.AdaptiveThreshold(10, 10, Channels.Red); imageB.AdaptiveThreshold(10, 10, 0.0, Channels.Red); var distortion = imageA.Compare(imageB, ErrorMetric.RootMeanSquared); Assert.Equal(0.0, distortion); } } }
private void CopyOpacity(MagickImage image) { image.Alpha(AlphaOption.Off); using (MagickImage gray = image.Clone()) { gray.ColorSpace = ColorSpace.Gray; gray.Negate(); gray.AdaptiveThreshold(FilterSize, FilterSize, FilterOffset); gray.ContrastStretch((Percentage)0); if (Threshold.HasValue) { gray.Blur((double)Threshold.Value / 100.0, Quantum.Max); gray.Level(Threshold.Value, new Percentage(100)); } image.Composite(gray, CompositeOperator.CopyAlpha); image.Opaque(MagickColor.Transparent, BackgroundColor); image.Alpha(AlphaOption.Off); } }
private void RemoveNoise(MagickImage image) { using (MagickImage second = image.Clone()) { second.ColorSpace = ColorSpace.Gray; second.Negate(); second.AdaptiveThreshold(FilterSize, FilterSize, FilterOffset); second.ContrastStretch((Percentage)0); if (SmoothingThreshold != null) { second.Blur(SmoothingThreshold.Value.ToDouble() / 100, Quantum.Max); second.Level(SmoothingThreshold.Value, new Percentage(100)); } image.Composite(second, CompositeOperator.CopyAlpha); } image.Opaque(MagickColor.FromRgba(255, 255, 255, 0), BackgroundColor); image.Alpha(AlphaOption.Off); }
private static System.Drawing.Image EnchanceDPI(System.Drawing.Image DPIImage) { //Bitmap currentImage = new Bitmap(ImageDirectory); //Bitmap cleanImage = CleanText.Resize(currentImage, currentImage.Width, currentImage.Height); //using (MagickImage img = new MagickImage(cleanImage)) //{ // img.Write("ManualImage.tif"); //} // QuantizeSettings qs = new QuantizeSettings(); // qs.ColorSpace = ColorSpace.Gray; // image.Quantize(qs); //image.ColorType = ColorType.Grayscale; //image.ContrastStretch(new Percentage(0), new Percentage(0)); //QuantizeSettings qs2 = new QuantizeSettings(); //image.Negate(); //qs2.ColorSpace = ColorSpace.GRAY; //image.Quantize(qs2); //image.ColorType = ColorType.Grayscale; //image.AdaptiveThreshold(30, 30, 10); // image.BackgroundColor = Color.White; //image.AdaptiveThreshold(15, 15, 10); //image.ContrastStretch(0, 0); //ImageOptimizer optimizer = new ImageOptimizer(); //optimizer.LosslessCompress(image.FileName); // image.Negate(); MagickNET.Initialize(@"C:\OCR\File\"); // Read first frame of gif image using (MagickImage image = new MagickImage("Snakeware.gif")) { image.Density = new PointD(600, 600); image.AutoLevel(); image.Negate(); image.AdaptiveThreshold(30, 30, 10); image.Negate(); image.Write("Snakeware.jpg"); } // Write to stream MagickReadSettings settings = new MagickReadSettings(); // Tells the xc: reader the image to create should be 800x600 settings.Width = 800; settings.Height = 600; //settings.Density = 600; using (MemoryStream memStream = new MemoryStream()) { // Create image that is completely purple and 800x600 using (MagickImage image = new MagickImage("xc:purple", settings)) { // Sets the output format to png image.Format = MagickFormat.Png; // Write the image to the memorystream image.Write(memStream); } } return(DPIImage); }
void ValidateDicomPixelData(IFileInfo fi, DicomFile dicomFile, DicomDataset ds) { string modality = GetTagOrUnknown(ds, DicomTag.Modality); string[] imageType = GetImageType(ds); string studyID = GetTagOrUnknown(ds, DicomTag.StudyInstanceUID); string seriesID = GetTagOrUnknown(ds, DicomTag.SeriesInstanceUID); string sopID = GetTagOrUnknown(ds, DicomTag.SOPInstanceUID); // Don't go looking for images in structured reports if (modality == "SR") { return; } try { DicomImage dicomImage = new DicomImage(ds); using (Bitmap oldBmp = dicomImage.RenderImage().AsClonedBitmap()) { using (Bitmap newBmp = new Bitmap(oldBmp)) // Strangle this line is neede for the subsequent Clone call to work { using (Bitmap targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format32bppArgb)) { Process(targetBmp, oldBmp.PixelFormat, targetBmp.PixelFormat, fi, dicomFile, sopID, studyID, seriesID, modality, imageType); // Need to threshold and possibly negate the image for best results // Magick.NET won't read from Bitmap directly in .net core so go via MemoryStream using (MemoryStream memStreamIn = new MemoryStream()) using (MemoryStream memStreamOut = new MemoryStream()) using (MagickImage mi = new MagickImage()) { targetBmp.Save(memStreamIn, ImageFormat.Bmp); memStreamIn.Position = 0; mi.Read(memStreamIn); // Threshold the image to monochrome using a window size of 25 square // The size 25 was determined empirically based on real images (could be larger, less effective if smaller) mi.AdaptiveThreshold(25, 25); // Write out to memory, can't reuse memStreamIn here as it breaks mi.Write(memStreamOut); memStreamOut.Position = 0; Process(memStreamOut, oldBmp.PixelFormat, targetBmp.PixelFormat, fi, dicomFile, sopID, studyID, seriesID, modality, imageType); // Tesseract only works with black text on white background so run again negated mi.Negate(); memStreamOut.Position = 0; mi.Write(memStreamOut); memStreamOut.Position = 0; Process(memStreamOut, oldBmp.PixelFormat, targetBmp.PixelFormat, fi, dicomFile, sopID, studyID, seriesID, modality, imageType); } //if user wants to rotate the image 90, 180 and 270 degrees // XXX this is done after negation, maybe needs to be done with and without negation? if (_opts.Rotate) { for (int i = 0; i < 3; i++) { //rotate image 90 degrees and run OCR again targetBmp.RotateFlip(RotateFlipType.Rotate90FlipNone); Process(targetBmp, oldBmp.PixelFormat, targetBmp.PixelFormat, fi, dicomFile, sopID, studyID, seriesID, modality, imageType, (i + 1) * 90); } } } } } } catch (Exception e) { // An internal error should cause IsIdentifiable to exit _logger.Info(e, "Could not run Tesseract on '" + fi.FullName + "'"); throw new ApplicationException("Could not run Tesseract on '" + fi.FullName + "'", e); // OR add a message to the report saying we failed to run OCR //string problemField = "PixelData"; //string text = "Error running OCR on pixel data: "+e; //var f = factory.Create(fi, dicomFile, text, problemField, new[] { new FailurePart(text, FailureClassification.PixelText) }); //AddToReports(f); // XXX do we need this? //_tesseractReport.FoundPixelData(fi, sopID, pixelFormat, processedPixelFormat, studyID, seriesID, modality, imageType, meanConfidence, text.Length, text, rotationIfAny); } }
protected override void ExecuteEffect(ref MagickImage input) { input.AdaptiveThreshold(Width, Height, percentage); }