# Blob's pixel position

**URL:** <https://forum.commonvisionblox.com/t/blobs-pixel-position/1049>\
**Category:** Programming Questions\
**Tags:** csharp\
**Created:** [February 7, 2020, 8:41am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049 "2020-02-07T08:41:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 7, 2020, 8:41am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/1 "2020-02-07T08:41:17Z")

</div>

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](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/362c45ff60a77dfdc701946d08b04e3bac9953f5.png)

thanks.

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [February 7, 2020, 8:55am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/2 "2020-02-07T08:55:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 7, 2020, 9:29am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/3 "2020-02-07T09:29:27Z")

</div>

> [@parsd](#):
>
> All the points that are part of the blob

Hi Parsd:

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

thank you for helping.

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [February 7, 2020, 10:03am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/4 "2020-02-07T10:03:37Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 13, 2020, 3:20am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/5 "2020-02-13T03:20:10Z")

</div>

Hi Sir:

My test binarized image is only one white circle below.  
 ![aa](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/2379201fa219dabd1cdda47cf36d89c0e508c9b0.jpeg)

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

 ![2020_02_13_11_03_36_AICapClassifier_偵錯_Microsoft_Visual_Studio](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/1dc5894c50fa495155d61bbbbc35bfdb8da20355.png)  
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());
        }
    }
```

---

<div class="post-metadata">

**Author:** ![Frank](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/frank/32/42_2.png) [@Frank](https://forum.commonvisionblox.com/u/Frank)\
**Post date:** [February 13, 2020, 7:58am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/6 "2020-02-13T07:58:50Z")

</div>

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:

```cs
// 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);

```

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [February 13, 2020, 9:00am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/7 "2020-02-13T09:00:58Z")

</div>

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_:

 ![grafik](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/55e457470b99e5211754935e5fb18221597c1b7f.png)  
(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:

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

```

---

<div class="post-metadata">

**Author:** ![Frank](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/frank/32/42_2.png) [@Frank](https://forum.commonvisionblox.com/u/Frank)\
**Post date:** [February 13, 2020, 9:28am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/8 "2020-02-13T09:28:05Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 14, 2020, 4:50am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/9 "2020-02-14T04:50:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 14, 2020, 4:52am UTC](https://forum.commonvisionblox.com/t/blobs-pixel-position/1049/10 "2020-02-14T04:52:03Z")

</div>

Hi Frank:

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