Ejemplo n.º 1
0
        public static Image DrawHistogramOfOrientedGradients(this Image image, int blockCount, int bucketCount)
        {
            Contract.Assert(image != null);
            Contract.Assert(image.Gradient != null, "gradient is not computed");
            Contract.Assert(bucketCount <= 9, "Cannot draw gradient for more than 9 buckets");

            ColorPixel[] colorPixel = HOGPixels.getHogPixels();

            var histogramSizeX = blockCount;
            var histogramSizeY = blockCount;
            var bucketAngles   = 360 / bucketCount;

            byte[,,] gradHist = new byte[histogramSizeX, histogramSizeY, bucketCount + 1];
            int angle;
            int sizex, sizey, binx, biny;

            sizex = (image.PixelWidth / histogramSizeX + 1);
            sizey = (image.PixelHeight / histogramSizeY + 1);

            for (int i = 0; i < histogramSizeX; i++)
            {
                for (int j = 0; j < (int)histogramSizeY; j++)
                {
                    for (int k = 0; k < 9; k++)
                    {
                        gradHist[i, j, k] = 0;
                    }
                }
            }
            for (int y = 0; y < image.PixelHeight; y++)
            {
                for (int x = 0; x < image.PixelWidth; x++)
                {
                    binx  = x / sizex;
                    biny  = y / sizey;
                    angle = image.Gradient[x, y] / bucketAngles;
                    angle = angle >= bucketCount ? 0 : angle;   // is this ok?
                    gradHist[binx, biny, angle]++;
                }
            }
            for (int y = 0; y < (int)histogramSizeX; y++)
            {
                for (int x = 0; x < (int)histogramSizeY; x++)
                {
                    var max      = 0;
                    var maxindex = 0;
                    for (int k = 0; k < bucketCount; k++)
                    {
                        if (gradHist[x, y, k] > max)
                        {
                            max      = gradHist[x, y, k];
                            maxindex = k;
                        }
                    }
                    gradHist[x, y, bucketCount] = (byte)maxindex;
                }
            }
            for (int y = 0; y < image.PixelHeight; y++)
            {
                for (int x = 0; x < image.PixelWidth; x++)
                {
                    binx = x / sizex;
                    biny = y / sizey;
                    var pixel = colorPixel[gradHist[binx, biny, bucketCount]];
                    image.GrayScale.SetPixel(x, y, (byte)((pixel.R + pixel.G + pixel.B) / 3)); // coud replace with bucket index times 360/bucketCount
                }
            }

            return(image);
        }
Ejemplo n.º 2
0
        public static int[] HistogramOfOrientedGradients(this Image image, int blockCount, int bucketCount)
        {
            Contract.Assert(image != null);
            Contract.Assert(image.Gradient != null, "gradient is not computed");

            ColorPixel[] colorPixel = HOGPixels.getHogPixels();

            var histogramSizeX = blockCount;
            var histogramSizeY = blockCount;
            var bucketAngles   = (int)Math.Ceiling((decimal)360 / bucketCount);

            byte[,,] gradHist = new byte[histogramSizeX, histogramSizeY, bucketCount + 1];
            int[] histogram = new int[bucketCount];
            int   angle;
            int   sizex, sizey, binx, biny;

            sizex = (int)(image.PixelWidth / histogramSizeX + 1);
            sizey = (int)(image.PixelHeight / histogramSizeY + 1);

            for (int i = 0; i < (int)histogramSizeX; i++)
            {
                for (int j = 0; j < (int)histogramSizeY; j++)
                {
                    for (int k = 0; k < 9; k++)
                    {
                        gradHist[i, j, k] = 0;
                    }
                }
            }
            // compute gradients for each angle
            for (int y = 0; y < image.PixelHeight; y++)
            {
                for (int x = 0; x < image.PixelWidth; x++)
                {
                    binx  = x / sizex;
                    biny  = y / sizey;
                    angle = image.Gradient[x, y] / bucketAngles;
                    if (angle >= bucketCount)
                    {
                        angle = 0;
                    }
                    gradHist[binx, biny, angle]++;
                }
            }
            //compute max gradient
            for (int y = 0; y < (int)histogramSizeX; y++)
            {
                for (int x = 0; x < (int)histogramSizeY; x++)
                {
                    var max      = 0;
                    var maxindex = 0;
                    for (int k = 0; k < bucketCount; k++)
                    {
                        if (gradHist[x, y, k] > max)
                        {
                            max      = gradHist[x, y, k];
                            maxindex = k;
                        }
                    }
                    gradHist[x, y, bucketCount] = (byte)maxindex;
                }
            }
            //compute histogram
            for (int y = 0; y < image.PixelHeight; y++)
            {
                for (int x = 0; x < image.PixelWidth; x++)
                {
                    binx = x / sizex;
                    biny = y / sizey;
                    histogram[gradHist[binx, biny, bucketCount]] += image.GrayScale.GetPixel(x, y);
                }
            }

            return(histogram);
        }