AForge.NET

Demos
Quick Start
Code Samples
License
Downloads
Sources
Discussions
Members
Donate

Glyph Recognition and Tracking Framework
2.0.1 version is available!

Code Samples

In the case you are going to use Glyph Recognition library in your application, the information and sample codes below may be quite useful to give quick direction and some results.

The first thing to do is to add reference to AForge.Vision.GlyphRecognition.dll assembly to your project, which can be found in binary distribution of the GRATF project or built from sources. Also you will need to add references to AForge.dll, AForge.Imaging.dll and AForge.Math.dll (part of AForge.NET framework), since those are used by Glyph Recognition library. In the case if you work with video files, cameras, etc., you may need adding some other references as well.

The simplest code sample will be just doing localization of glyphs. Here it is ...

// create glyph recognizer for 5x5 glyphs
GlyphRecognizer recognizer = new GlyphRecognizer( 5 );
// process an image and find glyphs in it
List<ExtractedGlyphData> glyphs = recognizer.FindGlyphs( bitmap );
// process all found glyphs
foreach ( ExtractedGlyphData glyphData in glyphs )
{
    // ...
}

The above code is very simple - just create an instance of GlyphRecognizer telling it which glyph size you are interested in and then feed a bitmap to it (standard .NET's Bitmap). If you put polygon drawing code inside of the above foreach loop (use ExtractedGlyphData.Quadrilateral for glyph's coordinates), then you should get something like this:

Highlighted glyphs

If you would like doing further glyph recognition on your own, you may use ExtractedGlyphData.RawData, which is a 2D arrays containing raw glyph values extracted from the processed images. The array contains "0" and "1" values, which correspond to black and white cells of a glyph.

However it is better to do glyph recognition utilizing the Glyph Recognition library. In order to do this you need to create a glyphs' database and utilize it in conjunction with the GlyphRecognizer class. Here is the code for creating a glyphs' database containing 3 glyphs:

// create glyph database for 5x5 glyphs
GlyphDatabase glyphDatabase = new GlyphDatabase( 5 );
// add some glyphs ...
// 1
glyphDatabase.Add( new Glyph( "Spider", new byte[5, 5] {
    { 0, 0, 0, 0, 0 },
    { 0, 1, 1, 0, 0 },
    { 0, 0, 1, 0, 0 },
    { 0, 1, 0, 1, 0 },
    { 0, 0, 0, 0, 0 } } ) );
// 2
glyphDatabase.Add( new Glyph( "Fly", new byte[5, 5] {
    { 0, 0, 0, 0, 0 },
    { 0, 1, 0, 0, 0 },
    { 0, 0, 1, 0, 0 },
    { 0, 1, 0, 1, 0 },
    { 0, 0, 0, 0, 0 } } ) );
// 3
glyphDatabase.Add( new Glyph( "Bug", new byte[5, 5] {
    { 0, 0, 0, 0, 0 },
    { 0, 0, 1, 0, 0 },
    { 0, 1, 1, 0, 0 },
    { 0, 0, 1, 1, 0 },
    { 0, 0, 0, 0, 0 } } ) );

Now we just need to specify the glyphs' database we've created to the GlyphRecognizer:

GlyphRecognizer recognizer = new GlyphRecognizer( glyphDatabase );

When glyph database is set, we may analyze ExtractedGlyphData.RecognizedGlyph after image processing is done to find which glyph was found (it will be set null if the glyph was not recognized). If GlyphRecognizer managed to recognize any of the glyphs in the specified image, then it will set RecognizedGlyph appropriately and glyph's name could be checked. So if we extend foreach loop with glyph's name drawing, then we may get something like this:

Glyphs' names

If a glyph was recognized, then you may also use RecognizedQuadrilateral property of ExtractedGlyphData - this property keeps quadrilateral corresponding to the glyph as it is defined in database (not the one which was extracted). This quadrilateral can be used with something like BackwardQuadrilateralTransformation to get something like this:

2D Augmented Reality

If you plan to do 3D augmented reality or something else which requires 3D pose estimation of glyphs, then you will need to use the GlyphTracker class also, which performs glyphs tracking, reducing shaking of glyphs and estimating their 3D pose in the real world. Just pass the list of detected glyphs to its TrackGlyph() method and then examine IsTransformationDetected and TransformationMatrix properties of ExtractedGlyphData objects. This will give us glyph recognition code like this (only few lines were added to the very first code shown above):

// create glyph recognizer for the specified glyphs' database
GlyphRecognizer recognizer = new GlyphRecognizer( glyphDatabase );
// create glyph tracker to track position of glyphs
private GlyphTracker glyphTracker = new GlyphTracker( );
// set glyphs' size 113 mm, for example
glyphTracker.GlyphSize = 113;

...

// process an image and find glyphs in it
List<ExtractedGlyphData> glyphs = recognizer.FindGlyphs( bitmap );
glyphTracker.TrackGlyphs( glyphs );

// process all found glyphs
foreach ( ExtractedGlyphData glyphData in glyphs )
{
    // ...
}

Important thing to note is that calculated glyph's transformation matrix represents rotation and translation in the real world coordinates system, which is left-handed - Z axis goes away from viewer when X and Y axes are directed to the right and up correspondingly. Also provided translation uses same units as were specified for glyphs size. In other words if you set glyphs size in millimeters, then translation coordinates are also in millimeters.

If you have some experience with OpenGL, DirectX, XNA or any other 3D rendering engine, then after transforming GRATF transformation matrix to the one used in the 3D engine of your choice (XNA and OpenGL, for example, use right-handed coordinates systems ...), you may get some 3D augmented reality like this:

3D Augmented Reality

Some more information about 3D augmented reality can be found in the "3D Augmented Reality" article.

That is it. Happy glyphing!

P.S. Need more samples? Try getting sources and study Glyph Recognition Studio ;)