Example #1
0
        private void PosterizationC(object obj)
        {
            Bitmap bmp = null;

            try
            {
                bmp = (Bitmap)Images.Instance.CurrentBitmap.Clone();
            }
            catch { }
            Bitmap bmpselected = null;

            try
            {
                bmpselected = (Bitmap)Images.Instance.SelectedBitmap.Clone();
            }
            catch { }
            if (partarea == true && bmpselected != null)
            {
                posterizationFilter.ApplyInPlace(bmpselected);
                Graphics newImage = Graphics.FromImage(bmp);
                newImage.DrawImage(bmpselected, (int)SelectedPoints.selectPointForEfX, (int)SelectedPoints.selectPointForEfY, bmpselected.Width, bmpselected.Height);
            }
            else if (bmp != null)
            {
                posterizationFilter.ApplyInPlace(bmp);
            }
            Images.Instance.CurrentBitmap = bmp;
            Images.Instance.NotifyImages();
            this.Temporary = bmp;
        }
        private Bitmap SimplePosterization(Bitmap image)
        {
            SimplePosterization filter = new SimplePosterization();

            // process image
            filter.ApplyInPlace(image);
            return(image);
        }
Example #3
0
        private Bitmap preProcessImage(Bitmap image, Grayscale gScale, SobelEdgeDetector sEgDetector, SimplePosterization posterization)
        {
            Bitmap temp = image;

            temp = gScale.Apply(temp);
            posterization.ApplyInPlace(temp);


            return(temp);
        }
Example #4
0
        /// <summary>
        /// Returns true if the average energy of the image is below the given threshold
        /// </summary>
        /// <param name="image"></param>
        /// <param name="threshold"></param>
        /// <returns></returns>
        private bool IsBlank(byte[] image, byte threshold)
        {
            var ms = new MemoryStream(image);

            using (var b = new Bitmap(ms))
            {
                using (var gray = Grayscale.CommonAlgorithms.BT709.Apply(b))
                {
                    new SobelEdgeDetector().ApplyInPlace(gray);
                    var p = new SimplePosterization(SimplePosterization.PosterizationFillingType.Average);
                    p.PosterizationInterval = 128;
                    p.ApplyInPlace(gray);
                    return(gray.GetPixel(0, 0).R < threshold);
                }
            }
        }
Example #5
0
        protected void ApplyFiltersTo(ref Bitmap b, ImageState s)
        {
            var imageRectF   = s.layout.GetRingAsRectF("image");
            var areaOfEffect = imageRectF == null ? new Rectangle(0, 0, b.Width, b.Height) : PolygonMath.ToRectangleShrinkRound(imageRectF.Value);

            //TODO: if the image is unrotated, use a rectangle to limit the effect to the desired area

            string str = null;
            int    i   = 0;

            //If radiusunits is specified, use that code path.
            double units = s.settings.Get <double>("a.radiusunits", 1000);

            //TODO: move blur/sharpen aliases to a compatibility shim, deprecate them

            i = GetRadius(s, "blur", "a.blur", units);
            if (i > 0)
            {
                new GaussianBlur(1.4, i).ApplyInPlace(b, areaOfEffect);
            }

            i = GetRadius(s, "sharpen", "a.sharpen", units);
            if (i > 0)
            {
                new GaussianSharpen(1.4, Math.Min(11, i)).ApplyInPlace(b, areaOfEffect);
            }

            i = GetRadius(s, "a.oilpainting", null, units);
            if (i > 0)
            {
                new OilPainting(i).ApplyInPlace(b, areaOfEffect);
            }

            if ("true".Equals(s.settings["a.removenoise"], StringComparison.OrdinalIgnoreCase))
            {
                new ConservativeSmoothing(3).ApplyInPlace(b, areaOfEffect);
            }
            else
            {
                i = GetRadius(s, "a.removenoise", null, units);
                if (i > 0)
                {
                    new ConservativeSmoothing(i).ApplyInPlace(b, areaOfEffect);
                }
            }



            //Sobel only supports 8bpp grayscale images.
            //true/false
            if ("true".Equals(s.settings["a.sobel"], StringComparison.OrdinalIgnoreCase))
            {
                Bitmap old = b;
                try{
                    b = Grayscale.CommonAlgorithms.Y.Apply(b);
                }finally{
                    if (old != s.sourceBitmap)
                    {
                        old.Dispose();
                    }
                }

                new SobelEdgeDetector().ApplyInPlace(b, areaOfEffect);

                str = s.settings["a.threshold"]; //radius
                if (!string.IsNullOrEmpty(str) && int.TryParse(str, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out i) && i > 0)
                {
                    new Threshold(i).ApplyInPlace(b, areaOfEffect);
                }
            }
            //Canny Edge Detector only supports 8bpp grayscale images.
            //true/false
            if ("true".Equals(s.settings["a.canny"], StringComparison.OrdinalIgnoreCase))
            {
                Bitmap old = b;
                try {
                    b = Grayscale.CommonAlgorithms.Y.Apply(b);
                } finally {
                    if (old != s.sourceBitmap)
                    {
                        old.Dispose();
                    }
                }
                new CannyEdgeDetector().ApplyInPlace(b, areaOfEffect);
            }

            //true/false - duplicate with SimpleFilters?
            if ("true".Equals(s.settings["a.sepia"], StringComparison.OrdinalIgnoreCase))
            {
                new Sepia().ApplyInPlace(b, areaOfEffect);
            }

            //true/false
            if ("true".Equals(s.settings["a.equalize"], StringComparison.OrdinalIgnoreCase))
            {
                new HistogramEqualization().ApplyInPlace(b, areaOfEffect);
            }

            //White balance adjustment
            var whiteAlg = s.settings.Get <HistogramThresholdAlgorithm>("a.balancewhite");
            var whiteVal = s.settings.Get <double>("a.balancethreshold");


            if (whiteAlg != null || whiteVal != null)
            {
                var bal = new AutoWhiteBalance(whiteAlg ?? HistogramThresholdAlgorithm.Area);
                if (whiteVal != null)
                {
                    bal.LowThreshold = bal.HighThreshold = whiteVal.Value / 100;
                }
                bal.ApplyInPlace(b, areaOfEffect);
            }

            str = s.settings["a.posterize"]; //number of colors to merge
            if (!string.IsNullOrEmpty(str) && int.TryParse(str, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out i) && i > 0)
            {
                SimplePosterization sp = new SimplePosterization();
                if (i < 1)
                {
                    i = 1;
                }
                if (i > 255)
                {
                    i = 255;
                }
                sp.PosterizationInterval = (byte)i;
                sp.ApplyInPlace(b, areaOfEffect);
            }

            //Pixelate doesn't support 32-bit images, only 24-bit
            //str = s.settings["a.pixelate"]; //number of colors to merge
            //if (!string.IsNullOrEmpty(str) && int.TryParse(str, out i)){
            //     if (i < 2) i = 2;
            //    if (i > 32) i = 32;
            //    new Pixelate(i).ApplyInPlace(s.destBitmap);
            //}


            float contrast   = s.settings.Get <float>("a.contrast", 0);
            float brightness = s.settings.Get <float>("a.brightness", 0);
            float saturation = s.settings.Get <float>("a.saturation", 0);

            if (contrast != 0 || brightness != 0 || saturation != 0)
            {
                HSLLinear adjust = new HSLLinear();
                AdjustContrastBrightnessSaturation(adjust, contrast, brightness, saturation, "true".Equals(s.settings["a.truncate"]));
                adjust.ApplyInPlace(b, areaOfEffect);
            }
            //TODO - add grayscale?

            //For adding fax-like thresholding, use BradleyLocalThresholding

            //For trimming solid-color whitespace, use Shrink
        }
Example #6
0
        public void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            time.Start();
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();

            if (flag_rotate != 0)
            {
                if (flag_rotate == 1)
                {
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                }
                else if (flag_rotate == 2)
                {
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                }
                else
                {
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                }
            }
            if (flag_smooth == 1)
            {
                img = grayscale.Apply(img);
            }
            if (trackBar1.Value != 0)
            {
                img = br.Apply(img);
            }
            if (flag_flipX == 1)
            {
                img.RotateFlip(RotateFlipType.RotateNoneFlipX);
            }
            if (flag_flipY == 1)
            {
                img.RotateFlip(RotateFlipType.RotateNoneFlipY);
            }
            if (flag_sharpen == 1)
            {
                sharpen.ApplyInPlace(img);
            }
            if (flag_treshold == 1)
            {
                marker.ApplyInPlace(img);
            }
            if (flag_edge == 1)
            {
                rubovi.ApplyInPlace(img);
            }
            if (flag_dither == 1)
            {
                poster_filter.ApplyInPlace(img);
            }
            if (flag_invert == 1)
            {
                invert_filter.ApplyInPlace(img);
            }
            pbx_image.Image = img;
            time.Stop();
            lbl_time.Text = "Frame times: " + time.ElapsedMilliseconds + " ms";
            time.Reset();
        }