private void HsvHistButton_Click(object sender, EventArgs e) { int HBins = 50; int SBins = 0; int VBins = 0; bool isCorrectValue = false; if (HistDim == 1 && int.TryParse(HBinTextBox.Text, out HBins)) { isCorrectValue = true; } else if (HistDim == 2 && int.TryParse(HBinTextBox.Text, out HBins) && int.TryParse(SBinTextBox.Text, out SBins)) { isCorrectValue = true; } else { MessageBox.Show("數值不對"); } if (wantExtractFeatureImage != null && isCorrectValue) { if (learningSys != null) { learningSys.SetLearningImage(wantExtractFeatureImage); } else { learningSys = new FeatureLearning(wantExtractFeatureImage); } templateHist = learningSys.CalHist(HistDim, HBins, SBins, VBins); if (HistDim <= 2) { showTemplateHistImg = SystemToolBox.DrawHsvHistogram(templateHist); ShowHistViewer(templateHistImgBox, showTemplateHistImg, "樣板影像"); } else { MessageBox.Show("Dim = " + HistDim + ",can;t draw"); } } else { MessageBox.Show("圖片未載入"); } }
private void loadImgButton_Click(object sender, EventArgs e) { string fileName = OpenLearningImgFile(); if (fileName != null) { loadImg = new Image <Bgr, byte>(fileName); if (learningSys != null) { learningSys.SetLearningImage(fileName); } else { learningSys = new FeatureLearning(fileName); } loadImgBox.Image = loadImg.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); } }
private void extractFeatureButton_Click(object sender, EventArgs e) { if (wantExtractFeatureImage != null) { if (learningSys != null) { learningSys.SetLearningImage(wantExtractFeatureImage); } else { learningSys = new FeatureLearning(wantExtractFeatureImage); } surfData = learningSys.CalSURFFeature(); //Draw Feature Image <Bgr, byte> drawKeyPointImg = SystemToolBox.DrawSURFFeature(surfData); new ImageViewer(drawKeyPointImg, "擷取特徵點結果").Show(); } }
public void Execute(Arguments arguments) { LoggedConsole.WriteLine("feature learning process..."); var inputDir = @"D:\Mahnoosh\Liz\Least_Bittern\"; var inputPath = Path.Combine(inputDir, "TrainSet\\one_min_recordings"); var trainSetPath = Path.Combine(inputDir, "TrainSet\\train_data"); // var testSetPath = Path.Combine(inputDir, "TestSet"); var configPath = @"D:\Mahnoosh\Liz\Least_Bittern\FeatureLearningConfig.yml"; var resultDir = Path.Combine(inputDir, "FeatureLearning"); Directory.CreateDirectory(resultDir); // var outputMelImagePath = Path.Combine(resultDir, "MelScaleSpectrogram.png"); // var outputNormMelImagePath = Path.Combine(resultDir, "NormalizedMelScaleSpectrogram.png"); // var outputNoiseReducedMelImagePath = Path.Combine(resultDir, "NoiseReducedMelSpectrogram.png"); // var outputReSpecImagePath = Path.Combine(resultDir, "ReconstrcutedSpectrogram.png"); // var outputClusterImagePath = Path.Combine(resultDir, "Clusters.bmp"); // +++++++++++++++++++++++++++++++++++++++++++++++++patch sampling from 1-min recordings var configFile = configPath.ToFileInfo(); if (configFile == null) { throw new FileNotFoundException("No config file argument provided"); } else if (!configFile.Exists) { throw new ArgumentException($"Config file {configFile.FullName} not found"); } var configuration = ConfigFile.Deserialize <FeatureLearningSettings>(configFile); int patchWidth = (configuration.MaxFreqBin - configuration.MinFreqBin + 1) / configuration.NumFreqBand; var clusteringOutputList = FeatureLearning.UnsupervisedFeatureLearning(configuration, inputPath); List <double[][]> allBandsCentroids = new List <double[][]>(); for (int i = 0; i < clusteringOutputList.Count; i++) { var clusteringOutput = clusteringOutputList[i]; // writing centroids to a csv file // note that Csv.WriteToCsv can't write data types like dictionary<int, double[]> (problems with arrays) // I converted the dictionary values to a matrix and used the Csv.WriteMatrixToCsv // it might be a better way to do this string pathToClusterCsvFile = Path.Combine(resultDir, "ClusterCentroids" + i.ToString() + ".csv"); var clusterCentroids = clusteringOutput.ClusterIdCentroid.Values.ToArray(); Csv.WriteMatrixToCsv(pathToClusterCsvFile.ToFileInfo(), clusterCentroids.ToMatrix()); // sorting clusters based on size and output it to a csv file Dictionary <int, double> clusterIdSize = clusteringOutput.ClusterIdSize; int[] sortOrder = KmeansClustering.SortClustersBasedOnSize(clusterIdSize); // Write cluster ID and size to a CSV file string pathToClusterSizeCsvFile = Path.Combine(resultDir, "ClusterSize" + i.ToString() + ".csv"); Csv.WriteToCsv(pathToClusterSizeCsvFile.ToFileInfo(), clusterIdSize); // Draw cluster image directly from clustering output List <KeyValuePair <int, double[]> > list = clusteringOutput.ClusterIdCentroid.ToList(); double[][] centroids = new double[list.Count][]; for (int j = 0; j < list.Count; j++) { centroids[j] = list[j].Value; } allBandsCentroids.Add(centroids); List <double[, ]> allCentroids = new List <double[, ]>(); for (int k = 0; k < centroids.Length; k++) { // convert each centroid to a matrix in order of cluster ID // double[,] cent = PatchSampling.ArrayToMatrixByColumn(centroids[i], patchWidth, patchHeight); // OR: in order of cluster size double[,] cent = MatrixTools.ArrayToMatrixByColumn(centroids[sortOrder[k]], patchWidth, configuration.PatchHeight); // normalize each centroid double[,] normCent = DataTools.normalise(cent); // add a row of zero to each centroid double[,] cent2 = PatchSampling.AddRow(normCent); allCentroids.Add(cent2); } // concatenate all centroids double[,] mergedCentroidMatrix = PatchSampling.ListOf2DArrayToOne2DArray(allCentroids); // Draw clusters var clusterImage = ImageTools.DrawMatrixWithoutNormalisation(mergedCentroidMatrix); clusterImage.RotateFlip(RotateFlipType.Rotate270FlipNone); var outputClusteringImage = Path.Combine(resultDir, "ClustersWithGrid" + i.ToString() + ".bmp"); clusterImage.Save(outputClusteringImage); } // extracting features FeatureExtraction.UnsupervisedFeatureExtraction(configuration, allBandsCentroids, trainSetPath, resultDir); LoggedConsole.WriteLine("Done..."); }