2

In the context of analysing images to find zones with movement, here's what I've got as an intermediate result, using opencv with python (assume these are 100% binary):

Binary image

So my question is: is there a way to locate blobs of white with a specific "thickness" threshold ?

Here's what it could look like, roughly:

Resulting image

I've been looking for transforms and manipulations such as connected components and morphological transformations but those won't work and I can't quite figure out where to start other than that.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Mat
  • 1,440
  • 1
  • 18
  • 38

1 Answers1

3

The morphological opening is ideal for this problem. It removes all the white parts thinner than a given diameter.

In OpenCV it is implemented in cv2.morphologyEx using op=cv2.MORPH_OPEN:

kernel = cv2.getStructuringElement(cv2.cv.MORPH_ELLIPSE, diameter)
output = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

But note that this removes parts of objects that are thin, it doesn't leave the full object if a part of it is wide enough. That can be done using an opening by reconstruction, an erosion followed by a morphological reconstruction (also known as geodesic dilation).

OpenCV doesn't have that algorithm. This Q&A gives a rough outline for how to implement it in OpenCV, but that is a very expensive algorithm, there are much more efficient ones.

There might be an implementation in Scikit-image, I haven't looked for it.

DIPlib (with Python bindings called PyDIP) (also, I'm an author) has a dip.OpeningByReconstruction. Install the Python module with pip install diplib.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • I would also add this [webpage](https://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm) containing articles for more extensive explanation regarding morphological operations. After performing an opening you can call `cv.boundingRect(img)` from openCV to locate the remaining objects. – RobertMoga May 21 '19 at 10:18
  • 1
    @RobertMoga: Though I appreciate Bob’s work in putting that web site together, the page you link provide a highly incomplete view of Mathematical Morphology. That was the state more or less 50 years ago when the field was created. A lot of things have happened in 50 years. – Cris Luengo May 21 '19 at 13:24