Capturing image of a selected blob

In an image which contains a number of blobs, I tried to capture image of one of the blobs, using
Foundation.FBlobBinariseImage(imgIn, ref selectedROI, blobIndex, minThresh, maxThresh, BlobImgOut)

an image of the entire blobs will be returned when blobIndex=0, and for the other values a black image (pixels value = 0) is returned.

I also tried using Blob.GetResultImage(blob, imgResult, blobNum)

if blobNum is greater than 0, an error is returned, for blobNum=0 the image containing the entire blobs is returned.

How can get the image of a selected blob ?

It might be you are you mixing FBlob and the stand-alone Blob tool. These are two different tools. You cannot use methods of Blob on results of FBlob and vice versa or mix them in some other way.

Hi Frank,

Thank you for your reply. We can not mix these two types, as the blob has different types in the two, SharedFBlob and the other SharedBlob.

Do you have a piece of code using either of the two methods that may be used to get the image of a selected blob from the pool of blobs found in an image ?

This works for me with FBlob.

auto img = Cvb::Image::Load(Cvb::Utilities::ExpandPath(LR"(%CVB%\Tutorial\Foundation\Images\Blob\Microswitch.bmp)"));

Cvb::Foundation::BlobFilter filter;
filter[Cvb::Foundation::BlobRangeFilter::Size] = Cvb::ValueRange<int>(100, 100000);
auto blobResults = Cvb::Foundation::BinarizeAndSearchBlobs(img->Plane(0), Cvb::ValueRange<int>(0, 62), filter);

int index = 0;
for (auto blob : blobResults)
{
	auto blobCropImage = img->Map(blob.BoundingBox());
	blobCropImage->Save(L"blobCropImage" + std::to_wstring(index) + L".bmp");

	index++;
}
2 Likes

Frank, thank you for the sample code. I am not very familiar with C++, as I code in C#. However from what I can gather the code crops the original image, with respect to the bounding box of the blob of interest. Does this mean if parts or the whole of other blobs are within that bounding box, they will be included in the cropped image. I am looking for a method to display only the blob of interest. Do you possibly have this code in C# ? Thanks.

Here you go:

using Stemmer.Cvb;
using Stemmer.Cvb.Foundation;
using System.Linq;

namespace FBlob_ExtractBlobImage_CS
{
  class Program
  {
    static void Main(string[] args)
    {
      var img = Image.FromFile(@"%CVB%\Tutorial\Foundation\Images\Blob\Microswitch.bmp");

      BlobFilter filter = new BlobFilter();
      filter.RangeFilters.Add(RangeFilter.Size, new ValueRange<int>(100, 100000));

      var blobResult = Blob.BinarizeAndSearch(img.Planes[0], new ValueRange<int>(0, 62), filter);

      foreach (var blob in blobResult.Select((value, index) => ( value, index )))
      {
        var blobCropImage = img.Map(blob.value.BoundingBox);
        blobCropImage.Save($"blobCropImage{blob.index}.bmp");
      }
    }
  }
}

By the way, if you prefer C#/.Net, you could have posted this question in that particular sub forum. I’ve only replied with C++, because we are here in the C-style API sub forum. :slight_smile:

You are right, this simply crops the bounding box of the a given blob.

If you only want the very area of the blob, you will have to use the binary image as a mask for the source image.

Thank you Frank. :slightly_smiling_face: