Recognize objects in image

Androme picture Androme · Apr 14, 2010 · Viewed 17k times · Source

Hello I am in the process of doing a school project, where we have a robot driving on the ground in between Flamingo plates. We need to create an algorithm that can identify the locations of these plates, so we can create paths around them (We are using A Star for that).

So far have we worked with AForged Library and we have created the following class, the only problem with this is that when it create the rectangles dose it not take in account that the plates are not always parallel with the camera border, and it that case will it just create a rectangle that cover the whole plate. So we need to some way find the rotation on the object, or another way to identify this. I have create an image that might help explain this

Image the describe the problem: http://img683.imageshack.us/img683/9835/imagerectangle.png

Any help on how I can do this would be greatly appreciated.

Any other information or ideers are always welcome.

public class PasteMap
{
    private Bitmap image;
    private Bitmap processedImage;
    private Rectangle[] rectangels;

    public void initialize(Bitmap image)
    {
        this.image = image;
    }

    public void process()
    {
        processedImage = image;
        processedImage = applyFilters(processedImage);
        processedImage = filterWhite(processedImage);
        rectangels = extractRectangles(processedImage);
        //rectangels = filterRectangles(rectangels);
        processedImage = drawRectangelsToImage(processedImage, rectangels);
    }

    public Bitmap getProcessedImage
    {
        get
        {
            return processedImage;
        }
    }

    public Rectangle[] getRectangles
    {
        get
        {
            return rectangels;
        }
    }

    private Bitmap applyFilters(Bitmap image)
    {
        image = new ContrastCorrection(2).Apply(image);
        image = new GaussianBlur(10, 10).Apply(image);
        return image;
    }

    private Bitmap filterWhite(Bitmap image)
    {
        Bitmap test = new Bitmap(image.Width, image.Height);

        for (int width = 0; width < image.Width; width++)
        {
            for (int height = 0; height < image.Height; height++)
            {
                if (image.GetPixel(width, height).R > 200 &&
                    image.GetPixel(width, height).G > 200 &&
                    image.GetPixel(width, height).B > 200)
                {
                    test.SetPixel(width, height, Color.White);
                }
                else
                    test.SetPixel(width, height, Color.Black);
            }
        }
        return test;
    }

    private Rectangle[] extractRectangles(Bitmap image)
    {
        BlobCounter bc = new BlobCounter();
        bc.FilterBlobs = true;
        bc.MinWidth  = 5;
        bc.MinHeight = 5;
        // process binary image
        bc.ProcessImage( image );
        Blob[] blobs = bc.GetObjects(image, false);
        // process blobs
        List<Rectangle> rects = new List<Rectangle>();
        foreach (Blob blob in blobs)
        {
            if (blob.Area > 1000)
            {
                rects.Add(blob.Rectangle);
            }
        }

        return rects.ToArray();
    }

    private Rectangle[] filterRectangles(Rectangle[] rects)
    {
        List<Rectangle> Rectangles = new List<Rectangle>();
        foreach (Rectangle rect in rects)
        {
            if (rect.Width > 75 && rect.Height > 75)
                Rectangles.Add(rect);
        }

        return Rectangles.ToArray();
    }

    private Bitmap drawRectangelsToImage(Bitmap image, Rectangle[] rects)
    {
        BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        foreach (Rectangle rect in rects)
            Drawing.FillRectangle(data, rect, Color.Red);
        image.UnlockBits(data);
        return image;
    }
}

Answer

Matt Warren picture Matt Warren · Apr 15, 2010

You need to analyse the blobs a bit more to find the corners as @kigurai has said. The AForge library allows you to do this, see the section Finding convex hull on this page for more info. The screenshot below (from the page) shows a small sample of what the convex hull is.

alt text
(source: aforgenet.com)

You want to take a look at the GetBlobsLeftAndRightEdges function and the GrahamConvexHull class.