Esempio n. 1
0
        /// <summary>
        /// метод запускающий выделение кластеров по среднеквадртичному отклонению.
        /// </summary>
        /// <param name="coeff_compression1">коэффициент сжатия фотографий</param>
        /// <param name="count_of_clasters1">число кластеров</param>
        public static Bitmap GetClasters(int coeff_compression1, int count_of_clasters1)
        {
            coeff_compression = coeff_compression1;
            count_of_clasters = count_of_clasters1;

            Color col;

            Int32[,] bytes = null;
            // double[,] images_data = null;
            Int32[] newImageData = null;
            double[,] result = null;
            int    i = 0, w = 0, h = 0;
            string mainPath = "C:\\Users\\Amigo's\\Desktop\\pic\\wrap";

            //string mainPath = "C:\\Users\\Amigo's\\Desktop\\h";

            string[] paths = Directory.GetFiles(mainPath);
            int      count = 0;

            foreach (string path in paths)
            {
                Bitmap bmp = new Bitmap(path);

                bmp = bmp.MakeBitmapSmaller();

                w = bmp.Width;
                h = bmp.Height;

                if (bytes == null)
                {
                    bytes = new Int32[w * h, paths.Length];
                }
                //bytes - массив, первый индекс - номер пикселя, второй индекс - номер фотки, значение массива - цвет

                // images_data = new double[w * h, paths.Length];

                int width  = bmp.Width;
                int height = bmp.Height;
                if (result == null)
                {
                    result = new double[paths.Length, w *h];
                }

                for (int y = 0; y < h; ++y)
                {
                    for (int x = 0; x < w; ++x)
                    {
                        Color color = bmp.GetPixel(x, y);
                        result[count, y *w + x] = ((color.R + color.G + color.B) / 3.0);
                    }
                }
                count++;
                //----------------test-------------------//
                ////////////////////////////////////////test
                // testing graph on antycycling
                // double[,] array_of_Minimum_spanning_graph={{-1, 0.1 ,-1, 0.1},{ 0.1,-1, 8 ,0.3},{-1,8,-1,-1},{0.1,0.3,-1,-1}};
                // int[] graph_nodes= {1,1,1,1};
                // bool kok = Checking_graph_on_antycycling(array_of_Minimum_spanning_graph, graph_nodes); //true - if cycle^ false if not
                // int g = 0;
                // test
////////////////////////////////////////

                //////////////////////////////////////////// test
                //testing Minimun_spanning_tree_clasterization
                // double[] testdescriptors = { 1, 2, 6, 8, 5, 10, 16, 14, 0.3, 2, 0.6, 15, 30  };
                // Claster.Minimun_spanning_tree_clasterization(testdescriptors, ref bytes, 3);
                // testing Minimun_spanning_tree_clasterization
                //////////////////////////////////////////////test
                //----------------test-------------------//

                Int32[] tmpimageData = bmp.ToImageData();

                for (uint y = 0; y < h; ++y)
                {
                    for (uint x = 0; x < w; ++x)
                    {
                        col = Color.FromArgb(tmpimageData[(int)y * w + (int)x]);

                        #region  для  компонент цвета Y (YUV),(R+G+B)/3 (RGB), V (HSV)
                        //bytes[(int)y * w + (int)x, i] = (byte)((col.R + col.G + col.B) / 3);                // RGB/3
                        bytes[(int)y * w + (int)x, i] = (byte)(0.299 * col.R + 0.587 * col.G + 0.114 * col.B);  // Y
                        //bytes[(int)y * w + (int)x, i] = (byte)(Math.Max(col.R, Math.Max(col.G, col.B)));    // V
                        #endregion
                    }
                }
                i++;
                GC.Collect();
            }


            //----------------test Вычисление главных компонент-------------------//
            ////////////////////////

            /*
             * var pca = new PrincipalComponentAnalysis(result, AnalysisMethod.Standardize);
             * pca.Compute();
             *
             * Color cl;
             * var res = pca.Eigenvalues;
             * var eee = pca.Result;
             * var res1 = pca.ComponentMatrix;
             * double[,] actual = pca.Transform(result, 1);
             * // alglib.rmatrixgemm(actual.GetLength(0), result.GetLength(1), actual.GetLength(1), 1, actual, 0, 0, 0, result, 0, 0, 0, 0, ref actual, 0, 0);
             *
             *
             *
             * newImageData = new Int32[actual.GetLength(0) *10];
             *
             * for (uint y = 0; y < actual.GetLength(0); y++)
             * {
             *   for (uint x = 0; x < 10; x++)
             *   {
             *       cl = Color.FromArgb((byte)actual[y, x], (byte)actual[y, x], (byte)actual[y, x]);
             *       newImageData[(int)y * 1 + (int)x] = cl.ToArgb();
             *   }
             * }
             *
             * Bitmap resultBmp1 = newImageData.ToBitmap(actual.GetLength(0), 10);
             * Bitmap resultBmp2 = ChangeBitmapSize(resultBmp1, 160, 190);
             * return resultBmp2;
             * //////////////////
             */

            //----------------test  Вычисление главных компонент-------------------//


            // кластеризация для Y(YUV), (R+G+B)/3, V (HSV)
            double[] descriptors = GetDescriptorsFor_Component(bytes);      //кластеризация для Y,V,rgb/3
            // кластеризация

            Claster.K_Means(descriptors, ref bytes, count_of_clasters);
            //Claster.Minimun_spanning_tree_clasterization(descriptors, ref bytes, count_of_clasters);

            //после выоплнения этого метода в байтс на месте первой фотки будет нужное нам изображение
            //потом обратно в imagedata и в битмап

            newImageData = new Int32[w * h];

            for (uint y = 0; y < h; ++y)
            {
                for (uint x = 0; x < w; ++x)
                {
                    newImageData[(int)y * w + (int)x] = bytes[(int)y * w + (int)x, 0];
                }
            }

            Bitmap resultBmp = newImageData.ToBitmap(w, h);
            //SuperSave(resultBmp, "C:\\Users\\Амир\\Desktop\\resultBmp.jpg");
            resultBmp = ChangeBitmapSize(newImageData.ToBitmap(w, h), 160, 190);
            GC.Collect();
            //SuperSave(resultBmp, "C:\\Users\\Амир\\Desktop\\resultBmp.jpg");
            //SuperSave(resultBmp, mainPath + "\\resultBmp.jpg");
            return(resultBmp);
        }
Esempio n. 2
0
        /// <summary>
        /// метод запускающий выделение кластеров по среднеквадртичному отклонению.
        /// </summary>
        /// <param name="coeff_compression1">коэффициент сжатия фотографий</param>
        /// <param name="count_of_clasters1">число кластеров</param>
        public static int[] GetClastersForMatStat(int coeff_compression1, int count_of_clasters1, string pathOfPhotos)
        {
            coeff_compression = coeff_compression1;
            count_of_clasters = count_of_clasters1;

            Color col;

            Int32[,] bytes = null;
            // double[,] images_data = null;
            Int32[] newImageData = null;
            double[,] result = null;
            int    i = 0, w = 0, h = 0;
            string mainPath = pathOfPhotos;     // "C:\\Users\\Amigo's\\Desktop\\pic\\wrap";

            //string mainPath = "C:\\Users\\Amigo's\\Desktop\\h";

            string[] paths = Directory.GetFiles(mainPath);
            int      count = 0;

            foreach (string path in paths)
            {
                Bitmap bmp = new Bitmap(path);

                bmp = bmp.MakeBitmapSmaller();

                w = bmp.Width;
                h = bmp.Height;

                if (bytes == null)
                {
                    bytes = new Int32[w * h, paths.Length];
                }
                //bytes - массив, первый индекс - номер пикселя, второй индекс - номер фотки, значение массива - цвет

                // images_data = new double[w * h, paths.Length];

                int width  = bmp.Width;
                int height = bmp.Height;
                if (result == null)
                {
                    result = new double[paths.Length, w *h];
                }

                for (int y = 0; y < h; ++y)
                {
                    for (int x = 0; x < w; ++x)
                    {
                        Color color = bmp.GetPixel(x, y);
                        result[count, y *w + x] = ((color.R + color.G + color.B) / 3.0);
                    }
                }
                count++;

                Int32[] tmpimageData = bmp.ToImageData();

                for (uint y = 0; y < h; ++y)
                {
                    for (uint x = 0; x < w; ++x)
                    {
                        col = Color.FromArgb(tmpimageData[(int)y * w + (int)x]);

                        #region  для  компонент цвета Y (YUV),(R+G+B)/3 (RGB), V (HSV)
                        //bytes[(int)y * w + (int)x, i] = (byte)((col.R + col.G + col.B) / 3);                 // RGB/3
                        bytes[(int)y * w + (int)x, i] = (byte)(0.299 * col.R + 0.587 * col.G + 0.114 * col.B);     // Y
                        //bytes[(int)y * w + (int)x, i] = (byte)(Math.Max(col.R, Math.Max(col.G, col.B)));     // V
                        #endregion
                    }
                }
                i++;
                GC.Collect();
            }

            // кластеризация для Y(YUV), (R+G+B)/3, V (HSV)
            double[] descriptors = GetDescriptorsFor_Component(bytes);      //кластеризация для Y,V,rgb/3
            // кластеризация

            int[] returnValues = Claster.K_Means_for_MatStat(descriptors, ref bytes, count_of_clasters);

            /*
             * // если returnValues - номера кластеров то!
             * int count_of_pixels_in_0_claster = 0;
             * for (int k = 0; k < returnValues.GetLength(0); k++)
             * {
             * if (returnValues[k] == 0)
             * {
             *     count_of_pixels_in_0_claster++;
             * }
             * }
             * int[] returns_values = new int[count_of_pixels_in_0_claster];
             * int counter = 0;
             * for (int k = 0; k < returnValues.GetLength(0); k++)
             * {
             * if (returnValues[k] == 0)
             * {
             *     returns_values[counter++] =  (int)middle_Of_Pixels[k];
             * }
             * }
             * // если returnValues - номера кластеров то! возвращаем средние значения
             */
            return(returnValues);    //returns_values;
        }