Exemplo n.º 1
0
        /// <summary>
        /// Convert an float image to a squared integral image
        /// </summary>
        /// <remarks>
        /// We skip the last line to keep the original size (better performance)
        /// </remarks>
        /// <param name="source">image source</param>
        /// <param name="dest">image destination</param>
        public void IntegralSquare(IImage2DFloatA source, IImage2DFloatA dest)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (dest == null) throw new ArgumentNullException("dest");
            if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the source image width and height but is only " + source.Width + "x" + source.Height);

            int heightS = source.Height;
            int widthS = source.Width;
            int heightD = dest.Height;
            int widthD = dest.Width;
            float[] sBuf = source.HostBuffer;
            float[] dBuf = dest.HostBuffer;

            for (int x = 0; x < widthD; x++) dest.HostBuffer[x] = 0; // zero first line
            for (int y = 0, a = widthD * heightD; y < a; y += widthD) dest.HostBuffer[y] = 0; // zero first column

            int y1IndexS = 0;
            int yIndexD = widthD;
            int y1IndexD = 0; // last

            // for each line
            for (int y = 1; y < heightS; y++)
            {
                // for each pixel
                for (int x = 1; x < widthS; x++)
                {
                    float p = sBuf[y1IndexS + x - 1];
                    dBuf[yIndexD + x] = p * p + dBuf[yIndexD + x - 1] + dBuf[y1IndexD + x] - dBuf[y1IndexD + x - 1];
                }

                yIndexD += widthD;
                y1IndexS += widthS;
                y1IndexD += widthD;
            }
            dest.Normalized = false;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Flip image Y coordinate
        /// </summary>
        /// <param name="source">image source</param>
        /// <param name="dest">image destination</param>
        public void FlipY(IImage2DFloatA source, IImage2DFloatA dest)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (dest == null) throw new ArgumentNullException("dest");
            if (source == dest) throw new ArgumentException("Flipping kernel is not designed to run inline therefore source and destination must be different images");
            if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")");

            var sBuf = source.HostBuffer;
            var dBuf = dest.HostBuffer;

            int posSource = 0;
            int posDest = (source.Height - 1) * dest.Width;

            for (int y = 0, h = source.Height; y < h; y++)
            {
                for (int x = 0, w = source.Width; x < w; x++)
                {
                    float color = sBuf[posSource++];
                    dBuf[posDest++] = color;
                }
                posDest -= dest.Width + source.Width;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// GrayScale image
        /// </summary>
        /// <param name="source">image source</param>
        /// <param name="dest">image destination</param>
        public void GrayScale(IImage2DFloatRgbA source, IImage2DFloatA dest)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (dest == null) throw new ArgumentNullException("dest");
            if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")");

            int length = source.Width * source.Height;
            int pos = 0;
            for (int i = 0; i < length; i++)
            {
                float gray = 0.2989f * source.HostBuffer[pos++] + 0.5870f * source.HostBuffer[pos++] + 0.1140f * source.HostBuffer[pos++];
                pos++;
                dest.HostBuffer[i] = gray;
            }
            dest.Normalized = source.Normalized;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extracts a channel of an RGB image
        /// </summary>
        /// <param name="source">image source</param>
        /// <param name="dest">image destination</param>
        /// <param name="offset">offset (0..3)</param>
        public void ExtractChannel(IImage2DFloatRgbA source, IImage2DFloatA dest, byte offset)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (dest == null) throw new ArgumentNullException("dest");
            if (offset > 3) throw new ArgumentOutOfRangeException("offset", String.Format("offset must be between 0..3 but was {0}", offset));
            if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")");

            int length = source.Width * source.Height;
            int pos = 0;
            for (int i = 0; i < length; i++)
                for (int j = 0; j < 4; j++)
                    if (j == offset) dest.HostBuffer[i] = source.HostBuffer[pos++]; else pos++;

            dest.Normalized = source.Normalized;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sobel filter image
        /// </summary>
        /// <param name="source">image source</param>
        /// <param name="dest">image destination</param>
        public void Sobel(IImage2DFloatA source, IImage2DFloatA dest)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (dest == null) throw new ArgumentNullException("dest");
            if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")");

            // TODO: Sobel filter image
            dest.Normalized = source.Normalized;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sets channel of a RGBA image
        /// </summary>
        /// <param name="source">source image</param>
        /// <param name="mask">mask image</param>
        /// <param name="dest">destination image</param>
        /// <param name="offset">offset (0..3)</param>
        public void SetChannel(IImage2DFloatRgbA source, IImage2DFloatA mask, IImage2DFloatRgbA dest, byte offset)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (mask == null) throw new ArgumentNullException("mask");
            if (dest == null) throw new ArgumentNullException("dest");
            if (offset > 3) throw new ArgumentOutOfRangeException("offset", String.Format("offset must be between 0..3 but was {0}", offset));

            if ((source.Width > mask.Width) || (source.Height > mask.Height)) throw new ArgumentException("Image mask (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")");
            if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")");

            int length = source.Width * source.Height;
            int pos = 0;
            for (int i = 0; i < length; i++)
                for (int j = 0; j < 4; j++)
                {
                    dest.HostBuffer[pos] = (j == offset) ? mask.HostBuffer[i] : source.HostBuffer[pos];
                    pos++;
                }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Gets the sum of a rectangular feature in an integral image.
 /// </summary>
 public static float GetSum(ref HaarRectangle rect, IImage2DFloatA integralImage, int x, int y)
 {
     return integralImage.GetIntegralSum(x + rect.ScaledX, y + rect.ScaledY, rect.ScaledWidth, rect.ScaledHeight) * rect.ScaledWeight;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the sum of the areas of the rectangular features in an integral image.
        /// </summary>
        public static float GetNodeSum(ref HaarFeatureNode node, IImage2DFloatA integralImage, int x, int y)
        {
            float sum;
            sum = GetSum(ref node.Rect1, integralImage, x, y);
            sum += GetSum(ref node.Rect2, integralImage, x, y);
            if (node.RectCount == 3) sum += GetSum(ref node.Rect3, integralImage, x, y);

            return sum;
        }