Blob's pixel position

Hi Sir

I want to try Stemmer.Cvb.Foundation.Blob Class to find the binarized Image blobs.

If I want to get each blob’s all pixel’s position, how can to do this class by C# code?

89

thanks.

Hi @Jesse,

there is currently no possibility to get the run length code of the blob detection from the outside, just the bounding box. What exactly would you need? All the points that are part of the blob or just the contour?

Hi Parsd:

Could the CVB C# dll can get all the points that are part of the blob ?

thank you for helping.

If you have no other objects inside the found blob’s bounding box you could use this function to extract all set (i.e. non-0) pixels on the binary image:

Point2D[] GetObjectPoints(ImagePlane plane, Rect aoi)
{
  if (plane.DataType != DataTypes.Int8BppUnsigned)
    throw new ArgumentException("Only byte-planes are supported", nameof(plane));
  if (!plane.Parent.Bounds.Contains(aoi.Left, aoi.Top) || !plane.Parent.Bounds.Contains(aoi.Right, aoi.Bottom))
    throw new ArgumentException("Area of interest is outside plane bounds", nameof(aoi));
  
  var objectPoints = new List<Point2D>();
  
  var access = plane.GetLinearAccess<byte>();
  
  for (int y = aoi.Top; y <= aoi.Bottom; y++)
  {
    for (int x = aoi.Left; x <= aoi.Right; x++)
    {
      if (access[x, y] > 0)
        objectPoints.Add(new Point2D(x, y));
    }
  }
  
  return objectPoints.ToArray();
}

With a different threshold than 0 you could also work on non-binary data. Or you could make this faster by using unsafe pixel access via pointers.

2 Likes

Hi Sir:

My test binarized image is only one white circle below.
aa

I try to use the Stemmer.Cvb.Foundation.Blob.SearchAll() function to find the blob.But it find 521 blobs.


Could you help me to check? thank you .

My C# code:

private void BlobDetect()
{
try
{
string filepath = @“C:\Users\Desktop\binary\aa.jpg”;

            var image = Stemmer.Cvb.Image.FromFile(filepath);

            Stemmer.Cvb.Foundation.BlobResult[] blob_result = Stemmer.Cvb.Foundation.Blob.SearchAll(image);
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.ToString());
        }
    }

I think you have so many blobs because you don’t filter by size. You could add a size filter first and then use this filter during the blob search.

It could be something like this:

// Create blob filter
BlobFilter filter = new BlobFilter();
// Allowed area in pixel, min: 10000000, max: 1000000000
filter.RangeFilters.Add(RangeFilter.Size, new ValueRange<int>(10000000, 1000000000));

// perform blob search
var blobResult = Blob.Search(img, filter);

Hi @Jesse,

also in addition to what @Frank said: your image is not really binary. Probably due to the fact that you stored it as a jpeg:


(This is a histogram removing gray-values 0 and 255)
The .SearchAll function treats all non-0 values as being set. Thus you find blobs in the compression artifacts of jpeg.

So either use a bmp for testing or binarize the image: you then only get the circle:

Blob.BinarizeAndSearchAll(image.Planes[0], 128, 255)

If you are going to use the binarize + search method, you could also use the filters there:

var blobResult = Blob.BinarizeAndSearch(img.Planes[0], 128, 255, filter);
1 Like

Hi Parsd:

Thank you for helping me to check the image and give me BinarizeAndSearchAll sample code. I also check it again, it has some pixel’s gray value is not zero.

Hi Frank:

Thank you for helping me to check and test the image and give me BinarizeAndSearch sample code.