Example #1
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        /// <param name="bmSrc">Bitmap data</param>
        private unsafe void ApplyHSL(BitmapData bmData, BitmapData bmSrc)
        {
            byte *p = (byte *)bmData.Scan0.ToPointer(), pSrc = (byte *)bmSrc.Scan0.ToPointer();
            int   width = bmData.Width, height = bmData.Height, stride = bmData.Stride;
            float length = values.GetLength(0) - 1;

            Parallel.For(0, height, j =>
            {
                HSL lumI; HSL lumIx; RGB rgb;
                int i, k, k1, k2, jstride = j * stride;

                for (i = 0; i < width; i++)
                {
                    k = jstride + i * 4; k1 = k + 1; k2 = k + 2;

                    lumI           = HSL.FromRGB(p[k2], p[k1], p[k]);
                    lumIx          = HSL.FromRGB(pSrc[k2], pSrc[k1], pSrc[k]);
                    lumI.Lightness = this.values[(int)(lumI.Lightness * length), (int)(lumIx.Lightness * length)];
                    rgb            = lumI.ToRGB;

                    p[k2] = rgb.Red; p[k1] = rgb.Green; p[k] = rgb.Blue;
                }
            }
                         );
        }
Example #2
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        private unsafe void ApplyHSL(BitmapData bmData)
        {
            byte *p = (byte *)bmData.Scan0.ToPointer();
            int   width = bmData.Width, height = bmData.Height, stride = bmData.Stride;
            float length = values.Length - 1;

            Parallel.For(0, height, y =>
            {
                int x, ystride, k;
                HSL hsl; RGB rgb;

                ystride = y * stride;

                for (x = 0; x < width; x++)
                {
                    k             = ystride + x * 4;
                    hsl           = HSL.FromRGB(p[k + 2], p[k + 1], p[k]);
                    hsl.Lightness = values[(int)(hsl.Lightness * length)];
                    rgb           = hsl.ToRGB;
                    p[k + 2]      = rgb.Red; p[k + 1] = rgb.Green; p[k] = rgb.Blue;
                }
            }
                         );

            return;
        }
Example #3
0
        public void fromYCbCr()
        {
            #region doc_ycbcr
            // This example shows how to convert to and from various
            // pixel representations. For example, let's say we start
            // with a YCbCr pixel with the value (0.8, 0.2, 0.5):
            YCbCr yCbCr = new YCbCr(y: 0.8f, cb: 0.2f, cr: 0.5f);

            // The value of this pixel in RGB format would be:
            RGB rgb = yCbCr.ToRGB();  // should be (255, 95, 255)

            // The value of this pixel in HSL format would be:
            HSL hsl = HSL.FromRGB(rgb); // should be (299, 0.99999994, 0.6862745)

            // Note: we can also convert using casting:
            RGB a = (RGB)yCbCr;
            HSL b = (HSL)yCbCr;
            #endregion

            Assert.AreEqual(255, rgb.Red);
            Assert.AreEqual(95, rgb.Green);
            Assert.AreEqual(255, rgb.Blue);
            Assert.AreEqual(255, rgb.Alpha);
            Assert.AreEqual(299, hsl.Hue, 1);
            Assert.AreEqual(0.99999994, hsl.Saturation, 1e-6);
            Assert.AreEqual(0.6862745, hsl.Luminance, 1e-6);
            Assert.AreEqual(0.8, yCbCr.Y, 1e-6);
            Assert.AreEqual(0.2, yCbCr.Cb, 1e-6);
            Assert.AreEqual(0.5, yCbCr.Cr, 1e-6);

            Assert.AreEqual(rgb, a);
            Assert.AreEqual(hsl, b);
        }
Example #4
0
        public void fromRGB()
        {
            #region doc_rgb
            // This example shows how to convert to and from various
            // pixel representations. For example, let's say we start
            // with a RGB pixel with the value (24, 250, 153):
            RGB rgb = new RGB(red: 24, green: 250, blue: 153);

            // The value of this pixel in HSL format would be:
            HSL hsl = HSL.FromRGB(rgb);       // should be (154, 0.9576271, 0.5372549)

            // The value of this pixel in YCbCr format would be:
            YCbCr yCbCr = YCbCr.FromRGB(rgb); // should be (0.671929836, -0.0406815559, -0.412097245)

            // Note: we can also convert using casting:
            HSL   a = (HSL)rgb;
            YCbCr b = (YCbCr)rgb;
            #endregion

            Assert.AreEqual(24, rgb.Red);
            Assert.AreEqual(250, rgb.Green);
            Assert.AreEqual(153, rgb.Blue);
            Assert.AreEqual(255, rgb.Alpha);
            Assert.AreEqual(154, hsl.Hue);
            Assert.AreEqual(0.9576271, hsl.Saturation, 1e-6);
            Assert.AreEqual(0.5372549, hsl.Luminance, 1e-6);
            Assert.AreEqual(0.671929836, yCbCr.Y, 1e-6);
            Assert.AreEqual(-0.0406815559, yCbCr.Cb, 1e-6);
            Assert.AreEqual(-0.412097245, yCbCr.Cr, 1e-6);

            Assert.AreEqual(hsl, a);
            Assert.AreEqual(yCbCr, b);
        }
Example #5
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap</param>
        public unsafe void Apply(BitmapData bmData)
        {
            byte *p = (byte *)bmData.Scan0.ToPointer();
            int   width = bmData.Width, height = bmData.Height, stride = bmData.Stride;

            Parallel.For(0, height, j =>
            {
                HSL hsl; RGB rgb;
                int i, k, jstride = j * stride;

                for (i = 0; i < width; i++)
                {
                    k = jstride + i * 4;

                    hsl = HSL.FromRGB(p[k + 2], p[k + 1], p[k + 0]);

                    hsl.Hue         = Maths.Scale(hsl.Hue + hue, 0, 360);
                    hsl.Saturation += saturation;
                    hsl.Lightness  += lightness;
                    rgb             = hsl.ToRGB;

                    p[k + 0] = rgb.Blue;
                    p[k + 1] = rgb.Green;
                    p[k + 2] = rgb.Red;
                }
            }
                         );

            return;
        }
Example #6
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num  = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int   left = rect.Left;
            int   top  = rect.Top;
            int   num2 = left + rect.Width;
            int   num3 = top + rect.Height;
            int   num4 = image.Stride - rect.Width * num;
            RGB   rGB  = new RGB();
            HSL   hSL  = new HSL();
            byte *ptr  = (byte *)image.ImageData.ToPointer();

            ptr += top * image.Stride + left * num;
            for (int i = top; i < num3; i++)
            {
                int num5 = left;
                while (num5 < num2)
                {
                    rGB.Red   = ptr[2];
                    rGB.Green = ptr[1];
                    rGB.Blue  = *ptr;
                    HSL.FromRGB(rGB, hSL);
                    hSL.Hue = hue;
                    HSL.ToRGB(hSL, rGB);
                    ptr[2] = rGB.Red;
                    ptr[1] = rGB.Green;
                    *ptr = rGB.Blue;
                    num5++;
                    ptr += num;
                }
                ptr += num4;
            }
        }
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num    = Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int   left   = rect.Left;
            int   top    = rect.Top;
            int   num4   = left + rect.Width;
            int   num5   = top + rect.Height;
            int   num6   = image.Stride - (rect.Width * num);
            RGB   rgb    = new RGB();
            HSL   hsl    = new HSL();
            byte *numPtr = (byte *)(image.ImageData.ToPointer() + ((top * image.Stride) + (left * num)));

            for (int i = top; i < num5; i++)
            {
                int num8 = left;
                while (num8 < num4)
                {
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    HSL.FromRGB(rgb, hsl);
                    hsl.Hue = this.hue;
                    HSL.ToRGB(hsl, rgb);
                    numPtr[2] = rgb.Red;
                    numPtr[1] = rgb.Green;
                    numPtr[0] = rgb.Blue;
                    num8++;
                    numPtr += num;
                }
                numPtr += num6;
            }
        }
 public void AddBitmap(Bitmap anotherLayer)
 {
     for (int y = 0; y < OriginalImage.Height; y++)
     {
         for (int x = 0; x < OriginalImage.Width; x++)
         {
             var pixel = anotherLayer.GetPixel(x, y);
             RGB rgb   = new RGB(pixel);
             Matrix[y, x] += HSL.FromRGB(rgb).Luminance;
         }
     }
 }
Example #9
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        public unsafe void Apply(BitmapData bmData)
        {
            byte *p = (byte *)bmData.Scan0.ToPointer();
            int   width = bmData.Width, height = bmData.Height, stride = bmData.Stride;

            Parallel.For(0, height, j =>
            {
                HSL hsl; RGB rgb;
                int i, k, jstride = j * stride;

                for (i = 0; i < width; i++)
                {
                    // This function modifies a given image in order to keep a specific hue
                    // (given too) and to desaturate the rest of the image. This procedure
                    // originates a image with black and white colormap, excluding the parts
                    // colored with that hue.
                    // Victor Martnez Cagigal, 23/02/2015
                    //
                    // Designed for UMapx.NET by Valery Asiryan, 2018.

                    k = jstride + i * 4;

                    // Convert to hsl:
                    hsl = HSL.FromRGB(p[k + 2], p[k + 1], p[k + 0]);

                    // Getting hue and saturation parameters:
                    float hue = hsl.Hue, saturation = hsl.Saturation;

                    // Applying filter:
                    if (min < max)
                    {
                        hsl.Saturation = (hue > min && hue < max) ? saturation : 0;
                    }
                    else
                    {
                        hsl.Saturation = ((hue > min && hue <= 360) || (hue < max && hue >= 0)) ? saturation : 0;
                    }

                    // Convert to rgb:
                    rgb = hsl.ToRGB;

                    p[k + 0] = rgb.Blue;
                    p[k + 1] = rgb.Green;
                    p[k + 2] = rgb.Red;
                }
            }
                         );

            return;
        }
Example #10
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        /// <param name="bmMax">Bitmap data</param>
        /// <param name="bmMin">Bitmap data</param>
        private unsafe void ApplyHSL(BitmapData bmData, BitmapData bmMax, BitmapData bmMin)
        {
            byte *p = (byte *)bmData.Scan0.ToPointer();
            byte *pMax = (byte *)bmMax.Scan0.ToPointer();
            byte *pMin = (byte *)bmMin.Scan0.ToPointer();
            int   width = bmData.Width, height = bmData.Height, stride = bmData.Stride;

            float required = 1.0f - this.contrast;

            Parallel.For(0, height, j =>
            {
                HSL imag; HSL imax; HSL imin; RGB rgb;
                int i, k, k1, k2, jstride = j * stride;
                float mag, max, min;
                float num1, num2, num3;

                for (i = 0; i < width; i++)
                {
                    k = jstride + i * 4; k1 = k + 1; k2 = k + 2;

                    imag = HSL.FromRGB(p[k2], p[k1], p[k]);
                    imax = HSL.FromRGB(pMax[k2], pMax[k1], pMax[k]);
                    imin = HSL.FromRGB(pMin[k2], pMin[k1], pMin[k]);

                    mag = imag.Lightness;
                    max = imax.Lightness;
                    min = imin.Lightness;

                    num1 = max - min;

                    if (num1 < required)
                    {
                        num2 = min + (required - num1) * min / (num1 - 1f);
                        min  = Maths.Float(num2);
                        max  = Maths.Float(num2 + required);
                    }

                    num1 = max - min;
                    num3 = mag - min;

                    if (num1 > 0)
                    {
                        imag.Lightness = num3 / num1;
                        rgb            = imag.ToRGB;
                        p[k2]          = rgb.Red; p[k1] = rgb.Green; p[k] = rgb.Blue;
                    }
                }
            }
                         );
        }
Example #11
0
        public wColor SetLuminance(double LuminanceValue)
        {
            HSL hsl = HSL.FromRGB(new RGB((byte)WindColor.R, (byte)WindColor.G, (byte)WindColor.B));

            hsl.Luminance = (float)LuminanceValue;

            RGB rgb = hsl.ToRGB();

            WindColor.R = rgb.Red;
            WindColor.G = rgb.Green;
            WindColor.B = rgb.Blue;

            return(WindColor);
        }
        public ImagesNormalizer(Bitmap image)
        {
            OriginalImage = image;
            Matrix        = new float[image.Height, image.Width];

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    var pixel = image.GetPixel(x, y);
                    RGB rgb   = new RGB(pixel);
                    Matrix[y, x] = HSL.FromRGB(rgb).Luminance;
                }
            }
        }
Example #13
0
        internal static double GetSat(Matrix <double> outputValues, int subdivisions)
        {
            var satCounter = 0.0;

            for (int i = 0; i < outputValues.RowCount; i++)
            {
                var rgb = new RGB((byte)(outputValues[i, 0] * 255), (byte)(outputValues[i, 1] * 255), (byte)(outputValues[i, 2] * 255));
                var sat = HSL.FromRGB(rgb).Saturation;
                satCounter += sat;
            }

            var meanSat = satCounter / outputValues.RowCount;

            return(meanSat);
        }
Example #14
0
        internal static double GetSatVar(Matrix <double> outputValues, int subdivisions)
        {
            List <double> sats = new List <double>();

            for (int i = 0; i < outputValues.RowCount; i++)
            {
                var rgb = new RGB((byte)(outputValues[i, 0] * 255), (byte)(outputValues[i, 1] * 255), (byte)(outputValues[i, 2] * 255));
                var sat = HSL.FromRGB(rgb).Saturation;
                sats.Add(sat);
            }

            var variance = MathNet.Numerics.Statistics.Statistics.StandardDeviation(sats);

            return(variance);
        }
Example #15
0
        internal static double GetHue(Matrix <double> values, int subdivisions)
        {
            var hueCounter = 0;

            for (int i = 0; i < values.RowCount; i++)
            {
                var rgb = new RGB((byte)(values[i, 0] * 255), (byte)(values[i, 1] * 255), (byte)(values[i, 2] * 255));
                var hue = HSL.FromRGB(rgb).Hue;
                hueCounter += hue;
            }

            var meanHue = hueCounter / values.RowCount;

            return(meanHue);
        }
Example #16
0
        internal static double GetHueDist(Matrix <double> values, Matrix <double> targets, int subdivisions)
        {
            var hueCounter = 0;
            var valDist    = new double[10];
            var tarDist    = new double[10];

            var sizeRatio = targets.RowCount / values.RowCount;

            for (int i = 0; i < values.RowCount; i++)
            {
                var rgb = new RGB((byte)(values[i, 0] * 255), (byte)(values[i, 1] * 255), (byte)(values[i, 2] * 255));
                var hue = HSL.FromRGB(rgb).Hue;

                int bucket = (int)hue / 36;
                if (bucket > 9)
                {
                    bucket = 9;
                }

                valDist[bucket]++;
                // hueCounter += hue;
            }

            for (int i = 0; i < targets.RowCount; i++)
            {
                var rgb = new RGB((byte)(targets[i, 0] * 255), (byte)(targets[i, 1] * 255), (byte)(targets[i, 2] * 255));
                var hue = HSL.FromRGB(rgb).Hue;

                int bucket = (int)hue / 36;
                if (bucket > 9)
                {
                    bucket = 9;
                }

                tarDist[bucket]++;
                //hueCounter += hue;
            }

            valDist = valDist.Select(x => x / (subdivisions * subdivisions)).ToArray();
            tarDist = tarDist.Select(x => x / (sizeRatio * (subdivisions * subdivisions))).ToArray();

            var distDist = FeatureDistance(valDist, tarDist);

            return(distDist);
        }
Example #17
0
        public mFilterHSL(wDomain HueValue, wDomain SaturationValue, wDomain LuminanceValue, bool isOut, Color fillColor)
        {
            Hue       = HueValue;
            Sat       = SaturationValue;
            Lum       = LuminanceValue;
            IsOut     = isOut;
            FillColor = fillColor;


            BitmapType = mFilter.BitmapTypes.None;

            Effect = new HSLFiltering(new Accord.IntRange((int)Hue.T0, (int)Hue.T1), new Accord.Range((float)Sat.T0, (float)Sat.T1), new Accord.Range((float)Lum.T0, (float)Lum.T1));

            Effect.FillOutsideRange = IsOut;
            Effect.FillColor        = HSL.FromRGB(new RGB(FillColor));

            filter = Effect;
        }
Example #18
0
        public void addcouleur(Bitmap bm)
        {
            if (LstZone.Count == 0 || LstZone[LstZone.Count - 1].ID != -1 && col != null)
            {
                int   w_i = bm.Width;
                int   h_i = bm.Height;
                int   w_c = imgReel.Width;
                int   h_c = imgReel.Height;
                float ord = ((float)w_i * (float)((float)this.col[0] / (float)w_c));
                float abs = ((float)h_i * (float)((float)this.col[1] / (float)h_c));
                this.col = null;
                int R = 0, G = 0, B = 0, A = 0;
                int count = 0;
                // Moyenne des pixels
                for (int i = (int)(ord - 1); i < (int)(ord + 1); i++)
                {
                    for (int j = (int)(abs - 1); j < (int)(abs + 1); j++)
                    {
                        count++;
                        Color c = bm.GetPixel(i, j);
                        R += c.R;
                        G += c.G;
                        B += c.B;
                        A += c.A;
                    }
                }
                RGB tmp  = new RGB(((byte)(R / count)), ((byte)(G / count)), ((byte)(B / count)), ((byte)(A / count)));
                HSL colo = HSL.FromRGB(tmp);
                Logger.GlobalLogger.debug("Couleur ajoutée : " + tmp.ToString() + " HLS : " + colo.Hue + " " + colo.Luminance + " " + colo.Saturation);

                HSLFiltering Filter = new HSLFiltering();
                Filter.Hue        = new IntRange((colo.Hue + 340) % 360, (colo.Hue + 20) % 360);
                Filter.Saturation = new Range(0.6f, 1f);
                Filter.Luminance  = new Range(0.1f, 1f);
                LstHslFiltering.Add(Filter);
                PositionZone ZoneTmp = new PositionZone();
                ZoneTmp.ID = -1;
                LstZone.Add(ZoneTmp);
                MessageBox.Show("Ajouter une zone de dépose avec le click milieu");
            }
        }
Example #19
0
        public static Color GetCorrected(Color color, Color background, string name)
        {
            if (cache.ContainsKey(name))
            {
                return(cache[name]);
            }

            if (color == default(Color))
            {
                var random = new Random();
                color = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
            }

            var darkMode = IsDark(background);

            var hsl = HSL.FromRGB(color.R, color.G, color.B);

            // Color correction concept from: https://github.com/fourtf/chatterino
            if (darkMode)
            {
                hsl.L = Math.Max(hsl.L, .5);
                if (hsl.L < .6 && hsl.H > 196 && hsl.H < 300)
                {
                    hsl.L += Math.Sin((hsl.H - 196) / (300 - 196) * Math.PI) * hsl.S * .4;
                }

                if (hsl.L < 0.8f && (hsl.H < 22 || hsl.H > 331))
                {
                    hsl.L += (hsl.S * .1);
                }
            }
            else
            {
                hsl.S = Math.Min(.4, hsl.S);
                hsl.L = Math.Min(.5, hsl.L);
            }

            hsl.L = Math.Min(.95, hsl.L);

            return(cache[name] = HSL.ToRGB(hsl));
        }
Example #20
0
        public FundusFilterBase(Bitmap inputBitmap, bool storeAsColor)
        {
            this.storeAsColor = storeAsColor;
            _inputBitmap      = inputBitmap;
            _inputLuminance   = new float[_inputBitmap.Height, _inputBitmap.Width];
            _inputHue         = new int[_inputBitmap.Height, _inputBitmap.Width];
            _inputSaturation  = new float[_inputBitmap.Height, _inputBitmap.Width];
            _outputLuminance  = new float[_inputBitmap.Height, _inputBitmap.Width];

            for (int x = 0; x < _inputBitmap.Width; x++)
            {
                for (int y = 0; y < _inputBitmap.Height; y++)
                {
                    var   pixelInMask            = inputBitmap.GetPixel(x, y);
                    RGB   rgbInMask              = new RGB(pixelInMask);
                    HSL   hslInMask              = HSL.FromRGB(rgbInMask);
                    float luminanceOfPixelInMask = hslInMask.Luminance;
                    _inputLuminance[y, x]  = luminanceOfPixelInMask;
                    _inputHue[y, x]        = hslInMask.Hue;
                    _inputSaturation[y, x] = hslInMask.Saturation;
                }
            }
        }
 static void ForAllPixels(Bitmap image, Action <double[]> m)
 {
     /*
      *      //Slow (2x times) but safe
      *      for (int i = 0; i < image.Width; i++)
      *      for (int j = 0; j < image.Height; j++)
      *              m(HSL.FromRGB(new RGB(image.GetPixel(i, j))));
      */
     using (var unmanagedImg = UnmanagedImage.FromManagedImage(image))
     {
         CheckSourceFormat(unmanagedImg.PixelFormat);
         var pixelSize = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
         int pixelCnt  = unmanagedImg.Height * unmanagedImg.Width;
         unsafe
         {
             var p = (byte *)unmanagedImg.ImageData.ToPointer();
             for (int pixels = 0; pixels < pixelCnt; pixels++, p += pixelSize)
             {
                 var hsl = HSL.FromRGB(new RGB(p[RGB.R], p[RGB.G], p[RGB.B]));
                 m(new[] { hsl.Hue / 359.0, hsl.Saturation, hsl.Luminance });
             }
         }
     }
 }
Example #22
0
        /// <summary>
        /// Gets an array of pixel luminances for a bitmap
        /// </summary>
        private static double[] GetLuminanceArray(Matrix <double> grayscales, int width)
        {
            //WHY DO I NEED WIDTH HERE?
            var w      = width;
            var h      = width;
            var values = new double[w * h];

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    values[h * i + j] = grayscales[h * i + j, 0];

                    if (grayscales.ColumnCount == 3)
                    {
                        var rgb = new RGB((byte)(grayscales[i, 0] * 255), (byte)(grayscales[i, 1] * 255), (byte)(grayscales[i, 2] * 255));
                        var lum = HSL.FromRGB(rgb).Luminance;
                        values[h * i + j] = lum;
                    }
                }
            }

            return(values);
        }
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num    = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   left   = rect.Left;
            int   top    = rect.Top;
            int   num4   = left + rect.Width;
            int   num5   = top + rect.Height;
            int   num6   = image.Stride - (rect.Width * num);
            RGB   rgb    = new RGB();
            HSL   hsl    = new HSL();
            byte *numPtr = (byte *)(image.ImageData.ToPointer() + ((top * image.Stride) + (left * num)));

            for (int i = top; i < num5; i++)
            {
                int num8 = left;
                while (num8 < num4)
                {
                    bool flag = false;
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    HSL.FromRGB(rgb, hsl);
                    if ((((hsl.Saturation >= this.saturation.Min) && (hsl.Saturation <= this.saturation.Max)) && ((hsl.Luminance >= this.luminance.Min) && (hsl.Luminance <= this.luminance.Max))) && ((((this.hue.Min < this.hue.Max) && (hsl.Hue >= this.hue.Min)) && (hsl.Hue <= this.hue.Max)) || ((this.hue.Min > this.hue.Max) && ((hsl.Hue >= this.hue.Min) || (hsl.Hue <= this.hue.Max)))))
                    {
                        if (!this.fillOutsideRange)
                        {
                            if (this.updateH)
                            {
                                hsl.Hue = this.fillH;
                            }
                            if (this.updateS)
                            {
                                hsl.Saturation = this.fillS;
                            }
                            if (this.updateL)
                            {
                                hsl.Luminance = this.fillL;
                            }
                            flag = true;
                        }
                    }
                    else if (this.fillOutsideRange)
                    {
                        if (this.updateH)
                        {
                            hsl.Hue = this.fillH;
                        }
                        if (this.updateS)
                        {
                            hsl.Saturation = this.fillS;
                        }
                        if (this.updateL)
                        {
                            hsl.Luminance = this.fillL;
                        }
                        flag = true;
                    }
                    if (flag)
                    {
                        HSL.ToRGB(hsl, rgb);
                        numPtr[2] = rgb.Red;
                        numPtr[1] = rgb.Green;
                        numPtr[0] = rgb.Blue;
                    }
                    num8++;
                    numPtr += num;
                }
                numPtr += num6;
            }
        }
Example #24
0
        private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
        {
            lock (this)
            {
                //copy input image
                using (Bitmap hsl = (Bitmap)image.Clone())
                {
                    //filter by HSL
                    HSLFiltering filterHSL = new HSLFiltering();
                    // set color ranges to keep
                    filterHSL.Hue        = new IntRange(Convert.ToInt32(HueLow), Convert.ToInt32(HueHigh));
                    filterHSL.Saturation = new Range((float)SatLow, (float)SatHigh);
                    filterHSL.Luminance  = new Range((float)ValLow, (float)ValHigh);
                    // apply the filter
                    if (cbxTargetFrame.Checked)
                    {
                        filterHSL.ApplyInPlace(image);
                    }
                    else
                    {
                        filterHSL.ApplyInPlace(hsl);
                    }

                    //Process blobs
                    BlobCounter bc = new BlobCounter();
                    bc.ObjectsOrder = ObjectsOrder.Area;
                    if (cbxTargetFrame.Checked)
                    {
                        bc.ProcessImage(image);
                    }
                    else
                    {
                        bc.ProcessImage(hsl);
                    }

                    //show HSL values for selected pixel
                    if (colorRequested)
                    {
                        try
                        {
                            colorRequested = false;
                            int   x    = colorX * image.Width / vspTarget.ClientSize.Width;
                            int   y    = colorY * image.Height / vspTarget.ClientSize.Height;
                            Color c    = image.GetPixel(x, y);
                            var   xrgb = new RGB(c);
                            var   xhsl = new HSL();
                            xhsl      = HSL.FromRGB(xrgb);
                            hue       = xhsl.Hue;
                            sat       = xhsl.Saturation;
                            lum       = xhsl.Luminance;
                            this.Text = "HUE: " + hue.ToString("N0") + "   SAT: " + sat.ToString("N2") + "   LUM: " + lum.ToString("N2");
                        }
                        catch { };
                    }

                    using (Graphics g = Graphics.FromImage(image))
                    {
                        Rectangle[] rects = bc.GetObjectsRectangles();
                        try
                        {
                            if (rects.Length > 0)
                            {
                                //collect info
                                targetWidth  = filter(Convert.ToInt32(rects[0].Width), targetWidth, outputFilter);
                                targetHeight = filter(Convert.ToInt32(rects[0].Height), targetHeight, outputFilter);
                                double area = targetHeight * targetWidth;
                                targetArea = ffilter(area, targetArea, outputFilter);
                                if (targetWidth > 0 && targetHeight > 0)
                                {
                                    targetAspectRatio = ffilter(Convert.ToDouble(targetWidth) / Convert.ToDouble(targetHeight), targetAspectRatio, outputFilter);
                                }
                                double tmpDist = (focal * calHeight) / targetHeight;
                                if (!double.IsInfinity(tmpDist))
                                {
                                    targetDistance = ffilter(tmpDist, targetDistance, outputFilter);
                                }
                                targetX = ffilter(rects[0].X + (rects[0].Width / 2), targetX, outputFilter);
                                targetY = ffilter(rects[0].Y + (rects[0].Height / 2), targetY, outputFilter);
                                HorizontalDegreesPerPixel = HFOV / Convert.ToDouble(image.Width);
                                targetAngle = ffilter((targetX - (Convert.ToDouble(image.Width) / 2)) * HorizontalDegreesPerPixel, targetAngle, outputFilter);
                                if (image.Width > 0)
                                {
                                    targetSteer = ffilter(Normalize(targetAngle, STEERMIN, STEERMAX, -1.0, 1.0), targetSteer, outputFilter);
                                }
                                targetVisible = true;
                                if (cbUseAreaFilter.Checked)
                                {
                                    if (targetArea < AreaLow || targetArea > AreaHigh)
                                    {
                                        targetVisible = false;
                                    }
                                }
                                if (cbUseAreaRatioFilter.Checked)
                                {
                                    if (targetAspectRatio < AspectRatioLow || targetAspectRatio > AspectRatioHigh)
                                    {
                                        targetVisible = false;
                                    }
                                }
                                if (cbUseWidthFilter.Checked)
                                {
                                    if (targetWidth < WidthLow || targetWidth > WidthHigh)
                                    {
                                        targetVisible = false;
                                    }
                                }
                                if (cbUseHeightFilter.Checked)
                                {
                                    if (targetHeight < HeightLow || targetHeight > HeightHigh)
                                    {
                                        targetVisible = false;
                                    }
                                }
                                if (nt.IsConnected)
                                {
                                    nt.PutNumber("targetX", targetX);
                                    nt.PutNumber("targetY", targetY);
                                    nt.PutNumber("targetDistance", targetDistance);
                                    nt.PutNumber("targetAngle", targetAngle);
                                    nt.PutNumber("targetSteer", targetSteer);
                                    nt.PutBoolean("targetVisible", targetVisible);
                                }
                            }
                            else
                            {
                                targetVisible = false;
                                if (nt.IsConnected)
                                {
                                    nt.PutBoolean("targetVisible", targetVisible);
                                }
                            }
                            if (targetVisible)
                            {
                                //Draw Bounding Rectangle
                                if (rects.Length > 0)
                                {
                                    g.DrawRectangle(new Pen(Color.Red, 1), rects[0]);
                                }
                            }
                        }
                        catch { };
                    }
                }
            }
        }
Example #25
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num  = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   left = rect.Left;
            int   top  = rect.Top;
            int   num2 = left + rect.Width;
            int   num3 = top + rect.Height;
            int   num4 = image.Stride - rect.Width * num;
            RGB   rGB  = new RGB();
            HSL   hSL  = new HSL();
            byte *ptr  = (byte *)image.ImageData.ToPointer();

            ptr += top * image.Stride + left * num;
            for (int i = top; i < num3; i++)
            {
                int num5 = left;
                while (num5 < num2)
                {
                    bool flag = false;
                    rGB.Red   = ptr[2];
                    rGB.Green = ptr[1];
                    rGB.Blue  = *ptr;
                    HSL.FromRGB(rGB, hSL);
                    if (hSL.Saturation >= saturation.Min && hSL.Saturation <= saturation.Max && hSL.Luminance >= luminance.Min && hSL.Luminance <= luminance.Max && ((hue.Min < hue.Max && hSL.Hue >= hue.Min && hSL.Hue <= hue.Max) || (hue.Min > hue.Max && (hSL.Hue >= hue.Min || hSL.Hue <= hue.Max))))
                    {
                        if (!fillOutsideRange)
                        {
                            if (updateH)
                            {
                                hSL.Hue = fillH;
                            }
                            if (updateS)
                            {
                                hSL.Saturation = fillS;
                            }
                            if (updateL)
                            {
                                hSL.Luminance = fillL;
                            }
                            flag = true;
                        }
                    }
                    else if (fillOutsideRange)
                    {
                        if (updateH)
                        {
                            hSL.Hue = fillH;
                        }
                        if (updateS)
                        {
                            hSL.Saturation = fillS;
                        }
                        if (updateL)
                        {
                            hSL.Luminance = fillL;
                        }
                        flag = true;
                    }
                    if (flag)
                    {
                        HSL.ToRGB(hSL, rGB);
                        ptr[2] = rGB.Red;
                        ptr[1] = rGB.Green;
                        *ptr = rGB.Blue;
                    }
                    num5++;
                    ptr += num;
                }
                ptr += num4;
            }
        }
Example #26
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num  = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int   left = rect.Left;
            int   top  = rect.Top;
            int   num2 = left + rect.Width;
            int   num3 = top + rect.Height;
            int   num4 = image.Stride - rect.Width * num;
            RGB   rGB  = new RGB();
            HSL   hSL  = new HSL();
            float num5 = 0f;
            float num6 = 0f;
            float num7 = 0f;
            float num8 = 0f;

            if (inLuminance.Max != inLuminance.Min)
            {
                num5 = (outLuminance.Max - outLuminance.Min) / (inLuminance.Max - inLuminance.Min);
                num6 = outLuminance.Min - num5 * inLuminance.Min;
            }
            if (inSaturation.Max != inSaturation.Min)
            {
                num7 = (outSaturation.Max - outSaturation.Min) / (inSaturation.Max - inSaturation.Min);
                num8 = outSaturation.Min - num7 * inSaturation.Min;
            }
            byte *ptr = (byte *)image.ImageData.ToPointer();

            ptr += top * image.Stride + left * num;
            for (int i = top; i < num3; i++)
            {
                int num9 = left;
                while (num9 < num2)
                {
                    rGB.Red   = ptr[2];
                    rGB.Green = ptr[1];
                    rGB.Blue  = *ptr;
                    HSL.FromRGB(rGB, hSL);
                    if (hSL.Luminance >= inLuminance.Max)
                    {
                        hSL.Luminance = outLuminance.Max;
                    }
                    else if (hSL.Luminance <= inLuminance.Min)
                    {
                        hSL.Luminance = outLuminance.Min;
                    }
                    else
                    {
                        hSL.Luminance = num5 * hSL.Luminance + num6;
                    }
                    if (hSL.Saturation >= inSaturation.Max)
                    {
                        hSL.Saturation = outSaturation.Max;
                    }
                    else if (hSL.Saturation <= inSaturation.Min)
                    {
                        hSL.Saturation = outSaturation.Min;
                    }
                    else
                    {
                        hSL.Saturation = num7 * hSL.Saturation + num8;
                    }
                    HSL.ToRGB(hSL, rGB);
                    ptr[2] = rGB.Red;
                    ptr[1] = rGB.Green;
                    *ptr = rGB.Blue;
                    num9++;
                    ptr += num;
                }
                ptr += num4;
            }
        }
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int    num   = Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int    left  = rect.Left;
            int    top   = rect.Top;
            int    num4  = left + rect.Width;
            int    num5  = top + rect.Height;
            int    num6  = image.Stride - (rect.Width * num);
            RGB    rgb   = new RGB();
            HSL    hsl   = new HSL();
            double num7  = 0.0;
            double num8  = 0.0;
            double num9  = 0.0;
            double num10 = 0.0;

            if (this.inLuminance.Max != this.inLuminance.Min)
            {
                num7 = (this.outLuminance.Max - this.outLuminance.Min) / (this.inLuminance.Max - this.inLuminance.Min);
                num8 = this.outLuminance.Min - (num7 * this.inLuminance.Min);
            }
            if (this.inSaturation.Max != this.inSaturation.Min)
            {
                num9  = (this.outSaturation.Max - this.outSaturation.Min) / (this.inSaturation.Max - this.inSaturation.Min);
                num10 = this.outSaturation.Min - (num9 * this.inSaturation.Min);
            }
            byte *numPtr = (byte *)(image.ImageData.ToPointer() + ((top * image.Stride) + (left * num)));

            for (int i = top; i < num5; i++)
            {
                int num12 = left;
                while (num12 < num4)
                {
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    HSL.FromRGB(rgb, hsl);
                    if (hsl.Luminance >= this.inLuminance.Max)
                    {
                        hsl.Luminance = this.outLuminance.Max;
                    }
                    else if (hsl.Luminance <= this.inLuminance.Min)
                    {
                        hsl.Luminance = this.outLuminance.Min;
                    }
                    else
                    {
                        hsl.Luminance = (num7 * hsl.Luminance) + num8;
                    }
                    if (hsl.Saturation >= this.inSaturation.Max)
                    {
                        hsl.Saturation = this.outSaturation.Max;
                    }
                    else if (hsl.Saturation <= this.inSaturation.Min)
                    {
                        hsl.Saturation = this.outSaturation.Min;
                    }
                    else
                    {
                        hsl.Saturation = (num9 * hsl.Saturation) + num10;
                    }
                    HSL.ToRGB(hsl, rgb);
                    numPtr[2] = rgb.Red;
                    numPtr[1] = rgb.Green;
                    numPtr[0] = rgb.Blue;
                    num12++;
                    numPtr += num;
                }
                numPtr += num6;
            }
        }
Example #28
0
        /// <summary>
        /// Converts a Bitmap to an HSL structure with or without alpha-channel.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        /// <param name="alpha">Alpha-channel</param>
        /// <returns>HSL structure array</returns>
        public unsafe static float[][,] ToHSL(this BitmapData bmData, bool alpha = false)
        {
            // params
            int   width = bmData.Width, height = bmData.Height, stride = bmData.Stride;
            byte *p = (byte *)bmData.Scan0.ToPointer();

            // matrices
            float[,] x = new float[height, width];
            float[,] y = new float[height, width];
            float[,] z = new float[height, width];

            // with alpha channel
            if (alpha)
            {
                float[,] a = new float[height, width];

                Parallel.For(0, height, j =>
                {
                    HSL mdl;

                    int i, k, jstride = j * stride;

                    for (i = 0; i < width; i++)
                    {
                        // shift:
                        k = jstride + i * 4;

                        mdl     = HSL.FromRGB(p[k + 2], p[k + 1], p[k + 0]);
                        x[j, i] = mdl.Hue;
                        y[j, i] = mdl.Saturation;
                        z[j, i] = mdl.Lightness;
                        a[j, i] = p[k + 3] / 255.0f;
                    }
                });

                return(new float[][, ] {
                    x, y, z, a
                });
            }

            // without alpha channel
            Parallel.For(0, height, j =>
            {
                HSL mdl;

                int i, k, jstride = j * stride;

                for (i = 0; i < width; i++)
                {
                    // shift:
                    k = jstride + i * 4;

                    mdl     = HSL.FromRGB(p[k + 2], p[k + 1], p[k + 0]);
                    x[j, i] = mdl.Hue;
                    y[j, i] = mdl.Saturation;
                    z[j, i] = mdl.Lightness;
                }
            });

            return(new float[][, ] {
                x, y, z
            });
        }