/// <summary> /// Used to classify image into a category /// </summary> /// <param name="processedImage">Image that has been preprocessed</param> /// <returns>Category that the image belongs to (according to the network)</returns> public string Classify(Bitmap processedImage) { double[] array; _imageToArray.Convert(processedImage, out array); var computedValue = _classificationNetwork.Compute(array)[0]; var result = (int)Math.Round(computedValue * (_allData.Count - 1)); return(_allData.Keys.ToArray()[result]); }
private double[] ConvertImage(Bitmap image) { double[] converted; if (_dataProviderconfiguration.UseGrayScale) { _imageToArray.Convert(image, out converted); } else { ExtractRgb(image, out converted); } return(converted); }
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 = 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]); } }
private void Classify(object sender, EventArgs e) { if (_imageToClassify == null) { label1.Text = "You didn't choose an image!\n"; label1.Refresh(); return; } double[] input; _itoa.Convert(_imageToClassify, out input); double[] output = _network.Compute(input); label1.Text = "Prediction: " + _categories[GetResult(output)]; label1.Refresh(); }
void TrainImage(FileInfo fi) { string value = readIni.ReadValue("ImageInfo", fi.Name); string[] P = value.Split('|'); double[] output = new double[P.Count()]; for (int i = 0; i < P.Count(); i++) { output[i] = double.Parse(P[i]); } Bitmap bitmap = new Bitmap(Image.FromFile(fi.FullName)); ImageToArray conv = new ImageToArray(min: 0, max: 1); double[] input; conv.Convert(bitmap, out input); labelTrain.Text = "0/10"; System.Threading.Tasks.Task.Run(() => { NeuralNetwork network = new NeuralNetwork(0.00125, new int[] { bitmap.Width *bitmap.Height, 6, 5, 5 }); for (int i = 0; i < 10; i++) { network.Train(input.ToList(), output.ToList()); this.Invoke(new Action( delegate() { labelTrain.Text = (i + 1).ToString() + "/" + 10; })); } }); }
public void somTraining() { double[][] input_data = new double[Data.instance.images.Count][]; for (int i = 0; i < images.Count; i++) { Bitmap image = new Bitmap(images[i]); image = preProcess(image); ImageToArray converter = new ImageToArray(0, 1); converter.Convert(image, out input_data[i]); } pca = new PrincipalComponentAnalysis(PrincipalComponentMethod.Center); pca.Learn(input_data); double[][] input_pca = pca.Transform(input_data); //o Clusters(Groups) Count: 4 som_network = new DistanceNetwork(input_pca[0].Count(), 4); som_learning = new SOMLearning(som_network); //o Error Goals: 0.001 //o Max Epochs: 100000 int maxIter = 100000; double maxError = 0.001; for (int i = 0; i < maxIter; i++) { double error = som_learning.RunEpoch(input_pca); if (error < maxError) { break; } } System.Windows.Forms.MessageBox.Show("SOM Training Complete"); }
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]); } }
private void predict_Click(object sender, EventArgs e) { 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[100]; ImageToArray convert = new ImageToArray(0, 1); convert.Convert(img, out input); double[] output = bpnn_net.Compute(input); int result = Convert.ToInt32((Math.Round(output[0]) - 0) * (Data.getInstance().classes.Count - 1 - 0) / (1 - 0) + 0); textBox1.Text = Data.getInstance().classes[result]; MessageBox.Show("Prediction Success"); }
private void btnDone_Click(object sender, EventArgs e) { totalData = Directory.GetDirectories(savedDirectoryName).Length; Console.WriteLine($"Total Data : {totalData}"); var counter = 0; 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); var output = new double[totalData]; inputLabel.Add(label); Array.Clear(output, 0, totalData); output[counter] = 1; inputImgArray.Add(imageAsArray); outputImgArray.Add(output); Console.WriteLine("Result Output"); output.ToList().ForEach(Console.WriteLine); } Console.WriteLine("Item Done"); counter++; outputCount++; } BackPropagationMethod(); Clustering(); MessageBox.Show("Trained Succesfully"); this.Close(); }
/// <summary> /// Image Kmeans. /// </summary> /// <param name="image">Input Image</param> /// <param name="k">Number of colors</param> /// <returns>K dominante colors</returns> public static Color[] GetDominanteColors(Bitmap image, int k) { // Create converters ImageToArray imageToArray = new ImageToArray(min: -1, max: +1); ArrayToImage arrayToImage = new ArrayToImage(1, k, 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, new SquareEuclidean()) { Tolerance = 0.05 }; // Compute the K-Means algorithm until the difference in // cluster centroids between two iterations is below 0.05 kmeans.Learn(pixels); var controids = kmeans.Clusters.Centroids; Bitmap controidsColors; arrayToImage.Convert(controids, out controidsColors); List <Color> results = new List <Color>(); for (var i = 0; i < k; i++) { Color colorPx = controidsColors.GetPixel(0, i); results.Add(colorPx); } return(results.ToArray()); }
/// <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); // 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; }
void cluster(ref Bitmap bitmap) { if (!PreCluster.Checked) { return; } int _k = (int)PartCount.Value; Bitmap image = realImage; 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); bitmap = result; }
//here is where the magic starts private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Image File|*.jpg;*.bmp;*.jpeg"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { clearItem(); Bitmap img = new Bitmap(ofd.FileName); pictureBrowsed.Image = img; img = mainForm.Preprocess(img); ImageToArray imageToArray = new ImageToArray(); double[] input; imageToArray.Convert(img, out input); var pcares = pca.Transform(input); dn.Compute(pcares); var index = dn.GetWinner(); var similarIndex = classMap[index]; foreach (var item in similarIndex) { showItem(item); } } }
private void btnSubmit_Click(object sender, EventArgs e) { Bitmap img = new Bitmap(pictBoxResult.Image); img = mainForm.Preprocess(img); ImageToArray imageToArray = new ImageToArray(); double[] input; imageToArray.Convert(img, out input); var res = an.Compute(input); var highest = 0; for (var i = 0; i < res.Length; i++) { if (res[i] > res[highest]) { highest = i; } } if (resultName[highest] == lblResult.Text) { MessageBox.Show("True"); score = score + 10; NewQuiz(); } else { MessageBox.Show($"Wrong Answer, You Uploaded {resultName[highest]}"); } }
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; }
private void button1_Click(object sender, EventArgs e) { if (Data.instance.images.Count == 0) { System.Windows.Forms.MessageBox.Show("No image for SOM Training..."); return; } if (openFileDialog1.ShowDialog() == DialogResult.OK) { pictureBox1.ImageLocation = openFileDialog1.FileName; pictureBox1.Image = Data.instance.preProcess(new Bitmap(pictureBox1.ImageLocation)); // Main Process listView1.Items.Clear(); imageList1.Images.Clear(); double[] input_data = new double[10 * 10]; Bitmap image = new Bitmap(pictureBox1.Image); image = Data.instance.preProcess(image); ImageToArray converter = new ImageToArray(0, 1); converter.Convert(image, out input_data); double[] input_pca = Data.instance.pca.Transform(input_data); Data.instance.som_network.Compute(input_pca); int winner = Data.instance.som_network.GetWinner(); for (int i = 0; i < Data.instance.images.Count; i++) { double[] input_data2 = new double[10 * 10]; Bitmap image2 = new Bitmap(Data.instance.images[i]); image2 = Data.instance.preProcess(image2); ImageToArray converter2 = new ImageToArray(0, 1); converter2.Convert(image2, out input_data2); double[] input_pca2 = Data.instance.pca.Transform(input_data2); Data.instance.som_network.Compute(input_pca2); int winner2 = Data.instance.som_network.GetWinner(); //bool isSame = false; if (winner == winner2) { Bitmap showImage = Data.instance.images[i]; imageList1.Images.Add(showImage); listView1.Items.Add("", imageList1.Images.Count - 1); } } MessageBox.Show("Find Similar Item Finished!"); } }
public static MeanShiftClusteringResult MeanShiftAccord(Image <Bgr, Byte> image, MeanShiftClusteringAcordParams msParams) { //Image<Bgr, byte> result = new Image<Bgr, byte>(image.Size); //int pixelSize = 3; // RGB color pixel //int kernel = 3; //double sigma = 0.06; // kernel bandwidth int pixelSize = 3; // RGB color pixel int kernel = msParams.Kernel; double sigma = msParams.Sigma; // kernel bandwidth // Load a test image (shown below) Bitmap msImage = image.Bitmap; // Create converters ImageToArray imageToArray = new ImageToArray(min: -1, max: +1); ArrayToImage arrayToImage = new ArrayToImage(msImage.Width, msImage.Height, min: -1, max: +1); // Transform the image into an array of pixel values double[][] pixels; imageToArray.Convert(msImage, out pixels); // Create a MeanShift algorithm using given bandwidth // and a Gaussian density kernel as kernel function. Accord.MachineLearning.MeanShift meanShift = new Accord.MachineLearning.MeanShift(pixelSize, new GaussianKernel(kernel), sigma); // We will compute the mean-shift algorithm until the means // change less than 0.5 between two iterations of the algorithm meanShift.Tolerance = 0.05; meanShift.MaxIterations = 10; // Learn the clusters in the data var clustering = meanShift.Learn(pixels); // Use clusters to decide class labels int[] labels = clustering.Decide(pixels); int regionCount = labels.DistinctCount(); // Replace every pixel with its corresponding centroid pixels.ApplyInPlace((x, i) => meanShift.Clusters.Modes[labels[i]]); // Retrieve the resulting image in a picture box Bitmap msResult; arrayToImage.Convert(pixels, out msResult); Image <Bgr, byte> result = new Image <Bgr, byte>(msResult); //EmguCvWindowManager.Display(result, "msResult"); return(new MeanShiftClusteringResult() { Image = result, Labels = labels, RegionCount = regionCount }); }
void doInputNormalization() { ImageToArray imageConverter = new ImageToArray(0, 1); double[] tempInput; Bitmap imageInput = new Bitmap(pictureBox1.Image); imageConverter.Convert(imageInput, out tempInput); input.Add(tempInput); }
private void btnTraining_Click(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 image = Data.getInstance().preprocessing(Data.getInstance().images[i]); ImageToArray converter = new ImageToArray(0, 1); converter.Convert(image, out input_data[i]); output_data[i] = new double[1]; output_data[i][0] = Data.getInstance().class_indexes[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); int max_iteration = 10000; double max_error = 0.0001; for (int i = 0; i < max_iteration; i++) { double error = som_learning.RunEpoch(input_from_pca); if (error < max_error) { break; } } btnBrowseClustering.Enabled = true; btnTraining.Enabled = false; }
public double[][] ConvertToArrays(Bitmap[] bitmaps) { double[][] inputs = new double[bitmaps.Length][]; var imageToArray = new ImageToArray(min: 0, max: 1); for (int i = 0; i < inputs.Length; i++) { imageToArray.Convert(bitmaps[i], out double[] imgArray); inputs[i] = imgArray; bitmaps[i].Dispose(); } return(inputs); }
private void showSimilarArt(String picturesPath) { pbArtDetail.Image = ImagePreprocessing(new Bitmap(picturesPath), 127); // Convert image to double ImageToArray imageConverter = new ImageToArray(); double[] data; imageConverter.Convert(new Bitmap(pbArtDetail.Image), out data); // Convert to PCA data = principalComponentAnalysis.Transform(data); // Find the similar image Network.distanceNetwork.Compute(data); // Get nearest neuron int winner = Network.distanceNetwork.GetWinner(); imageListSimilarArt.Images.Clear(); listViewSimilarArt.Items.Clear(); for (int i = 0; i < listImages.Count; i++) { double[] imageData = new double[principalComponentAnalysis.Result.GetLength(1)]; for (int j = 0; j < principalComponentAnalysis.Result.GetLength(1); j++) { imageData[j] = principalComponentAnalysis.Result[i, j]; } Network.distanceNetwork.Compute(imageData); int imageWinner = Network.distanceNetwork.GetWinner(); if (winner == imageWinner) { if (listImageNames[i] != picturesPath) { listViewSimilarArt.Groups.Add("groupKey0", "Similar Art"); imageListSimilarArt.Images.Add(listImageNames[i], listImages[i]); ListViewItem listViewItem = new ListViewItem(Path.GetFileName(listImageNames[i]), listImageNames[i], listViewSimilarArt.Groups[0]); listViewSimilarArt.Items.Add(listViewItem); } } } }
private void ShowImage_Load(object sender, EventArgs e) { //Stopwatch stopWatch = new Stopwatch(); //stopWatch.Start(); FI = folderImage.GetFiles().Where(x => x.FullName.EndsWith(".bmp") || x.FullName.EndsWith(".png") || x.FullName.EndsWith(".jpg")).ToArray(); var fileNames = FI.Select(x => x.Name); listViewImage.Columns.Add("Name"); listViewImage.Columns[0].Width = listViewImage.Width; for (int i = 0; i < fileNames.Count(); i++) { listViewImage.Items.Add(fileNames.ToArray()[i]); } LoadImage(); ImageToArray conv = new ImageToArray(min: 0, max: 1); double[] Input; conv.Convert(bm, out Input); //bm = MakeGrayScale(bm); //stopWatch.Stop(); //var y = stopWatch.ElapsedMilliseconds; //var x = ImageToArray(bm); //List<double> lst = d.Cast<double>().ToList(); NeuralNetwork network = new NeuralNetwork(0.00125, new int[] { 2, 4, 1 }); for (int i = 0; i < 1000; i++) { } var output = network.Run(new double[] { 0, 0 }.ToList()); MessageBox.Show(output[0].ToString()); //if (bm.Width + 100 < Screen.PrimaryScreen.Bounds.Width && bm.Height + 100 < Screen.PrimaryScreen.Bounds.Height) //{ // this.Width = bm.Width + 20; // this.Height = bm.Height + 50; // this.CenterToScreen(); // pictureBox1.Width = bm.Width; // pictureBox1.Height = bm.Height; // pictureBox1.Image = bm; //} //else //{ // this.Width = bm.Width / 2 + 20; // this.Height = bm.Height / 2 + 40; // this.CenterToScreen(); // pictureBox1.Width = bm.Width / 2; // pictureBox1.Height = bm.Height / 2; // pictureBox1.Image = bm; //} }
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; }
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 }
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 }
//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); } } }
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"); }
/// <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; }
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); }
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"); }
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(); }
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); }
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]); }
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 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]); } }
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]); }
/// <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; }