public override int ProcessImages(List <ImageData> images, out double[][] inputs, out int[] outputs) { outputs = new int[images.Count]; inputs = new double[images.Count][]; var imageData = new List <Bitmap>(); foreach (var image in images) { using (var stream = new MemoryStream(image.Image)) { imageData.Add(new Bitmap(stream)); } } var converter = new CornerFeaturesDetector(_detector); _bagOfVisualWords = new BagOfVisualWords <CornerFeaturePoint>(converter, NumberOfWords); _bagOfVisualWords.Compute(imageData.ToArray()); for (var i = 0; i < images.Count; i++) { inputs[i] = _bagOfVisualWords.GetFeatureVector(imageData[i]); outputs[i] = Utilities.GetIntFromClass(images[i].Type); } return(images.Count); }
public void BagOfVisualWordsConstructorTest() { BagOfVisualWords bow = new BagOfVisualWords(10); var points = bow.Compute(images, 1e-3); Assert.AreEqual(10, bow.NumberOfWords); Assert.AreEqual(6, points.Length); Assert.AreEqual(406, points[0].Count); Assert.AreEqual(727, points[1].Count); Assert.AreEqual(549, points[2].Count); Assert.AreEqual(458, points[3].Count); Assert.AreEqual(723, points[4].Count); Assert.AreEqual(1263, points[5].Count); Assert.AreEqual(388.043776954555, points[0][0].X); Assert.AreEqual(105.99327164889745, points[0][0].Y); Assert.AreEqual(335.64548481033881, points[3][7].X); Assert.AreEqual(152.14505651866821, points[2][3].Y); Assert.AreEqual(573.691355494602, points[2][52].X); Assert.AreEqual(153.6650841848263, points[1][11].Y); Assert.AreEqual(573.03087205188058, points[0][42].X); Assert.AreEqual(374.27580307739436, points[4][125].Y); }
/// <summary> /// Initialise a Visual Bag of Words Model /// </summary> /// <returns></returns> public static BagOfVisualWords <Accord.IFeatureDescriptor <double[]>, double[], Accord.MachineLearning.KMeans, Accord.Imaging.FastRetinaKeypointDetector> CreateBowModel() { if (Image.targetBitmap == null) { System.Drawing.Image imageTarget = System.Drawing.Image.FromFile(@"C:\Users\antmi\Pictures\bark1.jpg"); Size size = new Size(200, 200); Image.targetBitmap = new Bitmap(imageTarget, size); } var bow = BagOfVisualWords.Create(new FastRetinaKeypointDetector(), numberOfWords: 10); Bitmap[] images = new Bitmap[1] { Image.targetBitmap }; bow.Learn(images); if (Image.targetBitmapScaled == null) { System.Drawing.Image imageTarget = System.Drawing.Image.FromFile(@"C:\Users\antmi\Pictures\bark1.jpg"); Size size = new Size(100, 100); Image.targetBitmapScaled = new Bitmap(imageTarget, size); Image.scaledFeatureCount = bow.Transform(new Bitmap[1] { Image.targetBitmapScaled })[0].Sum(); } return(bow); }
public static void createBoW() { string[] files = Directory.GetFiles ("testset"); Dictionary<string, Bitmap> testImages = new Dictionary<string, Bitmap>(); for (int i = 0; i < files.Length; i++) { string name = Path.GetFileNameWithoutExtension (files [i]); testImages.Add(name, (Bitmap)Bitmap.FromFile(files[i])); } int numberOfWords = 6; // number of cluster centers: typically >>100 // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); // Create bag-of-words (BoW) with the given algorithm BagOfVisualWords surfBow = new BagOfVisualWords(binarySplit); // Compute the BoW codebook using training images only Bitmap[] bmps = new Bitmap[testImages.Count]; testImages.Values.CopyTo(bmps, 0); surfBow.Compute(bmps); surfBow.Save ("bagOfWords"); }
public void SerializeTest() { var images = BagOfVisualWordsTest.images.DeepClone(); Accord.Math.Tools.SetupGenerator(0); BagOfVisualWords bow = new BagOfVisualWords(10); bow.Compute(images); double[][] expected = new double[images.Length][]; for (int i = 0; i < expected.Length; i++) { expected[i] = bow.GetFeatureVector(images[i]); } MemoryStream stream = new MemoryStream(); BinaryFormatter fmt = new BinaryFormatter(); fmt.Serialize(stream, bow); stream.Seek(0, SeekOrigin.Begin); bow = (BagOfVisualWords)fmt.Deserialize(stream); double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } Assert.IsTrue(expected.IsEqual(actual)); }
private IBagOfWords <Bitmap> CreateBow() { var binarySplit = new BinarySplit(10); var surfBow = BagOfVisualWords.Create(10); return(surfBow.Learn(Images.ToArray())); }
private static void CreateBoW() { var numberOfWords = 36; foreach (var file in Directory.EnumerateFiles(@"C:\Temp\TLLCamerasTestData\37_Training", "*.jpg")) { var trainingImage = (Bitmap)Bitmap.FromFile(file); trainingImages.Add(file, trainingImage); } foreach (var file in Directory.EnumerateFiles(@"C:\Temp\TLLCamerasTestData\37_Testing", "*.jpg")) { var testImage = (Bitmap)Bitmap.FromFile(file); testingImages.Add(file, testImage); } // We will use SURF, so we can use a standard clustering // algorithm that is based on Euclidean distances. A good // algorithm for clustering codewords is the Binary Split // variant of the K-Means algorithm. // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); // Create bag-of-words (BoW) with the given algorithm BagOfVisualWords surfBow = new BagOfVisualWords(binarySplit); // Compute the BoW codebook using training images only IBagOfWords <Bitmap> bow = surfBow.Learn(trainingImages.Values.ToArray()); // now that we've created the bow we need to use it to create a representation of each training and test image foreach (var trainingImage in trainingImages.Keys) { var asBitmap = trainingImages[trainingImage] as Bitmap; var featureVector = (bow as ITransform <Bitmap, double[]>).Transform(asBitmap); var featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); trainingFeatures.Add(trainingImage, featureVector); } foreach (var testingImage in testingImages.Keys) { var asBitmap = testingImages[testingImage] as Bitmap; var featureVector = (bow as ITransform <Bitmap, double[]>).Transform(asBitmap); var featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); testingFeatures.Add(testingImage, featureVector); } }
private void bowsToolStripMenuItem_Click_1(object sender, EventArgs e) { var bow = BagOfVisualWords.Create(numberOfWords: bowSize); var images = trainData.GetBitmaps(mask); bow.Learn(images); Accord.IO.Serializer.Save(bow, dataPath + String.Format(@"\train-{0}.bow", bowSize)); logger.logStr("Done " + bowSize); Application.DoEvents(); }
public void CalculateSampleFeatures(BagOfVisualWords bow) { List<double[]> features = new List<double[]>(); foreach(var img in Images) { var vector = bow.GetFeatureVector(img.BitmapImage); features.Add(vector); } SampleFeatures = features.ToArray(); }
/// <summary> /// This methods computes the Bag-of-Visual-Words with the training images. /// </summary> /// private void btnBagOfWords_Click(object sender, EventArgs e) { int numberOfWords = (int)numWords.Value; // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); // Create bag-of-words (BoW) with the given algorithm BagOfVisualWords bow = new BagOfVisualWords(binarySplit); if (cbExtended.Checked) { bow.Detector.ComputeDescriptors = SpeededUpRobustFeatureDescriptorType.Extended; } Stopwatch sw1 = Stopwatch.StartNew(); // Compute the BoW codebook using training images only var points = bow.Compute(originalTrainImages.Values.ToArray()); sw1.Stop(); Stopwatch sw2 = Stopwatch.StartNew(); // Extract features for all images foreach (ListViewItem item in listView1.Items) { // Get item image Bitmap image = originalImages[item.ImageKey] as Bitmap; // Process image double[] featureVector = bow.GetFeatureVector(image); string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); if (item.SubItems.Count == 2) { item.SubItems[1].Text = featureString; } else { item.SubItems.Add(featureString); } int classLabel = (item.Tag as Tuple <double[], int>).Item2; item.Tag = Tuple.Create(featureVector, classLabel); } sw2.Stop(); lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s."; btnSampleRunAnalysis.Enabled = true; }
/// <summary> /// Computes the Bag of Words model. /// </summary> /// <typeparam name="TPoint"> /// The <see cref="Accord.Imaging.IFeaturePoint{TFeature}"/> type to be used with this class, /// such as <see cref="Accord.Imaging.SpeededUpRobustFeaturePoint"/>. /// </typeparam> /// <typeparam name="TFeature"> /// The feature type of the <typeparamref name="TPoint"/>, such /// as <see cref="T:double[]"/>. /// </typeparam> /// <param name="bow">Bag of Visual Words.</param> /// <param name="images">The set of images to initialize the model.</param> /// <param name="threshold">Convergence rate for the k-means algorithm. Default is 1e-5.</param> /// <returns>The list of feature points detected in all images.</returns> public static List <TPoint>[] Compute <TPoint, TFeature>(this BagOfVisualWords <TPoint, TFeature> bow, Image <Gray, byte>[] images, double threshold = 1e-5) where TPoint : IFeatureDescriptor <TFeature> { var featurePoints = bow.Compute ( images.Select(x => x.ToBitmap(copyAlways: false, failIfCannotCast: true)).ToArray(), threshold ); return(featurePoints); }
public bool TrainSceneFeature(SceneFeatureData scenefeatureData) { //Create bow Bitmap mask = Utils.CreateMaskBitmap(new Size(1280, 720), new Rectangle[] { scenefeatureData.feature.area }); var bow = BagOfVisualWords.Create(numberOfWords: scenefeatureData.feature.bowSize); var images = scenefeatureData.trainData.GetBitmaps(mask); bow.Learn(images); Accord.IO.Serializer.Save(bow, path + @"\" + scenefeatureData.feature.name + String.Format(@"\train-{0}.bow", scenefeatureData.feature.bowSize)); bow.Show(); return(Train(bow, scenefeatureData)); }
public void GetFeatureVectorTest() { var images = GetImages(); Accord.Math.Random.Generator.Seed = 0; Bitmap image = new Bitmap(images[0]); // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); bow.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(image); Assert.AreEqual(10, feature.Length); double[][] expected = { new double[] { 47, 44, 42, 4, 23, 22, 28, 53, 50, 96 }, new double[] { 26, 91, 71, 49, 99, 70, 59, 28, 155, 79 }, new double[] { 71, 34, 51, 33, 53, 25, 44, 64, 32, 145 } }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } //string str = actual.ToString(CSharpJaggedMatrixFormatProvider.InvariantCulture); //Assert.IsNullOrEmpty(str); for (int i = 0; i < actual.Length; i++) { for (int j = 0; j < actual[i].Length; j++) { Assert.IsTrue(expected[i].Contains(actual[i][j])); } } }
public static double[][] GetSURFFeatures(Bitmap[] images, int maxFeatures, string modelSaveLocation = null) { Accord.Math.Random.Generator.Seed = 0; var bow = BagOfVisualWords.Create(numberOfWords: maxFeatures); bow.Learn(images); if (!string.IsNullOrEmpty(modelSaveLocation)) { Serializer.Save(obj: bow, path: modelSaveLocation); } double[][] features = bow.Transform(images); return(features); }
static void ImageFeatures() { Dictionary <string, Bitmap> testImages = new Dictionary <string, Bitmap>(); testImages.Add("img_acropolis", (Bitmap)Bitmap.FromFile("test_imgs/acropolis_athens.jpg")); testImages.Add("img_cathedral", (Bitmap)Bitmap.FromFile("test_imgs/amiens_cathedral.jpg")); testImages.Add("img_bigben", (Bitmap)Bitmap.FromFile("test_imgs/big_ben.jpg")); int numberOfWords = 6; // number of cluster centers: typically >>100 // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); IBagOfWords <Bitmap> bow; // Create bag-of-words ( BoW ) with the given algorithm BagOfVisualWords surfBow = new BagOfVisualWords(binarySplit); // Compute the BoW codebook using training images only Bitmap[] bmps = new Bitmap[testImages.Count]; testImages.Values.CopyTo(bmps, 0); surfBow.Compute(bmps); bow = surfBow; // this model needs to be saved once it is calculated: only compute it once to calculate features // from the collection as well as for new queries. // THE SAME TRAINED MODEL MUST BE USED TO GET THE SAME FEATURES!!! Dictionary <string, double[]> testImageFeatures = new Dictionary <string, double[]>(); // Extract features for all images foreach (string imagename in testImages.Keys) { double[] featureVector = bow.GetFeatureVector(testImages[imagename]); testImageFeatures.Add(imagename, featureVector); Console.Out.WriteLine(imagename + " features: " + featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture)); } // Calculate Image Similarities string[] imagenames = new string[testImageFeatures.Keys.Count]; testImageFeatures.Keys.CopyTo(imagenames, 0); for (int i = 0; i < imagenames.Length; i++) { for (int j = i + 1; j < imagenames.Length; j++) { double dist = Distance.Cosine(testImageFeatures[imagenames[i]], testImageFeatures[imagenames[j]]); Console.Out.WriteLine(imagenames[i] + " <-> " + imagenames[j] + " distance: " + dist.ToString()); } } }
public void GetFeatureVectorTest() { var images = GetImages(); Accord.Math.Random.Generator.Seed = 0; // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); bow.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(images[0]); Assert.AreEqual(10, feature.Length); double[][] expected = new double[][] { new double[] { 4, 28, 24, 68, 51, 97, 60, 35, 18, 24 }, new double[] { 53, 111, 89, 70, 24, 80, 130, 46, 50, 74 }, new double[] { 31, 29, 57, 102, 63, 142, 40, 18, 37, 33 } }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } string str = actual.ToCSharp(); for (int i = 0; i < actual.Length; i++) { for (int j = 0; j < actual[i].Length; j++) { Assert.IsTrue(expected[i].Contains(actual[i][j])); } } }
public void LargeTest() { // Requires data from the National Data Science bowl // https://github.com/accord-net/framework/issues/58 var trainingDirectory = @"C:\Users\CésarRoberto\Downloads\train\train\"; var images = LabeledImages.FromDirectory(trainingDirectory).ToArray(); var binarySplit = new BinarySplit(32); var bow = new BagOfVisualWords(binarySplit); bow.Compute(images.Select(i => i.Item).Take(50).ToArray()); }
private void loadBow(object sender, RoutedEventArgs e) { imageInfo.Text = "Loading BoW"; Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog(); op.Title = "Select a BoW file."; if (op.ShowDialog() == true) { bow = Accord.IO.Serializer.Load <BagOfVisualWords>(op.FileName); } else { MessageBox.Show("Something went wrong while loadin BoW!"); } imageInfo.Text = "Done"; }
/// <summary> /// Computes the Bag of Words model. /// </summary> /// <typeparam name="TPoint"> /// The <see cref="Accord.Imaging.IFeaturePoint{TFeature}"/> type to be used with this class, /// such as <see cref="Accord.Imaging.SpeededUpRobustFeaturePoint"/>. /// </typeparam> /// <typeparam name="TFeature"> /// The feature type of the <typeparamref name="TPoint"/>, such /// as <see cref="T:double[]"/>. /// </typeparam> /// <param name="bow">Bag of Visual Words.</param> /// <param name="images">The set of images to initialize the model.</param> /// <param name="threshold">Convergence rate for the k-means algorithm. Default is 1e-5.</param> /// <returns>The list of feature points detected in all images.</returns> public static List <TPoint>[] Compute <TPoint, TFeature>(this BagOfVisualWords <TPoint, TFeature> bow, Gray <byte>[][,] images, double threshold = 1e-5) where TPoint : IFeatureDescriptor <TFeature> { var uImages = images.Select(x => x.Lock()); var featurePoints = bow.Compute ( uImages.Select(x => x.AsBitmap()).ToArray(), threshold ); uImages.ForEach(x => x.Dispose()); return(featurePoints); }
public void GetFeatureVectorTest() { Accord.Math.Tools.SetupGenerator(0); Bitmap image = (Bitmap)images[0].Clone(); // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(image); Assert.AreEqual(10, feature.Length); double[][] expected = { new double[] { 43, 39, 27, 21, 34, 30, 98, 51, 61, 5 }, new double[] { 69, 100, 61, 41, 114, 125, 85, 27, 34, 71 }, new double[] { 52, 46, 44, 19, 20, 51, 143, 73, 69, 35 } }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } // string str = actual.ToString(CSharpJaggedMatrixFormatProvider.InvariantCulture); for (int i = 0; i < actual.Length; i++) { for (int j = 0; j < actual[i].Length; j++) { Assert.IsTrue(expected[i].Contains(actual[i][j])); } } }
public void GetFeatureVectorTest() { Accord.Math.Tools.SetupGenerator(0); Bitmap image = images[0]; // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(image); Assert.AreEqual(10, feature.Length); double[][] expected = { new double[] { 102, 58, 42, 24, 47, 53, 29, 26, 3, 22 }, new double[] { 90, 135, 71, 101, 26, 28, 105, 61, 62, 48 }, new double[] { 138, 36, 55, 56, 71, 61, 30, 47, 33, 22 } }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } // string str = actual.ToString(CSharpJaggedMatrixFormatProvider.InvariantCulture); for (int i = 0; i < actual.Length; i++) { for (int j = 0; j < actual[i].Length; j++) { Assert.IsTrue(expected[i].Contains(actual[i][j])); } } }
/// <summary> /// Method use for deserialize bcf from file /// </summary> /// <returns>deserialized object</returns> internal static BagOfVisualWords ReadBinary() { //Create binary formatter BinaryFormatter formatter = new BinaryFormatter(); //Open stream for read FileStream fs = new FileStream(Path.Combine(Application.StartupPath, "Resources/" + "bcf.dat"), FileMode.OpenOrCreate); //Deserialize data from strim and convert it in bcf BagOfVisualWords freak = (BagOfVisualWords)formatter.Deserialize(fs); //Close stream fs.Close(); return(freak); }
public void BagOfVisualWordsConstructorTest() { Accord.Math.Random.Generator.Seed = 0; var images = GetImages(); BagOfVisualWords bow = new BagOfVisualWords(10); bow.ParallelOptions.MaxDegreeOfParallelism = 1; #pragma warning disable 612, 618 var points = bow.Compute(images, 1e-3); #pragma warning restore 612, 618 Assert.AreEqual(10, bow.NumberOfWords); Assert.AreEqual(6, points.Length); Assert.AreEqual(409, points[0].Count); Assert.AreEqual(727, points[1].Count); Assert.AreEqual(552, points[2].Count); Assert.AreEqual(460, points[3].Count); Assert.AreEqual(719, points[4].Count); Assert.AreEqual(1265, points[5].Count); double tol = 1e-5; Assert.AreEqual(388.04225639880224, points[0][0].X, tol); Assert.AreEqual(105.9954439039073, points[0][0].Y, tol); Assert.AreEqual(335.62395561144433, points[3][7].X, tol); Assert.AreEqual(152.14505651866821, points[2][3].Y, tol); Assert.AreEqual(573.691355494602, points[2][52].X, tol); Assert.AreEqual(153.6650841848263, points[1][11].Y, tol); Assert.AreEqual(289.54728415724327, points[0][42].X, tol); Assert.AreEqual(373.99402540151056, points[4][125].Y, tol); foreach (var point in points) { foreach (var p in point) { Assert.IsFalse(double.IsNaN(p.X)); Assert.IsFalse(double.IsNaN(p.Y)); } } }
public void BagOfVisualWordsConstructorTest3() { Accord.Math.Random.Generator.Seed = 0; var images = GetImages(); MoravecCornersDetector moravec = new MoravecCornersDetector(); CornerFeaturesDetector detector = new CornerFeaturesDetector(moravec); var bow = new BagOfVisualWords <CornerFeaturePoint>(detector, numberOfWords: 10); bow.ParallelOptions.MaxDegreeOfParallelism = 1; #pragma warning disable 612, 618 var points = bow.Compute(images, 1e-3); #pragma warning restore 612, 618 double[] vector = bow.GetFeatureVector(images[0]); Assert.AreEqual(10, bow.NumberOfWords); Assert.AreEqual(6, points.Length); Assert.AreEqual(10, vector.Length); Assert.AreEqual(2800, points[0].Count); Assert.AreEqual(4532, points[1].Count); Assert.AreEqual(2282, points[2].Count); Assert.AreEqual(1173, points[3].Count); Assert.AreEqual(4860, points[4].Count); Assert.AreEqual(5730, points[5].Count); Assert.AreEqual(596, points[0][0].Descriptor[0]); Assert.AreEqual(51, points[0][0].Descriptor[1]); Assert.AreEqual(points[0][0].Descriptor[0], points[0][0].X); Assert.AreEqual(points[0][0].Descriptor[1], points[0][0].Y); Assert.AreEqual(461, points[3][7].Descriptor[0]); Assert.AreEqual(8, points[2][3].Descriptor[1]); Assert.AreEqual(991, points[2][52].Descriptor[0]); Assert.AreEqual(82, points[1][11].Descriptor[1]); Assert.AreEqual(430, points[0][42].Descriptor[0]); Assert.AreEqual(135, points[4][125].Descriptor[1]); }
public void GetFeatureVectorTest() { Accord.Math.Tools.SetupGenerator(0); Bitmap image = images[0]; // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(image); Assert.AreEqual(10, feature.Length); double[][] expected = { new double[] { 26, 58, 102, 24, 53, 22, 47, 29, 42, 3 }, new double[] { 61, 135, 90, 101, 28, 48, 26, 105, 71, 62 }, new double[] { 47, 36, 138, 56, 61, 22, 71, 30, 55, 33 }, }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } Assert.IsTrue(expected.IsEqual(actual)); }
/// <summary> /// Main constructor for windows form /// </summary> public MainWindow() { InitializeComponent(); //Add properties for openFileDialog openFileDialog.DefaultExt = ".tif"; openFileDialog.Title = "Open Image"; openFileDialog.Filter = "JPEG files (*.jpg)| *.jpg|TIFF files (*.tif)| *.tif|" + " All files | *.*"; //Close button Yes and No, which user click for assert recognition labelCorrect.Visible = false; buttonCorrectYes.Visible = false; buttonCorrectYes.Visible = false; this.BackColor = Color.White; //Read ready SVM and bagOfContourFragments multiSVM = BinarySave.ReadBinary(true); bagOfContourFragments = BinarySave.ReadBinary(); }
/* public static Bitmap[] GetBitmaps(this ImageFileData[] _self) * { * List<Bitmap> ret = new List<Bitmap>(); * foreach (var a in _self) * { * ret.Add(a.image); * } * return ret.ToArray(); * }*/ public static void Show(this BagOfVisualWords bow) { // We can also check some statistics about the dataset: int numberOfImages = bow.Statistics.TotalNumberOfInstances; // 6 // Statistics about all the descriptors that have been extracted: int totalDescriptors = bow.Statistics.TotalNumberOfDescriptors; // 4132 double totalMean = bow.Statistics.TotalNumberOfDescriptorsPerInstance.Mean; // 688.66666666666663 double totalVar = bow.Statistics.TotalNumberOfDescriptorsPerInstance.Variance; // 96745.866666666669 IntRange totalRange = bow.Statistics.TotalNumberOfDescriptorsPerInstanceRange; // [409, 1265] Console.WriteLine(String.Format("{0} {1} {2} {3}", totalDescriptors, totalMean, totalVar, totalRange.ToString())); // Statistics only about the descriptors that have been actually used: int takenDescriptors = bow.Statistics.NumberOfDescriptorsTaken; // 4132 double takenMean = bow.Statistics.NumberOfDescriptorsTakenPerInstance.Mean; // 688.66666666666663 double takenVar = bow.Statistics.NumberOfDescriptorsTakenPerInstance.Variance; // 96745.866666666669 IntRange takenRange = bow.Statistics.NumberOfDescriptorsTakenPerInstanceRange; // [409, 1265] Console.WriteLine(String.Format("{0} {1} {2} {3}", takenDescriptors, takenMean, takenVar, takenRange.ToString())); }
public void SerializeTest2() { var images = GetImages(); Accord.Math.Random.Generator.Seed = 0; FastCornersDetector fast = new FastCornersDetector(); FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector(fast); var kmodes = new KModes <byte>(5, new Hamming()); kmodes.ParallelOptions.MaxDegreeOfParallelism = 1; var bow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(freak, kmodes); bow.Compute(images); double[][] expected = new double[images.Length][]; for (int i = 0; i < expected.Length; i++) { expected[i] = bow.GetFeatureVector(images[i]); } MemoryStream stream = new MemoryStream(); BinaryFormatter fmt = new BinaryFormatter(); fmt.Serialize(stream, bow); stream.Seek(0, SeekOrigin.Begin); bow = (BagOfVisualWords <FastRetinaKeypoint, byte[]>)fmt.Deserialize(stream); double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } Assert.IsTrue(expected.IsEqual(actual)); }
public void BagOfVisualWordsConstructorTest() { BagOfVisualWords bow = new BagOfVisualWords(10); var points = bow.Compute(images, 1e-3); Assert.AreEqual(10, bow.NumberOfWords); Assert.AreEqual(6, points.Length); Assert.AreEqual(409, points[0].Count); Assert.AreEqual(727, points[1].Count); Assert.AreEqual(552, points[2].Count); Assert.AreEqual(460, points[3].Count); Assert.AreEqual(719, points[4].Count); Assert.AreEqual(1265, points[5].Count); double tol = 1e-7; Assert.AreEqual(388.04225639880224, points[0][0].X, tol); Assert.AreEqual(105.9954439039073, points[0][0].Y, tol); Assert.AreEqual(335.62395561144433, points[3][7].X, tol); Assert.AreEqual(152.14505651866821, points[2][3].Y, tol); Assert.AreEqual(573.691355494602, points[2][52].X, tol); Assert.AreEqual(153.6650841848263, points[1][11].Y, tol); Assert.AreEqual(289.54728415724327, points[0][42].X, tol); Assert.AreEqual(373.99402540151056, points[4][125].Y, tol); foreach (var point in points) { foreach (var p in point) { Assert.IsFalse(double.IsNaN(p.X)); Assert.IsFalse(double.IsNaN(p.Y)); } } }
public void SerializeTest2() { var images = BagOfVisualWordsTest.images.DeepClone(); Accord.Math.Tools.SetupGenerator(0); FastCornersDetector fast = new FastCornersDetector(); FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector(fast); var kmodes = new KModes <byte>(5, Distance.BitwiseHamming); var bow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(freak, kmodes); bow.Compute(images); double[][] expected = new double[images.Length][]; for (int i = 0; i < expected.Length; i++) { expected[i] = bow.GetFeatureVector(images[i]); } MemoryStream stream = new MemoryStream(); BinaryFormatter fmt = new BinaryFormatter(); fmt.Serialize(stream, bow); stream.Seek(0, SeekOrigin.Begin); bow = (BagOfVisualWords <FastRetinaKeypoint, byte[]>)fmt.Deserialize(stream); double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) { actual[i] = bow.GetFeatureVector(images[i]); } Assert.IsTrue(expected.IsEqual(actual)); }
private void trainBoW(object sender, RoutedEventArgs e) { //train bow imageInfo.Text = "Training BoW"; WPFFolderBrowserDialog dd = new WPFFolderBrowserDialog(); dd.Title = "Select a folder"; if (dd.ShowDialog() == true) { String[] names = UtilFn.GetFilesFrom(dd.FileName, filtersDir, false); if (names.Length == 0) { MessageBox.Show("NO IMAGES IN SELECTED FOLDER."); } ushort c = 0; List <Bitmap> ims = new List <Bitmap>(); foreach (String fn in names) { if (c++ == 2000) { break; } ims.Add(new Bitmap(fn)); } bow = new BagOfVisualWords(20); // br features bow.Learn(ims.ToArray()); } else { MessageBox.Show("Something went wrong."); } imageInfo.Text = "Done"; }
public void BagOfVisualWordsConstructorTest3() { MoravecCornersDetector moravec = new MoravecCornersDetector(); CornerFeaturesDetector detector = new CornerFeaturesDetector(moravec); var bow = new BagOfVisualWords <CornerFeaturePoint>(detector, numberOfWords: 10); var points = bow.Compute(images, 1e-3); double[] vector = bow.GetFeatureVector(images[0]); Assert.AreEqual(10, bow.NumberOfWords); Assert.AreEqual(6, points.Length); Assert.AreEqual(10, vector.Length); Assert.AreEqual(2800, points[0].Count); Assert.AreEqual(4532, points[1].Count); Assert.AreEqual(2282, points[2].Count); Assert.AreEqual(1173, points[3].Count); Assert.AreEqual(4860, points[4].Count); Assert.AreEqual(5730, points[5].Count); Assert.AreEqual(596, points[0][0].Descriptor[0]); Assert.AreEqual(51, points[0][0].Descriptor[1]); Assert.AreEqual(points[0][0].Descriptor[0], points[0][0].X); Assert.AreEqual(points[0][0].Descriptor[1], points[0][0].Y); Assert.AreEqual(461, points[3][7].Descriptor[0]); Assert.AreEqual(8, points[2][3].Descriptor[1]); Assert.AreEqual(991, points[2][52].Descriptor[0]); Assert.AreEqual(82, points[1][11].Descriptor[1]); Assert.AreEqual(430, points[0][42].Descriptor[0]); Assert.AreEqual(135, points[4][125].Descriptor[1]); }
public void GetFeatureVectorTest() { Accord.Math.Tools.SetupGenerator(0); Bitmap image = images[0]; // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(image); Assert.AreEqual(10, feature.Length); double[][] expected = { new double[] { 102, 58, 42, 24, 47, 53, 29, 26, 3, 22 }, new double[] { 90, 135, 71, 101, 26, 28, 105, 61, 62, 48 }, new double[] { 138, 36, 55, 56, 71, 61, 30, 47, 33, 22 } }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) actual[i] = bow.GetFeatureVector(images[i]); // string str = actual.ToString(CSharpJaggedMatrixFormatProvider.InvariantCulture); for (int i = 0; i < actual.Length; i++) { for (int j = 0; j < actual[i].Length; j++) { Assert.IsTrue(expected[i].Contains(actual[i][j])); } } }
/// <summary> /// This methods computes the Bag-of-Visual-Words with the training images. /// </summary> /// private void btnBagOfWords_Click(object sender, EventArgs e) { int numberOfWords = (int)numWords.Value; // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); Stopwatch sw1 = Stopwatch.StartNew(); IBagOfWords<Bitmap> bow; if (rbSurf.Checked) { // Create bag-of-words (BoW) with the given algorithm var surfBow = new BagOfVisualWords(binarySplit); // Compute the BoW codebook using training images only surfBow.Compute(originalTrainImages.Values.ToArray()); bow = surfBow; } else { // Alternative creation using the FREAK detector // Create a Binary-Split clustering algorithm var kmodes = new KModes<byte>(numberOfWords, Distance.BitwiseHamming); var detector = new FastRetinaKeypointDetector(); // Create bag-of-words (BoW) with the given algorithm var freakBow = new BagOfVisualWords<FastRetinaKeypoint, byte[]>(detector, kmodes); // Compute the BoW codebook using training images only freakBow.Compute(originalTrainImages.Values.ToArray()); bow = freakBow; } sw1.Stop(); Stopwatch sw2 = Stopwatch.StartNew(); // Extract features for all images foreach (ListViewItem item in listView1.Items) { // Get item image Bitmap image = originalImages[item.ImageKey] as Bitmap; // Process image double[] featureVector = bow.GetFeatureVector(image); string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); if (item.SubItems.Count == 2) item.SubItems[1].Text = featureString; else item.SubItems.Add(featureString); int classLabel = (item.Tag as Tuple<double[], int>).Item2; item.Tag = Tuple.Create(featureVector, classLabel); } sw2.Stop(); lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s."; btnSampleRunAnalysis.Enabled = true; }
public void SerializeTest() { Accord.Math.Tools.SetupGenerator(0); BagOfVisualWords bow = new BagOfVisualWords(10); bow.Compute(images); double[][] expected = new double[images.Length][]; for (int i = 0; i < expected.Length; i++) expected[i] = bow.GetFeatureVector(images[i]); MemoryStream stream = new MemoryStream(); BinaryFormatter fmt = new BinaryFormatter(); fmt.Serialize(stream, bow); stream.Seek(0, SeekOrigin.Begin); bow = (BagOfVisualWords)fmt.Deserialize(stream); double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) actual[i] = bow.GetFeatureVector(images[i]); Assert.IsTrue(expected.IsEqual(actual)); }
public void GetFeatureVectorTest() { Accord.Math.Tools.SetupGenerator(0); Bitmap image = images[0]; // The Bag-of-Visual-Words model converts arbitrary-size images // into fixed-length feature vectors. In this example, we will // be setting the codebook size to 10. This means all generated // feature vectors will have the same length of 10. // Create a new Bag-of-Visual-Words (BoW) model BagOfVisualWords bow = new BagOfVisualWords(10); // Compute the model using // a set of training images bow.Compute(images); // After this point, we will be able to translate // images into double[] feature vectors using double[] feature = bow.GetFeatureVector(image); Assert.AreEqual(10, feature.Length); double[][] expected = { new double[] { 26, 58, 102, 24, 53, 22, 47, 29, 42, 3 }, new double[] { 61, 135, 90, 101, 28, 48, 26, 105, 71, 62 }, new double[] { 47, 36, 138, 56, 61, 22, 71, 30, 55, 33 }, }; double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) actual[i] = bow.GetFeatureVector(images[i]); Assert.IsTrue(expected.IsEqual(actual)); }
public void BagOfVisualWordsConstructorTest3() { MoravecCornersDetector moravec = new MoravecCornersDetector(); CornerFeaturesDetector detector = new CornerFeaturesDetector(moravec); var bow = new BagOfVisualWords<CornerFeaturePoint>(detector, numberOfWords: 10); var points = bow.Compute(images, 1e-3); double[] vector = bow.GetFeatureVector(images[0]); Assert.AreEqual(10, bow.NumberOfWords); Assert.AreEqual(6, points.Length); Assert.AreEqual(10, vector.Length); Assert.AreEqual(2800, points[0].Count); Assert.AreEqual(4532, points[1].Count); Assert.AreEqual(2282, points[2].Count); Assert.AreEqual(1173, points[3].Count); Assert.AreEqual(4860, points[4].Count); Assert.AreEqual(5730, points[5].Count); Assert.AreEqual(596, points[0][0].Descriptor[0]); Assert.AreEqual(51, points[0][0].Descriptor[1]); Assert.AreEqual(points[0][0].Descriptor[0], points[0][0].X); Assert.AreEqual(points[0][0].Descriptor[1], points[0][0].Y); Assert.AreEqual(461, points[3][7].Descriptor[0]); Assert.AreEqual(8, points[2][3].Descriptor[1]); Assert.AreEqual(991, points[2][52].Descriptor[0]); Assert.AreEqual(82, points[1][11].Descriptor[1]); Assert.AreEqual(430, points[0][42].Descriptor[0]); Assert.AreEqual(135, points[4][125].Descriptor[1]); }
/// <summary> /// This methods computes the Bag-of-Visual-Words with the training images. /// </summary> /// private void btnBagOfWords_Click(object sender, EventArgs e) { int numberOfWords = (int)numWords.Value; // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); // Create bag-of-words (BoW) with the given algorithm BagOfVisualWords bow = new BagOfVisualWords(binarySplit); if (cbExtended.Checked) bow.Detector.ComputeDescriptors = SpeededUpRobustFeatureDescriptorType.Extended; Stopwatch sw1 = Stopwatch.StartNew(); // Compute the BoW codebook using training images only var points = bow.Compute(originalTrainImages.Values.ToArray()); sw1.Stop(); Stopwatch sw2 = Stopwatch.StartNew(); // Extract features for all images foreach (ListViewItem item in listView1.Items) { // Get item image Bitmap image = originalImages[item.ImageKey] as Bitmap; // Process image double[] featureVector = bow.GetFeatureVector(image); string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); if (item.SubItems.Count == 2) item.SubItems[1].Text = featureString; else item.SubItems.Add(featureString); int classLabel = (item.Tag as Tuple<double[], int>).Item2; item.Tag = Tuple.Create(featureVector, classLabel); } sw2.Stop(); lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s."; btnSampleRunAnalysis.Enabled = true; }
public void SerializeTest2() { Accord.Math.Tools.SetupGenerator(0); FastCornersDetector fast = new FastCornersDetector(); FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector(fast); var kmodes = new KModes<byte[]>(5, Distance.BitwiseHamming); var bow = new BagOfVisualWords<FastRetinaKeypoint, byte[]>(freak, kmodes); bow.Compute(images); double[][] expected = new double[images.Length][]; for (int i = 0; i < expected.Length; i++) expected[i] = bow.GetFeatureVector(images[i]); MemoryStream stream = new MemoryStream(); BinaryFormatter fmt = new BinaryFormatter(); fmt.Serialize(stream, bow); stream.Seek(0, SeekOrigin.Begin); bow = (BagOfVisualWords<FastRetinaKeypoint, byte[]>)fmt.Deserialize(stream); double[][] actual = new double[expected.Length][]; for (int i = 0; i < actual.Length; i++) actual[i] = bow.GetFeatureVector(images[i]); Assert.IsTrue(expected.IsEqual(actual)); }
public void CalculateFeatureVector(BagOfVisualWords bow) { FeatureVector = bow.GetFeatureVector(_image.BitmapImage); }