예제 #1
0
        private void ApplyWhiteBalance(MagickImage image)
        {
            using (MagickImage mask = image.Clone())
            {
                mask.ColorSpace = ColorSpace.HSB;
                mask.Negate(Channels.Green);

                using (MagickImage newMask = mask.Separate(Channels.Green).First())
                {
                    using (MagickImage maskBlue = mask.Separate(Channels.Blue).First())
                    {
                        newMask.Composite(maskBlue, CompositeOperator.Multiply);
                    }

                    newMask.ContrastStretch((Percentage)0, WhiteBalance);
                    newMask.InverseOpaque(new MagickColor("white"), new MagickColor("black"));

                    double maskMean = GetMean(newMask);

                    double redRatio   = GetRatio(image, Channels.Red, newMask, maskMean);
                    double greenRatio = GetRatio(image, Channels.Green, newMask, maskMean);
                    double blueRatio  = GetRatio(image, Channels.Blue, newMask, maskMean);

                    ColorMatrix matrix = new ColorMatrix(3, redRatio, 0, 0, 0, greenRatio, 0, 0, 0, blueRatio);

                    image.ColorMatrix(matrix);
                }
            }
        }
예제 #2
0
 private void EnhanceImage(MagickImage image)
 {
     if (Enhance == TextCleanerEnhance.Stretch)
     {
         image.ContrastStretch((Percentage)0);
     }
     else if (Enhance == TextCleanerEnhance.Normalize)
     {
         image.Normalize();
     }
 }
예제 #3
0
        public IEnumerable <byte> PreProcess(IEnumerable <byte> bitmap)
        {
            using (var processed = new MagickImage(bitmap.ToArray()))
            {
                processed.ContrastStretch(new Percentage(0));

                processed.Morphology(MorphologyMethod.Dilate, Kernel.Diamond, Constants.HighlightIntensity);

                processed.Threshold(new Percentage(20));

                return(processed.ToByteArray());
            }
        }
예제 #4
0
        private void EnhanceImage(MagickImage image)
        {
            if (Enhance == WhiteboardEnhancements.None)
            {
                return;
            }

            if (Enhance.HasFlag(WhiteboardEnhancements.Stretch))
            {
                image.ContrastStretch((Percentage)0, (Percentage)0);
            }

            if (Enhance.HasFlag(WhiteboardEnhancements.Whitebalance))
            {
                ApplyWhiteBalance(image);
            }
        }
예제 #5
0
        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);
            }
        }
예제 #6
0
        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);
        }