Friday, March 4, 2011

C# :- Imaging ( AForge.net )

If you are doing some image processing there is an interesting and cool open source project called AForge.net .  It contains many routines and filters for image processing.  I started playing with Glyph’s Recognition,  glyph’s recognition engine is provided by AForge Framework.   There is  GRATF  (Glyph Recognition and Tracking Framework)  project  based upon  AForge framework.  GRATF framework mainly deals with the Glyph recognition algorithm and glyph tracking  , utilizing the AForge.Net  image processing library. 

To  get started with Glyph recognition , download the GRATF source code from source code repository at Google :-

http://code.google.com/p/gratf/downloads/list 

After downloading the source  you can start exploring the  code. There is an application that comes with download called Glyph’s Recognition Studio.  It lets you to specify the particular glyph to be recognized and also support 2d augmentation , that lets you to place a particular image over the recognized glyph.   Implementation and description of the glyph recognition algorithm is described at :-  http://www.aforgenet.com/articles/glyph_recognition/.

Glyph to be recognised is created on a white paper with outermost white border , then black border with glyph inside.

Glyph’s recognition make use of various image filters from the AForge Framework. It starts all with the bitmap to be processed , its the managed bitmap captured from the source . Bitmap image is converted into Unmanaged image using the UnmanagedImage class of AForge.Imaging namespace.  UnmanagedImage class takes the BitmapData as a parameter in its constructor and converts the Bitmap image into unmanaged image. With unmanaged image we do not have to lock image bits before processing the image pixels , as with managed bitmap image , so overhead of locking and unlocking bitmap is avoided

BitmapData bitmapData = image.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                ImageLockMode.ReadOnly, image.PixelFormat );

UnmanagedImage unmanagedImg = new UnmanagedImage( bitmapData ) ;

With our unmanaged image , first step is to apply the GrayScale filter to unmanaged image . GrayScale filter processes the image pixels and returns only the pixels within gray range from 0 to 1 . Gray range is from complete white to black and varied gray component in-between . As we are only concerned with the intensity of the pixels, so we discard the colour information  by applying the GrayScale filter of AForge framework.

UnmanagedImage grayImage = UnmanagedImage.Create( image.Width, image.Height, PixelFormat.Format8bppIndexed );
Grayscale.CommonAlgorithms.BT709.Apply( image, grayImage );

After we have reduced the image with only gray intensity pixels, we apply DifferenceEdgeDetector filter on the grayscale image to detect the edges in the image .

DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();
           UnmanagedImage edgesImage = edgeDetector.Apply(grayImage);

Then we apply Threshold filter to the image to convert gray range pixels into  black or white pixels using the threshold value taken as constructor parameter by Threshold filter.

Threshold thresholdFilter = new Threshold(40);
           thresholdFilter.ApplyInPlace(edgesImage);

We use BlobCounter class to get all the detected blobs in the image of particular size and width.

BlobCounter blobCounter = new BlobCounter();
          blobCounter.MinHeight = 48;
          blobCounter.MinWidth = 48;

blobCounter.ProcessImage(edgesImage);
           Blob[] blobs = blobCounter.GetObjectsInformation();

We iterate over all the blobs detected and use SimpleShapeChecker to check if the blob is of a particular shape.

List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
List<IntPoint> corners = null;

if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
              

After passing the IsQuadrilateral check of SimpleShapeChecker we calculate the average brightness of the pixels from both the sides of the edges of Quadrilateral . If the average brightness exceeds a predefined value we further process to get the glyph value.  We apply QuadrilateralTransformation filter on the corners of the Quadrilateral and get the rectangle image of the glyph.

Once we have the rectangle image of the glyph we can iterate over all the pixels of the image using the UnmanagedImage pixel byte pointer and calculate the intensity of the pixels for the each cell of the image and then checking it with the glyph database for glyph recognition.

http://www.aforgenet.com/projects/gratf/

http://www.aforgenet.com/projects/gratf/code_samples.html

1 comment:

  1. Hi,

    I'm using asp.net and aforge.net to capture image from ipaddress of dlink webcam do you know where can i find source for this or any links that can help me most of code i found are for windows app i'm looking for webapp.
    please help

    my email id: vi2psn@yahoo.com

    ReplyDelete