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)); }
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); }
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"); }
/// <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); }
/// <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 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])); } } }
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()); }
/// <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])); } } }
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 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 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 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)); }
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 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); 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; }
/// <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, new Hamming()); 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 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])); } } }
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)); }