Ejemplo n.º 1
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        public void Apply(Bitmap Data)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            Apply(bmData);
            BitmapFormat.Unlock(Data, bmData);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        public void Apply(Bitmap Data)
        {
            Bitmap Max = (Bitmap)Data.Clone();
            Bitmap Min = (Bitmap)Data.Clone();

            di.Apply(Max); er.Apply(Min);

            BitmapData bmData = BitmapFormat.Lock32bpp(Data);
            BitmapData bmMax  = BitmapFormat.Lock32bpp(Max);
            BitmapData bmMin  = BitmapFormat.Lock32bpp(Min);

            if (smoothing)
            {
                gb.Apply(bmMax);
            }
            gb.Apply(bmMin);

            Apply(bmData, bmMax, bmMin);

            BitmapFormat.Unlock(Data, bmData);
            BitmapFormat.Unlock(Max, bmMax);
            BitmapFormat.Unlock(Min, bmMin);

            Max.Dispose(); Min.Dispose();
            return;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Apply filter and returns motion level in range [0, 1].
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <returns>Motion level</returns>
        public double Apply(Bitmap Data)
        {
            // synchronize
            lock (locker)
            {
                if (Frame == null)
                {
                    // create initial backgroung image
                    Frame = (Bitmap)Data.Clone();

                    // just return for the first time
                    return(0.0);
                }

                // creating clone of current frame
                var temp = (Bitmap)Data.Clone();

                // lock in memory
                var bitmapData = BitmapFormat.Lock32bpp(Data);
                var frameData  = BitmapFormat.Lock32bpp(Frame);

                // calculate alarm
                var alarm = UseFilter ? ProcessFrameWithFilter(bitmapData, frameData) :
                            ProcessFrameWithoutFilter(bitmapData, frameData);

                // unlock
                Data.Unlock(bitmapData);
                Frame.Unlock(frameData);

                // update detector
                Frame.Dispose();
                Frame = temp;
                return(alarm);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets a histogram of the image.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <returns>Array</returns>
        public static int[] Histogram(this Bitmap Data)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            int[] rgb = Histogram(bmData);
            BitmapFormat.Unlock(Data, bmData);
            return(rgb);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Converts a Bitmap to an BGR tensor arrays.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <param name="rgb">RGB or BGR</param>
        /// <returns>RGB tensor arrays</returns>
        public static float[][] ToFloatTensor(this Bitmap Data, bool rgb = false)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            float[][] _ix = BitmapTensor.ToFloatTensor(bmData, rgb);
            BitmapFormat.Unlock(Data, bmData);
            return(_ix);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Calculates the optimal threshold for the original bitmap.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <returns>Integer number</returns>
        public static int SISThreshold(this Bitmap Data)
        {
            BitmapData bmData    = BitmapFormat.Lock32bpp(Data);
            int        threshold = SISThreshold(bmData);

            BitmapFormat.Unlock(Data, bmData);
            return(threshold);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Converts Bitmap to averaged channel value matrix.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <returns>Matrix</returns>
        public static float[,] ToGrayscale(this Bitmap Data)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            float[,] rgb = ToGrayscale(bmData);
            BitmapFormat.Unlock(Data, bmData);
            return(rgb);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Converts an YCbCr structure to a color image.
        /// </summary>
        /// <param name="array">YCbCr structure array</param>
        /// <param name="Data">Bitmap</param>
        public static void FromYCbCr(this float[][,] array, Bitmap Data)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            FromYCbCr(array, bmData);
            BitmapFormat.Unlock(Data, bmData);
            return;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Converts a Bitmap to an YCbCr structure with or without alpha-channel.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <param name="alpha">Alpha-channel</param>
        /// <returns>YCbCr structure array</returns>
        public static float[][,] ToYCbCr(this Bitmap Data, bool alpha = false)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            float[][,] rgb = BitmapMatrix.ToYCbCr(bmData, alpha);
            BitmapFormat.Unlock(Data, bmData);
            return(rgb);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Converts a matrix of channel values to a monochrome Bitmap.
        /// </summary>
        /// <param name="m">Matrix</param>
        /// <param name="Data">Bitmap</param>
        public static void FromGrayscale(this float[,] m, Bitmap Data)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);

            FromGrayscale(m, bmData);
            BitmapFormat.Unlock(Data, bmData);
            return;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <param name="Src">Bitmap</param>
        public void Apply(Bitmap Data, Bitmap Src)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);
            BitmapData bmSrc  = BitmapFormat.Lock32bpp(Src);

            Apply(bmData, bmSrc);
            BitmapFormat.Unlock(Data, bmData);
            BitmapFormat.Unlock(Src, bmSrc);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        public void Apply(BitmapData bmData)
        {
            Bitmap     Src   = (Bitmap)BitmapFormat.Bitmap(bmData).Clone();
            BitmapData bmSrc = BitmapFormat.Lock32bpp(Src);

            Apply(bmData, bmSrc);
            BitmapFormat.Unlock(Src, bmSrc);
            Src.Dispose();
            return;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="Data">Bitmap</param>
        /// <param name="Src">Bitmap</param>
        public float[,] Apply(Bitmap Data, Bitmap Src)
        {
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);
            BitmapData bmSrc  = BitmapFormat.Lock32bpp(Src);
            var        output = Apply(bmData, bmSrc);

            BitmapFormat.Unlock(Data, bmData);
            BitmapFormat.Unlock(Src, bmSrc);

            return(output);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Apply filter.
        /// </summary>
        /// <param name="bmData">Bitmap data</param>
        /// <param name="bmSrc">Bitmap data</param>
        public void Apply(BitmapData bmData, BitmapData bmSrc)
        {
            // Creating resources:
            Bitmap     Src0   = (Bitmap)BitmapFormat.Bitmap(bmSrc).Clone();
            BitmapData bmSrc0 = BitmapFormat.Lock32bpp(Src0);

            // Filter applying:
            closing.Apply(bmSrc, bmSrc0);
            subtraction.Apply(bmData, bmSrc);

            // Delete resources:
            BitmapFormat.Unlock(Src0, bmSrc0);
            Src0.Dispose();
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Converts a BGR tensor arrays to Bitmap.
        /// </summary>
        /// <param name="tensor">Tensor arrays</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        /// <param name="rgb">RGB or BGR</param>
        /// <returns>Bitmap</returns>
        public unsafe static Bitmap FromFloatTensor(this float[][] tensor, int width, int height, bool rgb = false)
        {
            // params
            Bitmap     Data   = new Bitmap(width, height);
            BitmapData bmData = BitmapFormat.Lock32bpp(Data);
            int        stride = bmData.Stride;
            byte *     p      = (byte *)bmData.Scan0.ToPointer();
            int        shift  = height * width;
            int        z      = 0;

            // do job
            if (rgb)
            {
                for (int j = 0; j < height; j++)
                {
                    for (int i = 0; i < width; i++, z++)
                    {
                        int k, jstride = j * stride;
                        k = jstride + i * 4;

                        // transform
                        p[k + 2] = (byte)tensor[0][z];
                        p[k + 1] = (byte)tensor[1][z];
                        p[k + 0] = (byte)tensor[2][z];
                    }
                }
            }
            else
            {
                for (int j = 0; j < height; j++)
                {
                    for (int i = 0; i < width; i++, z++)
                    {
                        int k, jstride = j * stride;
                        k = jstride + i * 4;

                        // transform
                        p[k + 0] = (byte)tensor[0][z];
                        p[k + 1] = (byte)tensor[1][z];
                        p[k + 2] = (byte)tensor[2][z];
                    }
                }
            }

            // arrays
            BitmapFormat.Unlock(Data, bmData);
            return(Data);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Converts a matrix of channel values to a monochrome Bitmap.
        /// </summary>
        /// <param name="m">Matrix</param>
        /// <returns>Bitmap</returns>
        public unsafe static Bitmap FromGrayscale(this float[,] m)
        {
            int        width = m.GetLength(1), height = m.GetLength(0);
            Bitmap     bitmap = new Bitmap(width, height);
            BitmapData bmData = BitmapFormat.Lock32bpp(bitmap);
            int        stride = bmData.Stride;
            byte *     p      = (byte *)bmData.Scan0.ToPointer();

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

                for (i = 0; i < width; i++)
                {
                    k        = jstride + i * 4;
                    p[k + 2] = p[k + 1] = p[k] = Maths.Byte(m[j, i] * 255.0f);
                    p[k + 3] = 255;
                }
            });

            BitmapFormat.Unlock(bitmap, bmData);
            return(bitmap);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Converts an RGB structure to a color image.
        /// </summary>
        /// <param name="array">RGBA structure array</param>
        /// <returns>Bitmap</returns>
        public unsafe static Bitmap FromRGB(this float[][,] array)
        {
            // matrices
            float[,] x = array[0];
            float[,] y = array[1];
            float[,] z = array[2];

            // params
            int        width = x.GetLength(1), height = x.GetLength(0);
            Bitmap     bitmap = new Bitmap(width, height);
            BitmapData bmData = BitmapFormat.Lock32bpp(bitmap);
            int        stride = bmData.Stride;
            byte *     p      = (byte *)bmData.Scan0.ToPointer();

            // alpha
            bool alpha = array.Length == 4;

            if (alpha)
            {
                float[,] a = array[3];

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

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

                        // recording model:
                        p[k + 0] = Maths.Byte(x[j, i] * 255.0f);
                        p[k + 1] = Maths.Byte(y[j, i] * 255.0f);
                        p[k + 2] = Maths.Byte(z[j, i] * 255.0f);
                        p[k + 3] = Maths.Byte(a[j, i] * 255.0f);
                    }
                });
            }
            else
            {
                Parallel.For(0, height, j =>
                {
                    int i, k, jstride = j * stride;

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

                        // recording model:
                        p[k + 0] = Maths.Byte(x[j, i] * 255.0f);
                        p[k + 1] = Maths.Byte(y[j, i] * 255.0f);
                        p[k + 2] = Maths.Byte(z[j, i] * 255.0f);
                        p[k + 3] = (byte)255;
                    }
                });
            }


            BitmapFormat.Unlock(bitmap, bmData);
            return(bitmap);
        }