示例#1
0
    /*  // this part is not necessary, was created for testing purpose only, it could be removed
    static void Main(string[] args)
    {
        if (args.Length < 1)
            return;
        //arr will represent the original image in 2D array form.
        ImageToArray image = new ImageToArray(new Bitmap(args[0]), 5);

        int[,] arr = image.createArray();
        arr = image.getBlockArray(image);

        // change it to args[1] later for commandline execution and also change the same
        //in printArray(int[,],String path)....
        image.printArray(arr, "C:\\Users\\Debarati\\Desktop\\CS682\\project\\created.txt");
     //   image.printArray(arr_small, "C:\\Users\\Debarati\\Desktop\\CS682\\project\\derived.txt");

    } */
    public int[,] getBlockArray(ImageToArray image)
    {
        //height and width of the original image file
        int num_width = image.m_myImage.Width;
        int num_height = image.m_myImage.Height;

        // height and width of the array representation of the new array to be created
        // each cell represent the walkable or non-walkable block defined earlier

        int workingHeight = num_height / m_blockLength;
        int workingWidth = num_width / m_blockLength;

        // new array to hold information of the image
        int[,] imageArray = new int[workingHeight, workingWidth];

        //any color darker than gray is non walkable
        Color gray = Color.Gray;
        for (int row = 0; row < workingHeight * m_blockLength; row++)
        {
            for (int col = 0; col < workingWidth * m_blockLength; col++)
            {
                Color pixelColor = image.m_myImage.GetPixel(col, row);
                if (pixelColor.R < gray.R && pixelColor.G < gray.G && pixelColor.B < gray.B)
                {
                    imageArray[row / m_blockLength, col / m_blockLength] = 1;
                }
            }
        }
        return imageArray;
    }
示例#2
0
        private void kmeans()
        {
            // Retrieve the number of clusters
            int k = (int)numClusters.Value;

            // Load original image
            Bitmap image = Properties.Resources.leaf;

            // Create conversors
            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
            ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a K-Means algorithm using given k and a
            //  square euclidean distance as distance metric.
            KMeans kmeans = new KMeans(k, Distance.SquareEuclidean);

            // Compute the K-Means algorithm until the difference in
            //  cluster centroids between two iterations is below 0.05
            int[] idx = kmeans.Compute(pixels, 0.05);


            // Replace every pixel with its corresponding centroid
            pixels.ApplyInPlace((x, i) => kmeans.Clusters.Centroids[idx[i]]);

            // Show resulting image in the picture box
            Bitmap result; arrayToImage.Convert(pixels, out result);

            pictureBox.Image = result;
        }
        public void ImageToArrayConstructorTest()
        {
            ImageToArray target = new ImageToArray();

            Assert.AreEqual(0, target.Min);
            Assert.AreEqual(1, target.Max);
            Assert.AreEqual(0, target.Channel);
        }
        public void ImageToArrayConstructorTest1()
        {
            double min = -10;
            double max = +10;
            int channel = 2;

            ImageToArray target = new ImageToArray(min, max, channel);

            Assert.AreEqual(min, target.Min);
            Assert.AreEqual(max, target.Max);
            Assert.AreEqual(channel, target.Channel);
        }
示例#5
0
        private void Form3_Load(object sender, EventArgs e)
        {
            double[][] input_data  = new double[Data.getInstance().images.Count][];
            double[][] output_data = new double[Data.getInstance().images.Count][];
            int        max         = Data.getInstance().classes.Count - 1;
            int        min         = 0;

            for (int i = 0; i < Data.getInstance().images.Count; i++)
            {
                Bitmap img = Data.getInstance().preprocessing(Data.getInstance().images[i]);

                ImageToArray converter = new ImageToArray(0, 1);
                converter.Convert(img, out input_data[i]);

                output_data[i]    = new double[1];
                output_data[i][0] = Data.getInstance().classindex[i];
                output_data[i][0] = 0 + (output_data[i][0] - min) * (1 - 0) / (max - min);
            }

            pca        = new PrincipalComponentAnalysis();
            pca.Method = PrincipalComponentMethod.Center;
            pca.Learn(input_data);

            double[][] input_from_pca = pca.Transform(input_data);

            int a            = 0;
            int output_count = 0;

            while (a < Data.getInstance().classes.Count)
            {
                output_count = a * a;
                a++;
            }

            som_network  = new DistanceNetwork(input_from_pca[0].Count(), output_count);
            som_learning = new SOMLearning(som_network);

            double max_iter = 1000000;
            double max_err  = 0.000001;

            for (int i = 0; i < max_iter; i++)
            {
                double error = som_learning.RunEpoch(input_from_pca);
                if (error < max_err)
                {
                    break;
                }
            }
        }
        public void ConvertTest1()
        {
            ArrayToImage target = new ArrayToImage(16, 16);

            double[] pixels =
            {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 1
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,  // 2
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 3
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 4
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 5
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 6
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 7
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 8
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 9
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 10
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 11
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 12
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,  // 13
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 14
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 15
            };

            Bitmap imageActual;

            target.Convert(pixels, out imageActual);


            double[]     actual;
            ImageToArray c = new ImageToArray();

            c.Convert(imageActual, out actual);

            double[] expected;

            Bitmap imageExpected = Accord.Imaging.Image.Clone(Resources.image1);

            new Invert().ApplyInPlace(imageExpected);
            new Threshold().ApplyInPlace(imageExpected);

            c.Convert(imageExpected, out expected);


            for (int i = 0; i < pixels.Length; i++)
            {
                Assert.AreEqual(actual[i], expected[i]);
            }
        }
示例#7
0
        double[][] extract()
        {
            double[][] hands = new double[dataGridView1.Rows.Count][];
            ImageToArray converter = new ImageToArray(min: -1, max: +1);

            int index = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                Bitmap image = row.Cells["colHand"].Value as Bitmap;
                converter.Convert(image, out hands[index]);
                index++;
            }

            return hands;
        }
示例#8
0
        public void meanShift()
        {
            string basePath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "kmeans");

            Directory.CreateDirectory(basePath);

            #region doc_meanshift
            // Load a test image (shown in a picture box below)
            var    sampleImages = new TestImages(path: basePath);
            Bitmap image        = sampleImages.GetImage("airplane.png");

            // ImageBox.Show("Original", image).Hold();

            // Create converters to convert between Bitmap images and double[] arrays
            var imageToArray = new ImageToArray(min: -1, max: +1);
            var arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);

            // Create a MeanShift algorithm using given bandwidth
            //   and a Gaussian density kernel as kernel function.
            MeanShift meanShift = new MeanShift()
            {
                Kernel    = new GaussianKernel(3),
                Bandwidth = 0.06,

                // We will compute the mean-shift algorithm until the means
                // change less than 0.05 between two iterations of the algorithm
                Tolerance     = 0.05,
                MaxIterations = 10
            };

            // Learn the clusters from the data
            var clusters = meanShift.Learn(pixels);

            // Use clusters to decide class labels
            int[] labels = clusters.Decide(pixels);

            // Replace every pixel with its corresponding centroid
            double[][] replaced = pixels.Apply((x, i) => clusters.Modes[labels[i]]);

            // Retrieve the resulting image (shown in a picture box)
            Bitmap result; arrayToImage.Convert(replaced, out result);

            // ImageBox.Show("Mean-Shift clustering", result).Hold();
            #endregion
        }
示例#9
0
        public void kmeans()
        {
            string basePath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "kmeans");

            Directory.CreateDirectory(basePath);

            #region doc_kmeans
            // Load a test image (shown in a picture box below)
            var    sampleImages = new TestImages(path: basePath);
            Bitmap image        = sampleImages.GetImage("airplane.png");

            // ImageBox.Show("Original", image).Hold();

            // Create converters to convert between Bitmap images and double[] arrays
            var imageToArray = new ImageToArray(min: -1, max: +1);
            var arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a K-Means algorithm using given k and a
            //  square Euclidean distance as distance metric.
            KMeans kmeans = new KMeans(k: 5)
            {
                Distance = new SquareEuclidean(),

                // We will compute the K-Means algorithm until cluster centroids
                // change less than 0.5 between two iterations of the algorithm
                Tolerance = 0.05
            };


            // Learn the clusters from the data
            var clusters = kmeans.Learn(pixels);

            // Use clusters to decide class labels
            int[] labels = clusters.Decide(pixels);

            // Replace every pixel with its corresponding centroid
            double[][] replaced = pixels.Apply((x, i) => clusters.Centroids[labels[i]]);

            // Retrieve the resulting image (shown in a picture box)
            Bitmap result; arrayToImage.Convert(replaced, out result);

            // ImageBox.Show("k-Means clustering", result).Hold();
            #endregion
        }
示例#10
0
 //setting up trained data
 void getTrainedDataArray()
 {
     foreach (var subFolder in Directory.GetDirectories(savedDirectoryName))
     {
         var          label        = new DirectoryInfo(subFolder).Name;
         ImageToArray imageToArray = new ImageToArray();
         foreach (var files in Directory.GetFiles(subFolder))
         {
             var      img = new Bitmap(files);
             double[] imageAsArray;
             imageToArray.Convert(mainForm.Preprocess(img), out imageAsArray);
             trainedData.Add(imageAsArray);
             inputLabel.Add(label);
         }
     }
 }
示例#11
0
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            imageList1.Images.Clear();
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter      = "Images Files (*.jpg, *jpeg, *.png) | *.jpg; *.jpeg; *.png";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap img1 = new Bitmap(openFileDialog1.FileName);
                pictureBox1.Image = img1;
            }
            Bitmap img = new Bitmap(pictureBox1.Image);

            img = Data.getInstance().preprocessing(img);

            double[]     input     = new double[10 * 10];
            ImageToArray converter = new ImageToArray(0, 1);

            converter.Convert(img, out input);

            double[] input_from_pca = pca.Transform(input);
            som_network.Compute(input_from_pca);
            double winner = som_network.GetWinner();

            for (int i = 0; i < Data.getInstance().images.Count; i++)
            {
                Bitmap img2 = Data.getInstance().preprocessing(Data.getInstance().images[i]);

                double[] input2 = new double[10 * 10];

                ImageToArray converter2 = new ImageToArray(0, 1);
                converter2.Convert(img2, out input2);

                double[] input_from_pca2 = pca.Transform(input2);
                som_network.Compute(input_from_pca2);
                double winner2 = som_network.GetWinner();

                if (winner == winner2)
                {
                    imageList1.Images.Add(Data.getInstance().images[i]);
                    listView1.Items.Add(Data.getInstance().filenames[i], imageList1.Images.Count - 1);
                }
            }
            MessageBox.Show("Find Simillar Item Success");
        }
示例#12
0
        /// <summary>
        ///   Runs the Mean-Shift algorithm.
        /// </summary>
        ///
        private void runMeanShift()
        {
            int pixelSize = 3;

            // Retrieve the kernel bandwidth
            double sigma = (double)numBandwidth.Value;

            // Load original image
            Bitmap image = Properties.Resources.leaf;

            // Create converters
            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
            ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a MeanShift algorithm using the given bandwidth
            // and a Gaussian density kernel as the kernel function:

            IRadiallySymmetricKernel kernel = new GaussianKernel(pixelSize);

            var meanShift = new MeanShift(pixelSize, kernel, sigma)
            {
                Tolerance     = 0.05,
                MaxIterations = 10
            };


            // Compute the mean-shift algorithm until the difference
            // in shift vectors between two iterations is below 0.05

            int[] idx = meanShift.Compute(pixels);


            // Replace every pixel with its corresponding centroid
            pixels.ApplyInPlace((x, i) => meanShift.Clusters.Modes[idx[i]]);

            // Show resulting image in the picture box
            Bitmap result; arrayToImage.Convert(pixels, out result);

            pictureBox.Image = result;
        }
示例#13
0
        public static Tile[][] getTiles(Bitmap source, int width, int height)
        {
            int tileWidth  = source.Width / width;
            int tileHeight = source.Height / height;

            Tile[][]     tiles        = new Tile[tileWidth * tileHeight][];
            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);

            double[][] pixels; imageToArray.Convert(source, out pixels);
            for (int yy = 0; yy < tileHeight; yy++)
            {
                for (int xx = 0; xx < tileWidth; xx++)
                {
                    tiles[yy * tileWidth + xx] = new Tile[] { new Tile(width, height, getPixels(pixels, xx * width, width, yy * height, height, source.Width)) };
                }
            }

            return(tiles);
        }
示例#14
0
        public void bpnnTraining()
        {
            double[][] input_data = new double[Data.instance.images.Count][];
            double[][] output_data = new double[Data.instance.indexClasses.Count][];
            int        max_output = Data.instance.classes.Count - 1, min_output = 0;

            for (int i = 0; i < Data.instance.images.Count; i++)
            {
                //Pilih gambar berwarna
                Bitmap image = new Bitmap(Data.instance.images[i]);
                //Preprocess jadi 10 x 10 hitam putih
                image = Data.instance.preProcess(image);
                // dari pixel 0-255 jadi 0-1
                ImageToArray converter = new ImageToArray(0, 1);
                converter.Convert(image, out input_data[i]);

                output_data[i]    = new double[1];
                output_data[i][0] = Data.instance.indexClasses[i];
                output_data[i][0] = 0 + (output_data[i][0] - min_output) * (1 - 0) / (max_output - min_output);
            }

            bpnnNetwork  = new ActivationNetwork(new SigmoidFunction(), 100, 3, 1);
            bpnnLearning = new BackPropagationLearning(bpnnNetwork);

            //o   Error Goals: 0.000001
            //o   Max Epochs: 1000000

            int    max_iter  = 1000000;
            double max_error = 0.000001;

            for (int i = 0; i < max_iter; i++)
            {
                double error = bpnnLearning.RunEpoch(input_data, output_data);

                if (error < max_error)
                {
                    break;
                }
            }

            bpnnNetwork.Save("an.bin");
        }
示例#15
0
        private void btnFeature_Click(object sender, EventArgs e)
        {
            if (pca == null)
            {
                MessageBox.Show("Please compute the analysis first!");
                return;
            }

            ImageToArray converter = new ImageToArray(min: -1, max: +1);

            int rows = dataGridView3.Rows.Count;

            double[][] inputs   = new double[rows][];
            double[][] features = new double[rows][];
            int[]      outputs  = new int[rows];

            int index = 0;

            foreach (DataGridViewRow row in dataGridView3.Rows)
            {
                Bitmap image = row.Cells["colHand2"].Value as Bitmap;
                int    label = (int)row.Cells["colLabel2"].Value;

                double[] input;
                converter.Convert(image, out input);

                double[] feature = pca.Transform(input);

                row.Cells["colProjection"].Value = feature.ToString("N2");

                row.Tag         = feature;
                inputs[index]   = input;
                features[index] = feature;
                outputs[index]  = label;
                index++;
            }

            classifier = new MinimumMeanDistanceClassifier(features, outputs);

            btnClassify.Enabled = true;
        }
示例#16
0
        static void TestMeanShift()
        {
            Bitmap image = Accord.Imaging.Image.FromUrl("https://c1.staticflickr.com/4/3209/2527630511_fae07530c2_b.jpg");

            //ImageBox.Show("Original", image).Hold();

            // Create converters to convert between Bitmap images and double[] arrays
            var imageToArray = new ImageToArray(min: -1, max: +1);
            var arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a MeanShift algorithm using given bandwidth
            //   and a Gaussian density kernel as kernel function.
            MeanShift meanShift = new MeanShift()
            {
                Kernel    = new EpanechnikovKernel(),
                Bandwidth = 0.1,

                // We will compute the mean-shift algorithm until the means
                // change less than 0.05 between two iterations of the algorithm
                Tolerance     = 0.05,
                MaxIterations = 10
            };

            // Learn the clusters from the data
            var clusters = meanShift.Learn(pixels);

            // Use clusters to decide class labels
            int[] labels = clusters.Decide(pixels);

            // Replace every pixel with its corresponding centroid
            double[][] replaced = pixels.Apply((x, i) => clusters.Modes[labels[i]]);

            // Retrieve the resulting image (shown in a picture box)
            Bitmap result; arrayToImage.Convert(replaced, out result);

            //ImageBox.Show("Mean-Shift clustering", result).Hold();
        }
示例#17
0
        private void btnPredict_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.ImageLocation = openFileDialog1.FileName;
                pictureBox1.Image         = Data.instance.preProcess(new Bitmap(pictureBox1.ImageLocation));

                double[] input_data;

                int          max_output = Data.instance.classes.Count - 1, min_output = 0;
                Bitmap       image     = new Bitmap(pictureBox1.Image);
                ImageToArray converter = new ImageToArray(0, 1);
                converter.Convert(image, out input_data);

                double[] output = Data.instance.bpnnNetwork.Compute(input_data);

                int result = Convert.ToInt32(min_output + Math.Round(output[0] - 0) * (max_output - min_output) / (1 - 0));

                MessageBox.Show(output[0] + " has result " + result);
                textBox1.Text = Data.instance.classes[result];
                MessageBox.Show("Prediction Complete");
            }
        }
示例#18
0
        private void principalComponentAnalysisProcessing()
        {
            double[][] datas = new double[listImages.Count][];
            double[]   data;

            ImageToArray imageConverter = new ImageToArray();

            for (int i = 0; i < listImages.Count; i++)
            {
                // Image Preprocessing
                Bitmap preprocessedImage = ImagePreprocessing(listImages[i], 127);

                // Convert to Double[]
                imageConverter.Convert(preprocessedImage, out data);

                // Input Dataset
                datas[i] = data;
            }

            // Compute PCA
            principalComponentAnalysis = new PrincipalComponentAnalysis(datas);
            principalComponentAnalysis.Compute();
        }
示例#19
0
        private void button2_Click(object sender, EventArgs e)
        {
            preprocess();

            //normalisasi input yang mau di recognize
            ImageToArray imageConverter = new ImageToArray(0, 1);

            double[] recognizeInput;

            Bitmap preprocessedImage = new Bitmap(pictureBox1.Image);

            imageConverter.Convert(preprocessedImage, out recognizeInput);

            double[] recognizeOutput = brain.Compute(recognizeInput);

            //denormalisasi
            int imageIndex = (int)Math.Round(recognizeOutput[0] * output.Count());

            label3.Text += (imageName[imageIndex]);

            button2.Hide();
            button1.Show();
        }
示例#20
0
        public void ConvertTest3()
        {
            double[] pixels =
            {
                0, 0, 0, 0,
                0, 1, 1, 0,
                0, 1, 1, 0,
                0, 0, 0, 0,
            };

            ArrayToImage conv1 = new ArrayToImage(width: 4, height: 4);
            Bitmap       image;

            conv1.Convert(pixels, out image);
            image = new ResizeNearestNeighbor(16, 16).Apply(image);


            // Obtain a 16x16 bitmap image
            // Bitmap image = ...

            // Show on screen
            // ImageBox.Show(image, PictureBoxSizeMode.Zoom);

            // Create the converter to convert the image to an
            //   array containing only values between 0 and 1
            ImageToArray conv = new ImageToArray(min: 0, max: 1);

            // Convert the image and store it in the array
            double[] array; conv.Convert(image, out array);

            // Show the array on screen
            // ImageBox.Show(array, 16, 16, PictureBoxSizeMode.Zoom);

            Assert.AreEqual(0, array.Min());
            Assert.AreEqual(1, array.Max());
            Assert.AreEqual(16 * 16, array.Length);
        }
示例#21
0
        private void cluster(ushort j)
        {
            cvsbmp = UtilFn.BitmapImage2Bitmap(images[j]);
            var imageToArray = new ImageToArray(min: -1, max: +1);
            var arrayToImage = new ArrayToImage(cvsbmp.Width, cvsbmp.Height, min: -1, max: +1);
            int kk;

            double[][] pixels; imageToArray.Convert(cvsbmp, out pixels);
            try
            {
                kk = Int16.Parse(kCluster.Text);
            }
            catch (Exception)
            {
                return;
            }

            if (kk < 1)
            {
                return;
            }
            KMeans kmeans = new KMeans(k: kk)
            {
                Distance  = new SquareEuclidean(),
                Tolerance = 0.05
            };

            var clusters = kmeans.Learn(pixels);

            int[] labels = clusters.Decide(pixels);

            double[][] replaced = pixels.Apply((x, i) => clusters.Centroids[labels[i]]);

            Bitmap result; arrayToImage.Convert(replaced, out result);

            imagesEdited.Add(converter.Convert(result, Type.GetType("BitmapImage"), null, null) as BitmapImage);
        }
示例#22
0
        private void runKMeans()
        {
            int k = (int)numClusters.Value;

            Bitmap image = img;

            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
            ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            double[][] pixels; imageToArray.Convert(image, out pixels);

            KMeans kmeans = new KMeans(k, new SquareEuclidean())
            {
                Tolerance = 0.05
            };

            int[] idx = kmeans.Learn(pixels).Decide(pixels);

            pixels.Apply((x, i) => kmeans.Clusters.Centroids[idx[i]], result: pixels);

            Bitmap result; arrayToImage.Convert(pixels, out result);

            pictureBox1.Image = result;
        }
示例#23
0
        private void btnCluster_Click(object sender, EventArgs e)
        {
            Bitmap image = new Bitmap(pictureBox1.Image);

            image             = Data.getInstance().preprocessing(image);
            pictureBox1.Image = image;

            double[]     input     = new double[100 * 100];
            ImageToArray converter = new ImageToArray(0, 1);

            converter.Convert(image, out input);

            double[] input_from_pca = pca.Transform(input);
            som_network.Compute(input_from_pca);
            double winner = som_network.GetWinner();

            MessageBox.Show("Winner : " + winner);

            for (int i = 0; i < Data.getInstance().images.Count; i++)
            {
                Bitmap image2 = Data.getInstance().preprocessing(Data.getInstance().images[i]);

                double[]     input2     = new double[100 * 100];
                ImageToArray converter2 = new ImageToArray(0, 1);
                converter2.Convert(image2, out input2);

                double[] input_from_pca2 = pca.Transform(input2);
                som_network.Compute(input_from_pca2);
                double check_winner = som_network.GetWinner();

                if (winner == check_winner)
                {
                    listBox1.Items.Add(Data.getInstance().file_names[i] + " - " + check_winner);
                }
            }
        }
示例#24
0
        /// <summary>
        ///   Runs the K-Means algorithm.
        /// </summary>
        ///
        private void runKMeans()
        {
            // Retrieve the number of clusters
            int k = (int)numClusters.Value;

            // Load original image
            Bitmap image = Properties.Resources.leaf;

            // Create converters
            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
            ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a K-Means algorithm using given k and a
            //  square Euclidean distance as distance metric.
            KMeans kmeans = new KMeans(k, Distance.SquareEuclidean)
            {
                Tolerance = 0.05
            };

            // Compute the K-Means algorithm until the difference in
            //  cluster centroids between two iterations is below 0.05
            int[] idx = kmeans.Compute(pixels);


            // Replace every pixel with its corresponding centroid
            pixels.ApplyInPlace((x, i) => kmeans.Clusters.Centroids[idx[i]]);

            // Show resulting image in the picture box
            Bitmap result; arrayToImage.Convert(pixels, out result);

            pictureBox.Image = result;
        }
示例#25
0
        private void train_Click(object sender, EventArgs e)
        {
            double[][] input  = new double[Data.getInstance().images.Count()][];
            double[][] output = new double[Data.getInstance().images.Count()][];
            int        max    = Data.getInstance().classes.Count() - 1;
            int        min    = 0;

            for (int i = 0; i < Data.getInstance().images.Count(); i++)
            {
                Bitmap       img       = Data.getInstance().preprocessing(Data.getInstance().images[i]);
                ImageToArray converter = new ImageToArray(0, 1);
                converter.Convert(img, out input[i]);

                output[i]    = new double[1];
                output[i][0] = Data.getInstance().classindex[i];
                output[i][0] = 0 + (output[i][0] - min) * (1 - 0) / (max - min);
            }

            bpnn_net   = new ActivationNetwork(new SigmoidFunction(), 100, 4, 1);
            bpnn_learn = new BackPropagationLearning(bpnn_net);

            double max_iter = 1000000;
            double max_err  = 0.000001;

            for (double i = 0; i < max_iter; i++)
            {
                double err = bpnn_learn.RunEpoch(input, output);
                if (err < max_err)
                {
                    break;
                }
            }

            predict.Enabled = true;
            MessageBox.Show("Computing BPNN Success");
        }
示例#26
0
        public Browse()
        {
            InitializeComponent();

            if (listView1.Items.Count == 0)
            {
                MessageBox.Show("Add some art first");
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;
                List <double[]> PCAInput = new List <double[]>();

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    for (int i = 0; i < dialog.FileNames.Length; i++)
                    {
                        Bitmap image = new Bitmap(dialog.FileNames.ElementAt(i));
                        image = preprocessImage(image);
                        double[]     output;
                        ImageToArray converter = new ImageToArray();
                        converter.Convert(image, out output);
                        PCAInput.Add(output);
                        pca = new PrincipalComponentAnalysis(PCAInput.ToArray());
                        pca.Compute();
                    }
                }

                dn  = new DistanceNetwork(pca.Result.GetLength(0), 4);
                som = new SOMLearning(dn);

                double[][] inputSOM = new double[pca.Result.GetLength(0)][];
                for (int i = 0; i < pca.Result.GetLength(1); i++)
                {
                    inputSOM[i] = new double[pca.Result.GetLength(1)];
                    for (int j = 0; j < pca.Result.GetLength(1); j++)
                    {
                        inputSOM[i][j] = pca.Result[i, j];
                    }
                }
                //training som
                int    epoch    = 10000;
                double minError = 0.0000001;
                for (int i = 0; i < epoch; i++)
                {
                    double error = som.RunEpoch(inputSOM);
                    if (error < minError)
                    {
                        break;
                    }
                }

                //clustering
                for (int i = 0; i < pca.Result.GetLength(0); i++)
                {
                    dn.Compute(inputSOM[i].ToArray());
                    int winner = dn.GetWinner();

                    ListViewGroup group;
                    if (listView1.Groups[winner.ToString()] == null)
                    {
                        //bkin group baru
                        group = new ListViewGroup(winner.ToString(), "" + winner);
                    }
                    else
                    {
                        //masukin ke group lama
                        group = listView1.Groups[winner.ToString()];
                    }

                    listView1.Groups.Add(group);
                    //listView1.Items.Add(new ListViewItem(dialog.SafeFileNames[i], i, group));
                    //imageList1.Images.Add(new Bitmap(dialog.FileNames.ElementAt(i)));
                }
            }
        }
示例#27
0
        public void ConvertTest4()
        {
            ImageToArray target = new ImageToArray(min: 0, max: 255);
            Bitmap       image  = Accord.Imaging.Image.Clone(Properties.Resources.image3);

            Assert.AreEqual(PixelFormat.Format32bppArgb, image.PixelFormat);

            {
                double[] output;
                double[] outputExpected =
                {
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 0
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 1
                    0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 2
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 3
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 4
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 5
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 6
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 7
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 8
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 9
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 10
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 11
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 12
                    0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 13
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 14
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      // 15
                };

                target.Channel = RGB.R;
                target.Convert(image, out output);

                for (int i = 0; i < outputExpected.Length; i++)
                {
                    Assert.AreEqual(outputExpected[i], output[i]);
                }
            }

            {
                double[] output;
                double[] outputExpected =
                {
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 0
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 1
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0,    // 2
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 3
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 4
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 5
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 6
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 7
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 8
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 9
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 10
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 11
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 12
                    0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 13
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 14
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 15
                };

                target.Channel = RGB.G;
                target.Convert(image, out output);

                for (int i = 0; i < outputExpected.Length; i++)
                {
                    for (int j = 0; j < outputExpected.Length; j++)
                    {
                        Assert.AreEqual(outputExpected[i], output[i]);
                    }
                }
            }

            {
                double[] output;
                double[] outputExpected =
                {
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 0
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 1
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 2
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 3
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 4
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 5
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 6
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 7
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 8
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 9
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 10
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 11
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 12
                    0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0,    // 13
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 14
                    0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0,    // 15
                };

                target.Channel = RGB.B;
                target.Convert(image, out output);

                for (int i = 0; i < outputExpected.Length; i++)
                {
                    Assert.AreEqual(outputExpected[i], output[i]);
                }
            }
        }
示例#28
0
        public void ConvertTest1()
        {

            ArrayToImage target = new ArrayToImage(16, 16);

            double[] pixels = 
            {
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
                 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 2 
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 11
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 12
                 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 13
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15
            };

            Bitmap imageActual;
            target.Convert(pixels, out imageActual);


            double[] actual;
            ImageToArray c = new ImageToArray();
            c.Convert(imageActual, out actual);

            double[] expected;

            Bitmap imageExpected = Accord.Imaging.Image.Clone(Properties.Resources.image1);
            new Invert().ApplyInPlace(imageExpected);
            new Threshold().ApplyInPlace(imageExpected);

            c.Convert(imageExpected, out expected);


            for (int i = 0; i < pixels.Length; i++)
                Assert.AreEqual(actual[i], expected[i]);
        }
示例#29
0
        public static async Task <string> ProcessFormFile(IFormFile formFile,
                                                          ModelStateDictionary modelState)
        {
            var fieldDisplayName = string.Empty;

            // Use reflection to obtain the display name for the model
            // property associated with this IFormFile. If a display
            // name isn't found, error messages simply won't show
            // a display name.
            MemberInfo property =
                typeof(FileUpload).GetProperty(
                    formFile.Name.Substring(formFile.Name.IndexOf(".") + 1));

            if (property != null)
            {
                var displayAttribute =
                    property.GetCustomAttribute(typeof(DisplayAttribute))
                    as DisplayAttribute;

                if (displayAttribute != null)
                {
                    fieldDisplayName = $"{displayAttribute.Name} ";
                }
            }

            // Use Path.GetFileName to obtain the file name, which will
            // strip any path information passed as part of the
            // FileName property. HtmlEncode the result in case it must
            // be returned in an error message.
            var fileName = WebUtility.HtmlEncode(
                Path.GetFileName(formFile.FileName));

            // Check the file length and don't bother attempting to
            // read it if the file contains no content. This check
            // doesn't catch files that only have a BOM as their
            // content, so a content length check is made later after
            // reading the file's content to catch a file that only
            // contains a BOM.
            if (formFile.Length == 0)
            {
                modelState.AddModelError(formFile.Name,
                                         $"The {fieldDisplayName}file ({fileName}) is empty.");
            }
            else if (formFile.Length > 1048576)
            {
                modelState.AddModelError(formFile.Name,
                                         $"The {fieldDisplayName}file ({fileName}) exceeds 1 MB.");
            }
            else
            {
                try
                {
                    string fileContents;

                    // The StreamReader is created to read files that are UTF-8 encoded.
                    // If uploads require some other encoding, provide the encoding in the
                    // using statement. To change to 32-bit encoding, change
                    // new UTF8Encoding(...) to new UTF32Encoding().
                    using (var ms = new MemoryStream())
                    {
                        formFile.CopyTo(ms);
                        ImageToArray img = new ImageToArray();
                        await img.request_AIAsync(ms);
                    }
                }
                catch (Exception ex)
                {
                    modelState.AddModelError(formFile.Name,
                                             $"The {fieldDisplayName}file ({fileName}) upload failed. " +
                                             $"Please contact the Help Desk for support. Error: {ex.Message}");
                    // Log the exception
                }
            }

            return(fileName);
        }
        public void ConvertTest4()
        {
            ImageToArray target = new ImageToArray(min: 0, max: 255);
            Bitmap image = Properties.Resources.image3;
            Assert.AreEqual(PixelFormat.Format32bppArgb, image.PixelFormat);

            {
                double[] output;
                double[] outputExpected =
                {
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 0
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 1
                      0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 2 
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 3
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 4
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 5
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 6
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 7
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 8
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 9
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 10
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 11
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 12
                      0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 13
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 14
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 15
                };

                target.Channel = RGB.R;
                target.Convert(image, out output);

                for (int i = 0; i < outputExpected.Length; i++)
                        Assert.AreEqual(outputExpected[i], output[i]);
            }

            {
                double[] output;
                double[] outputExpected =
                {
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 0
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 1
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0 , // 2 
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 3
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 4
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 5
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 6
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 7
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 8
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 9
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 10
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 11
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 12
                      0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 13
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 14
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 15
                };

                target.Channel = RGB.G;
                target.Convert(image, out output);

                for (int i = 0; i < outputExpected.Length; i++)
                    for (int j = 0; j < outputExpected.Length; j++)
                        Assert.AreEqual(outputExpected[i], output[i]);
            }

            {
                double[] output;
                double[] outputExpected =
                {
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 0
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 1
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 2 
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 3
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 4
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 5
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 6
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 7
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 8
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 9
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 10
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 11
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 12
                      0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0 , // 13
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 14
                      0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0 , // 15
                };

                target.Channel = RGB.B;
                target.Convert(image, out output);

                for (int i = 0; i < outputExpected.Length; i++)
                        Assert.AreEqual(outputExpected[i], output[i]);
            }
        }
示例#31
0
        /// <summary>
        ///   Runs the Mean-Shift algorithm.
        /// </summary>
        /// 
        private void runMeanShift()
        {
            int pixelSize = 3;

            // Retrieve the kernel bandwidth
            double sigma = (double)numBandwidth.Value;

            // Load original image
            Bitmap image = Properties.Resources.leaf;

            // Create converters
            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
            ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a MeanShift algorithm using the given bandwidth
            // and a Gaussian density kernel as the kernel function:

            IRadiallySymmetricKernel kernel = new GaussianKernel(pixelSize);
            
            var meanShift = new MeanShift(pixelSize, kernel, sigma)
            {
                Tolerance = 0.05,
                MaxIterations = 10
            };

            
            // Compute the mean-shift algorithm until the difference 
            // in shift vectors between two iterations is below 0.05
            
            int[] idx = meanShift.Compute(pixels);


            // Replace every pixel with its corresponding centroid
            pixels.ApplyInPlace((x, i) => meanShift.Clusters.Modes[idx[i]]);

            // Show resulting image in the picture box
            Bitmap result; arrayToImage.Convert(pixels, out result);

            pictureBox.Image = result;
        }
示例#32
0
        public void ConvertTest2()
        {
            Bitmap image = Properties.Resources.image1;

            ImageToArray target = new ImageToArray();

            double[] pixels;
            double[] pixelsExpected =
            {
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
                 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 2 
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 11
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 12
                 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 13
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15
            };


            target.Convert(image, out pixels);

            for (int i = 0; i < pixels.Length; i++)
                    Assert.AreEqual(pixelsExpected[i], pixelsExpected[i]);
        }
示例#33
0
        private void btnFeature_Click(object sender, EventArgs e)
        {
            if (pca == null)
            {
                MessageBox.Show("Please compute the analysis first!");
                return;
            }

            ImageToArray converter = new ImageToArray(min: -1, max: +1);

            int rows = dataGridView3.Rows.Count;
            double[][] inputs = new double[rows][];
            double[][] features = new double[rows][];
            int[] outputs = new int[rows];

            int index = 0;
            foreach (DataGridViewRow row in dataGridView3.Rows)
            {
                Bitmap image = row.Cells["colHand2"].Value as Bitmap;
                int label = (int)row.Cells["colLabel2"].Value;

                double[] input;
                converter.Convert(image, out input);

                double[] feature = pca.Transform(input);

                row.Cells["colProjection"].Value = feature.ToString("N2");

                row.Tag = feature;
                inputs[index] = input;
                features[index] = feature;
                outputs[index] = label;
                index++;
            }

            classifier = new MinimumMeanDistanceClassifier(features, outputs);

            btnClassify.Enabled = true;
        }
        public void ConvertTest3()
        {
            double[] pixels = 
            {
                 0, 0, 0, 0,
                 0, 1, 1, 0,
                 0, 1, 1, 0,
                 0, 0, 0, 0,
            };

            ArrayToImage conv1 = new ArrayToImage(width: 4, height: 4);
            Bitmap image;
            conv1.Convert(pixels, out image);
            image = new ResizeNearestNeighbor(16, 16).Apply(image);


            // Obtain a 16x16 bitmap image
            // Bitmap image = ...

            // Show on screen
            // ImageBox.Show(image, PictureBoxSizeMode.Zoom);

            // Create the converter to convert the image to an
            //   array containing only values between 0 and 1 
            ImageToArray conv = new ImageToArray(min: 0, max: 1);

            // Convert the image and store it in the array
            double[] array; conv.Convert(image, out array);

            // Show the array on screen
            // ImageBox.Show(array, 16, 16, PictureBoxSizeMode.Zoom);

            Assert.AreEqual(0, array.Min());
            Assert.AreEqual(1, array.Max());
            Assert.AreEqual(16 * 16, array.Length);
        }
示例#35
0
        static void Main(string[] args)
        {
            //Bitmap image1 = new Bitmap(@"D:\eigenfaces edit resize\image1.bmp");
            //Bitmap image1 = new Bitmap(@"D:\eigenfaces edit resize test\image1.bmp");
            //Bitmap image1 = new Bitmap(@"D:\eigenfaces edit black\image1.bmp");


            List <Bitmap> trainingFaces       = new List <Bitmap>();
            List <Bitmap> testingFaces        = new List <Bitmap>();
            List <Bitmap> settingFaces        = new List <Bitmap>();
            int           imageWidth          = 192;
            int           imageHeight         = 168;
            int           trainingImageNumber = 44;
            //int settingImageNumber = 5;
            int testingImageNumber = 21;

            for (int i = 1; i <= trainingImageNumber + testingImageNumber; i++)
            {
                string path = string.Format(@"yaleB01\subject1 ({0}).bmp", i);

                Bitmap newBitmap = new Bitmap(path);
                if (i <= trainingImageNumber)
                {
                    trainingFaces.Add(newBitmap);
                }
                else
                {
                    testingFaces.Add(newBitmap);
                }
            }

            //for (int i = 1; i <= trainingImageNumber + testingImageNumber; i++)
            //{

            //    string path = string.Format(@"yaleB03\subject3 ({0}).bmp", i);

            //    Bitmap newBitmap = new Bitmap(path);
            //    if (i <= trainingImageNumber)
            //        trainingFaces.Add(newBitmap);
            //    else
            //        testingFaces.Add(newBitmap);

            //}


            string path1      = string.Format(@"yaleB01\tree.bmp");
            Bitmap newBitmap1 = new Bitmap(path1);

            testingFaces.Add(newBitmap1);

            path1      = string.Format(@"yaleB01\puppy.bmp");
            newBitmap1 = new Bitmap(path1);
            testingFaces.Add(newBitmap1);

            //path1 = string.Format(@"yaleB01\subject3 (3).bmp");
            //newBitmap1 = new Bitmap(path1);
            //testingFaces.Add(newBitmap1);


            ImageToArray converter = new ImageToArray(-1, +1);

            List <double[]> trainingOutputList = new List <double[]>();

            foreach (Bitmap bitmap in trainingFaces)
            {
                double[] newOutput;
                converter.Convert(bitmap, out newOutput);
                trainingOutputList.Add(newOutput);
            }

            List <double[]> testingOutputList = new List <double[]>();

            foreach (Bitmap bitmap in testingFaces)
            {
                double[] newOutput;
                converter.Convert(bitmap, out newOutput);
                testingOutputList.Add(newOutput);
            }

            //List<double[]> settingOutputList = new List<double[]>();
            //foreach (Bitmap bitmap in settingFaces)
            //{
            //    double[] newOutput;
            //    converter.Convert(bitmap, out newOutput);
            //    settingOutputList.Add(newOutput);
            //}



            //ArrayToImage ati1 = new ArrayToImage(image1.Height, image1.Width);
            //Bitmap test = new Bitmap(image1.Height, image1.Width);
            //ati1.Convert(output1, out test);
            //test.Save(@"d:\eigenface1.bmp");



            int size = imageHeight * imageWidth;

            double[,] data = new double[trainingFaces.Count, size];


            for (int i = 0; i < trainingOutputList.Count; i++)
            {
                data.SetRow(i, trainingOutputList[i]);
            }


            ObjectPCA obj = new ObjectPCA(data);

            obj.take2();
            obj.setMaxValue();
            var finalData = obj.W;

            //var x = obj.projectImage(testingOutputList[10].Transpose());
            //Console.WriteLine(x);
            double x;
            double max = 0;

            //foreach (var row in settingOutputList)
            //{
            //    x = obj.projectImage(row.Transpose());
            //    if (x > max)
            //        max = x;
            //}
            Console.WriteLine("max " + obj.MaxValue);
            int bad = 0;
            int v   = 0;

            foreach (var row in testingOutputList)
            {
                x = obj.projectImage(row.Transpose());
                // Console.WriteLine(x+" "+v++);
                if (x > obj.MaxValue)
                {
                    Console.WriteLine(testingOutputList.IndexOf(row) + trainingImageNumber + " " + x);
                    bad++;
                }
            }
            Console.WriteLine("eroare " + bad / (testingImageNumber + 0.0) * 100);


            //finalData = obj.W;

            //ArrayToImage ati = new ArrayToImage(imageHeight, imageWidth);
            //ati.Min = finalData.Min();
            //ati.Max = finalData.Max();
            //Bitmap eigenface = new Bitmap(imageHeight, imageWidth);

            //for (int i = 0; i < finalData.Columns(); i++)
            //{
            //    string path = string.Format(@"D:\eigenfaces result\image{0}.bmp", i);
            //    ati.Convert(finalData.GetColumn(i), out eigenface);
            //    eigenface.Save(path);
            //}


            //obj.Compute();
            //double[,] finalData = obj.KernelData;


            //var image = testingOutputList[14].Transpose().Dot(finalData.Transpose());

            // var image1 = data.Transpose().Dot(finalData.GetColumn(0));
            //double[,] finalData = obj.FinalData;


            //double[,] finalData =obj.plotPointPCA(testingOutputList[0]);
            //foreach (var face in trainingOutputList)
            //    obj.faceRecognition(face);


            //-------------------------------------------------

            //obj.Gamma = Math.Pow(10, -3);
            //obj.ComputeKernel();

            //int[] indexesInitial = new int[testingOutputList.Count];
            //for (int i = 0; i < testingOutputList.Count; i++)
            //    if (i < testingOutputList.Count / 2)
            //        indexesInitial[i] = 1;
            //    else
            //        indexesInitial[i] = 2;

            //double min1 = double.MaxValue, max1 = double.MinValue, min2 = min1, max2 = max1;
            //for (int i = 0; i < trainingOutputList.Count; i++)
            //{
            //    if (i < trainingOutputList.Count / 2)
            //    {
            //        if (obj.KernelVectors[i] < min1)
            //            min1 = obj.KernelVectors[i];
            //        if (obj.KernelVectors[i] > max1)
            //            max1 = obj.KernelVectors[i];
            //    }
            //    else
            //    {
            //        if (obj.KernelVectors[i] < min2)
            //            min2 = obj.KernelVectors[i];
            //        if (obj.KernelVectors[i] > max2)
            //            max2 = obj.KernelVectors[i];
            //    }
            //}

            //int x = 0;

            //double mean1 = min2 - (max1 + min2) / 2;
            //double separationPoint = min2 + Math.Abs(mean1);

            //int[] indexesFinal = new int[testingOutputList.Count];

            //foreach (var face in testingOutputList)
            //{
            //    System.Console.WriteLine(x++);
            //    double aux = obj.plotPointKernelPCA(face);
            //    System.Console.WriteLine(aux);
            //    if (aux < separationPoint)
            //        indexesFinal[testingOutputList.IndexOf(face)] = 1;
            //    else
            //        indexesFinal[testingOutputList.IndexOf(face)] = 2;

            //}

            //Console.WriteLine(obj.KernelVectors.ToString("+0.0000;-0.0000"));
            //Console.WriteLine();
            //Console.WriteLine(obj.KernelValues);
            //Console.WriteLine();
            //Console.WriteLine(indexesInitial.ToString("+0.0000;-0.0000"));
            //Console.WriteLine(indexesFinal.ToString("+0.0000;-0.0000"));
            //Console.WriteLine(separationPoint);

            //------------------------------------------------------------


            //int minimi = 0;
            //foreach (var training in testingOutputList)
            //{

            //    if (obj.faceRecognition(training) < 105)
            //        minimi++;
            //}
            //System.Console.WriteLine(minimi);


            //ArrayToImage ati = new ArrayToImage(imageHeight, imageWidth);
            //ati.Min = finalData.Min();
            //ati.Max = finalData.Max();
            //Bitmap eigenface = new Bitmap(imageHeight, imageWidth);

            //for (int i = 0; i < finalData.Columns(); i++)
            //{
            //    string path = string.Format(@"D:\eigenfaces result\image{0}.bmp", i);
            //    ati.Convert(finalData.GetColumn(i), out eigenface);
            //    eigenface.Save(path);
            //}


            //finalData = image;
            //for (int i = 0; i < finalData.Columns(); i++)
            //{
            //    string path = string.Format(@"D:\eigenfaces result\image{0}.bmp", i);
            //    ati.Convert(finalData.GetColumn(i), out eigenface);
            //    eigenface.Save(path);
            //}


            //string path2 = string.Format(@"D:\eigenfaces result\image{0}.bmp", "asd");
            //ati.Convert(image1, out eigenface);
            //eigenface.Save(path2);
            //eigenface.Dispose();



            foreach (Bitmap bitmap in trainingFaces)
            {
                bitmap.Dispose();
            }
            //Console.WriteLine(output.ToString("+0.00;-0.00"));
            //ArrayToImage ati = new ArrayToImage(image1.Height, image1.Width);
            //Bitmap image2 = new Bitmap(image1.Height, image1.Width);
            //ati.Convert(output, out image2);
            //image2.Save("d:\\image.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            //StreamWriter sw = new StreamWriter("data.txt");
            //foreach (double x in output)
            //    sw.Write(x + " ");
        }
示例#36
0
 public DataProvider(DataProviderConfiguration dataProviderconfiguration)
 {
     _dataProviderconfiguration = dataProviderconfiguration;
     _imageToArray = new ImageToArray(min: 0, max: 1);
 }