Detecting blobs and connections using Python: An Image Processing Introduction

Blobs are pixels that are grouped or connected together that share common properties in an image. Blobs may take different kinds of objects In this article, we will attempt to correctly detect these blobs using this image:

A set of US coins

Our aim is to be able to detect the coins correctly. In order to do this we need to transform the image into a binary format since blobs could be identified as bright areas in a dark background or vice-versa.

from skimage.io import imread, imshow
from skimage.color import rgb2gray
im = rgb2gray(imread(‘coins.jfif’))
im_bw = im < 0.85
imshow(im_bw)
Binarized image of coins

Once we have binarized our image, we can now use the three (3) methods available in detecting blobs and these are: 1) Laplacian of Gaussian, 2) Difference of Gaussian, and 3)Determinant of Hessian

blobs_log = blob_log(im_bw, max_sigma=30, num_sigma=10, threshold=.1)
Results from different blob detection methods

The results from using the three blob detection methods are quite dirty. However, we could clearly see here that the difference of gaussian has much better result in detecting the coins compared to the other two in this case. You may want to play around with the different parameters for the blob_log function to have better results as well.

Another way of detecting objects in an image beside from blob detection is via connected components. The advantage of this compared to blob detection is that we could detect irregular shaped objects, however we need to use morphological operations (which I discussed in one of my articles) in cleaning our image.

## Morphing/Cleaning the image
from skimage.morphology import erosion, dilation, opening, closing
from skimage.measure import label, regionprops
from skimage.color import label2rgb
Morphed image of coins

As you can see, I dilated the image 5 times and then eroded it 5 times again in order to fill the gaps in the coins. The morphological operations that you will use and the number of times you will apply it may vary depending on the image that you have. After morphing, we can now use the following for identifying the coins:

## Detecting the coins
label_im = label(im_cleaned)
imshow(label_im)
Detecting the coins based on their connectivity and color

What I did here is that I used the label function from the skimage library that will label the connected pixels from one another. Each of the coins has their own somewhat a color code value which makes them identifiable from one another. If ever there are coins that are probably overlapping and thus there’s a connection, they might end up with the same color even though they are actually different coins.

And that’s it for blob detection and connected components! Hope I was able to help you regarding this topic.

A developer by day, a programmer by night

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store