Finding thick objects on binary image
Question:
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):
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:
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.
Answers:
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
.
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):
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:
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.
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
.