Exemple #1
0
        /* This vector represents the lights/darks of an image. Convert each pixel to gray-
         * scale (0-255). Create X bins, and divide each pixel into the bin with like-pixels.
         * Increment the count (numPixelsInBin) of each bin every time a pixel is added. Normalize
         * this value, dividing by the total # of pixels. This gives a unique X element vector
         * for this image describing its' lights/darks.
         */
        public static float[] calculateInputVector_Histogram(rgbColor[,] imgArray, int imgHeight, int imgWidth)
        {
            float[] grayArray = new float[imgArray.Length];
            float maxNumInBucket = (float)(imgHeight * imgWidth);     // Used to normalize this vector.

            // Convert each pixel to a single gray-scale value.
            for (int i = 0; i < imgHeight; ++i)
            {
                for (int j = 0; j < imgWidth; ++j)
                {
                    grayArray[(i * imgWidth) + j] = (imgArray[i, j].r * .333333f) +
                                                      (imgArray[i, j].g * .333333f) +
                                                      (imgArray[i, j].b * .333333f);
                }
            }

            int numberOfBins = SOMConstants.INPUT_VECTOR_SIZE_HISTOGRAM;
            float[] resultArray = new float[numberOfBins];

            for (int i = 0; i < grayArray.Length; ++i)
            {
                int binNumber = (int)grayArray[i] / numberOfBins;
                ++resultArray[binNumber];
            }

            // Normalize.
            for (int i = 0; i < resultArray.Length; ++i)
            {
                resultArray[i] /= maxNumInBucket;
            }

            return resultArray;
        }
Exemple #2
0
        /* Chop the image into X regions. Calculate the average RGB values for each region (1
         * RGB value per region). This gives you X * 3 values describing the color arrangement
         * of this image.
         */
        public static float[] calculateInputVector_Area(rgbColor[,] imgArray, int imgHeight, int imgWidth)
        {
            float[] result = new float[SOMConstants.INPUT_VECTOR_SIZE_AREA];

            int pixPerRegionHigh = imgHeight / SOMConstants.INPUT_VECTOR_AREA_REGIONS_HIGH;
            int pixPerRegionWide = imgWidth / SOMConstants.INPUT_VECTOR_AREA_REGIONS_WIDE;

            /* A 9 region image is traversed as such:
             *
             *      0 | 1 | 2
             *      ---------
             *      3 | 4 | 5
             *      ---------
             *      6 | 7 | 8
             */

            // Traverse through all regions.
            for (int rHigh = 0; rHigh < SOMConstants.INPUT_VECTOR_AREA_REGIONS_HIGH; ++rHigh)
            {
                for (int rWide = 0; rWide < SOMConstants.INPUT_VECTOR_AREA_REGIONS_WIDE; ++rWide)
                {

                    float totalPixels = 0.0f;
                    rgbColor totalColor;
                    totalColor.r = totalColor.g = totalColor.b = 0.0f;

                    for (int i = rHigh * pixPerRegionHigh; i < (rHigh + 1) * pixPerRegionHigh; ++i)
                    {
                        for (int j = rWide * pixPerRegionWide; j < (rWide + 1) * pixPerRegionWide; ++j)
                        {

                            totalColor.r += imgArray[i, j].r;
                            totalColor.g += imgArray[i, j].g;
                            totalColor.b += imgArray[i, j].b;
                            ++totalPixels;

                        } // End for each pixel across.
                    } // End for each pixel down.

                    // Calculate the average.
                    totalColor.r /= totalPixels;
                    totalColor.g /= totalPixels;
                    totalColor.b /= totalPixels;

                    // Store the averages (separate rgb values).
                    result[3 * ((rHigh * SOMConstants.INPUT_VECTOR_AREA_REGIONS_WIDE) + rWide)] = totalColor.r;
                    result[3 * ((rHigh * SOMConstants.INPUT_VECTOR_AREA_REGIONS_WIDE) + rWide) + 1] = totalColor.g;
                    result[3 * ((rHigh * SOMConstants.INPUT_VECTOR_AREA_REGIONS_WIDE) + rWide) + 2] = totalColor.b;

                } // End for each region across.
            } // End for each region down.

            // Normalize to 0.0 to 1.0 range;
            for (int i = 0; i < result.Length; ++i)
            {
                result[i] /= 255.0f;
            }

            return result;
        }