heltonbiker
heltonbiker

Reputation: 27575

How to sort blobs vertically with AForge.NET BlobCounter?

I have zebra-striped images where each fringe is to be considered a blob for further processing, but which also contains blobs that should not be considered. I am using AForge.NET / C# / VisualStudio2010. A typical example would be this:

enter image description here

The plan is to do this:

  1. Choose some X coordinate defining a vertical line;
  2. Take each white blob which crosses this vertical line;
  3. The output should be a list of blobs or blob indexes SORTED VERTICALLY (this is necessary), so that white fringes would be ordered from higher to lower.

I have this code, which successfully isolate only the fringes crossing the reference vertical line:

using AForge.Imaging;
using AForge.Imaging.Filters;

Bitmap threshold_binary = gimme_threshold_image();
var blob_counter = new BlobCounter();
blob_counter.ProcessImage(threshold_binary);
var blobs = blob_counter.GetObjects(threshold_binary, true);

int midline_x_coord = threshold_binary.Width/2;
int counter = 1;
foreach (var blob in blobs)
{
    // This saves an image for each blob (white fringe) which crosses the vertical reference line,
    // but the fringes are not sorted vertically!
    if (blob.Rectangle.X < midline_x_coord & (blob.Rectangle.X + blob.Rectangle.Width) > midline_x_coord) {
        blob_counter.ExtractBlobsImage(threshold_grayscale, blob, true);
        var blobimage = blob.Image.ToManagedImage();
        blobimage.Save(String.Format("{0}singlefringes/{1}_singlefringe.png", base_input_dir, counter));
        counter++;
    }
}

Since the block above doesn't return sorted fringes, another approach would be to scan the midline vertically, pixel by pixel, identifying the ordered blobs, but I'm having difficulty figuring out how to do it. A proposition would be something like this:

for (int y = 0; y < threshold_binary.Height; y++) {
    if (threshold_binary.GetPixel(midline_x_coord, y)) {
        // somehow create a List of blobs or blob indexes,
        // but only if I can find out which blob
        // this pixel is in (brute-force would be ugly, right?)
    }
}

Thanks for any suggestion!

Upvotes: 1

Views: 1325

Answers (1)

GUSET
GUSET

Reputation: 11

you can sort by vertically by chossing how you want to get the blobs by area or size or x or y (you have to chose y) take it :

BlobCounter bc = new BlobCounter();
bc.ObjectsOrder = ObjectsOrder.YX;

Upvotes: 1

Related Questions