private static void Main(string[] args) { try { // Make sure the user entered an argument to this program. It should be the // filename for an image. if (args.Length != 1) { Console.WriteLine("error, you have to enter a BMP file as an argument to this program."); return; } // Here we declare an image object that can store color rgb_pixels. // Now load the image file into our image. If something is wrong then // load_image() will throw an exception. Also, if you linked with libpng // and libjpeg then load_image() can load PNG and JPEG files in addition // to BMP files. using (var img = Dlib.LoadImage <RgbPixel>(args[0])) { // Now convert the image into a FHOG feature image. The output, hog, is a 2D array // of 31 dimensional vectors. using (var hog = Dlib.ExtracFHogFeatures <float>(img)) { Console.WriteLine($"hog image has {hog.Rows} rows and {hog.Columns} columns."); // Let's see what the image and FHOG features look like. using (var win = new ImageWindow(img)) using (var drawhog = Dlib.DrawHog(hog)) using (var winhog = new ImageWindow(drawhog)) { // Another thing you might want to do is map between the pixels in img and the // cells in the hog image. dlib provides the image_to_fhog() and fhog_to_image() // routines for this. Their use is demonstrated in the following loop which // responds to the user clicking on pixels in the image img. Point p; // A 2D point, used to represent pixel locations. while (win.GetNextDoubleClick(out p)) { var hp = Dlib.ImgaeToFHog(p); Console.WriteLine($"The point {p} in the input image corresponds to {hp} in hog space."); var row = hog[hp.Y]; var column = row[hp.X]; var t = Dlib.Trans(column); Console.WriteLine($"FHOG features at this point: {t}"); } // Finally, sometimes you want to get a planar representation of the HOG features // rather than the explicit vector (i.e. interlaced) representation used above. var planar_hog = Dlib.ExtracFHogFeaturesArray <float>(img); // Now we have an array of 31 float valued image planes, each representing one of // the dimensions of the HOG feature vector. } } } } catch (Exception e) { Console.WriteLine($"exception thrown: {e}"); } }
public void ExtracFHogFeatures2() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = MatrixElementTypes.Float, ExpectResult = true }, new { Type = MatrixElementTypes.Double, ExpectResult = true } }; foreach (var output in tests) { Array2DBase imageObj = null; Array2DMatrixBase outputObj = null; try { imageObj = DlibTest.LoadImage(ImageTypes.RgbPixel, path); switch (output.Type) { case MatrixElementTypes.Float: outputObj = Dlib.ExtracFHogFeatures <float>(imageObj); break; case MatrixElementTypes.Double: outputObj = Dlib.ExtracFHogFeatures <double>(imageObj); break; default: throw new ArgumentOutOfRangeException(); } MatrixBase matrix = Dlib.DrawHog(outputObj); if (!this.CanGuiDebug) { var window = new ImageWindow(matrix); window.WaitUntilClosed(); } } catch (Exception e) { Console.WriteLine(e); throw; } finally { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } if (outputObj != null) { this.DisposeAndCheckDisposedState(outputObj); } } } }
public void ExtracFHogFeatures() { const string testName = "ExtracFHogFeatures"; var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = MatrixElementTypes.Float, ExpectResult = true }, new { Type = MatrixElementTypes.Double, ExpectResult = true }, new { Type = MatrixElementTypes.RgbPixel, ExpectResult = false }, new { Type = MatrixElementTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = MatrixElementTypes.HsiPixel, ExpectResult = false }, new { Type = MatrixElementTypes.UInt32, ExpectResult = false }, new { Type = MatrixElementTypes.UInt8, ExpectResult = false }, new { Type = MatrixElementTypes.UInt16, ExpectResult = false }, new { Type = MatrixElementTypes.Int8, ExpectResult = false }, new { Type = MatrixElementTypes.Int16, ExpectResult = false }, new { Type = MatrixElementTypes.Int32, ExpectResult = false } }; foreach (ImageTypes inputType in Enum.GetValues(typeof(ImageTypes))) { foreach (var output in tests) { if (inputType == ImageTypes.Matrix) { continue; } var expectResult = output.ExpectResult; var imageObj = DlibTest.LoadImage(inputType, path); Array2DMatrixBase outputObj = null; var outputImageAction = new Func <bool, Array2DMatrixBase>(expect => { switch (output.Type) { case MatrixElementTypes.UInt8: outputObj = Dlib.ExtracFHogFeatures <byte>(imageObj); break; case MatrixElementTypes.UInt16: outputObj = Dlib.ExtracFHogFeatures <ushort>(imageObj); break; case MatrixElementTypes.UInt32: outputObj = Dlib.ExtracFHogFeatures <uint>(imageObj); break; case MatrixElementTypes.Int8: outputObj = Dlib.ExtracFHogFeatures <sbyte>(imageObj); break; case MatrixElementTypes.Int16: outputObj = Dlib.ExtracFHogFeatures <short>(imageObj); break; case MatrixElementTypes.Int32: outputObj = Dlib.ExtracFHogFeatures <int>(imageObj); break; case MatrixElementTypes.Float: outputObj = Dlib.ExtracFHogFeatures <float>(imageObj); break; case MatrixElementTypes.Double: outputObj = Dlib.ExtracFHogFeatures <double>(imageObj); break; case MatrixElementTypes.RgbPixel: outputObj = Dlib.ExtracFHogFeatures <RgbPixel>(imageObj); break; case MatrixElementTypes.RgbAlphaPixel: outputObj = Dlib.ExtracFHogFeatures <RgbAlphaPixel>(imageObj); break; case MatrixElementTypes.HsiPixel: outputObj = Dlib.ExtracFHogFeatures <HsiPixel>(imageObj); break; default: throw new ArgumentOutOfRangeException(); } return(outputObj); }); var successAction = new Action <Array2DMatrixBase>(image => { MatrixBase ret = null; try { ret = Dlib.DrawHog(image); } catch (Exception e) { Console.WriteLine(e); throw; } finally { if (ret != null) { this.DisposeAndCheckDisposedState(ret); } } }); var failAction = new Action(() => { Assert.Fail($"{testName} should throw excption for InputType: {inputType}, OutputType: {output.Type}."); }); var finallyAction = new Action(() => { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } if (outputObj != null) { this.DisposeAndCheckDisposedState(outputObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}, OutputType: {output.Type}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } } }