image-processingcharacterocraforgeblobs

Eliminating blob inside another blob


I'm currently working on a program for character recognition using C# and AForge.NET and now I'm struggling with the processing of blobs.

This is how I created the blobs:

BlobCounter bcb = new BlobCounter();
            bcb.FilterBlobs = true;
            bcb.MinHeight = 30;
            bcb.MinWidth = 5;
            bcb.ObjectsOrder = ObjectsOrder.XY;
            bcb.ProcessImage(image);

I also marked them with rectangles:

Rectangle[] rects;
rects = bcb.GetObjectsRectangles();
Pen pen = new Pen(Color.Red, 1);
Graphics g = Graphics.FromImage(image);

foreach (Rectangle rect in rects)
{
     g.DrawRectangle(pen, rect);
}

After execution my reference image looks like this:

BlobImage BlobImage

As you can see, almost all characters are recognized. Unfortunately, some character include blobs inside a blob, e.g. "g", "o" or "d".

I would like to eliminate the blobs which are inside another blob.

I tried to adjust the drawing of the rectangles to achieve my objective:

foreach (Rectangle rect in rects)
{

    for (int i = 0; i < (rects.Length - 1); i++)
    {
         if (rects[i].Contains(rects[i + 1]))
             rects[i] = Rectangle.Union(rects[i], rects[i + 1]);

    }
    g.DrawRectangle(pen, rect);
}

...but it wasn't successful at all.

Maybe some of you can help me?


Solution

  • you can try to detect rectangles within rectangles by check their corner indices, I have one MATLAB code for this which i have written for similar kind of problem:

    Here is snippet of the code is:

    function varargout = isBoxMerg(ReferenceBox,TestBox,isNewBox)

    X = ReferenceBox; Y = TestBox;

    X1 = X(1);Y1 = X(2);W1 = X(3);H1 = X(4); X2 = Y(1);Y2 = Y(2);W2 = Y(3);H2 = Y(4);

    if ((X1+W1)>=X2 && (Y2+H2)>=Y1 && (Y1+H1)>=Y2 && (X1+W1)>=X2 && (X2+W2)>=X1)

    Intersection = true; else

    `Intersection = false;`
    

    end

    in above if Intersection variable becomes true that means the boxes having intersection. you can use this code for further customization.

    Thank You