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:
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
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)# Compute radii in the 3rd column.
blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)blobs_dog = blob_dog(im_bw, max_sigma=30, threshold=.1)
blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2)blobs_doh = blob_doh(im_bw, max_sigma=30, threshold=.01)blobs_list = [blobs_log, blobs_dog, blobs_doh]
colors = ['blue', 'lime', 'red']
titles = ['Laplacian of Gaussian', 'Difference of Gaussian',
'Determinant of Hessian']
sequence = zip(blobs_list, colors, titles)fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True)
ax = axes.ravel()for idx, (blobs, color, title) in enumerate(sequence):
for blob in blobs:
y, x, r = blob
c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)
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 label2rgbdef multi_dil(im,num):
for i in range(num):
im = dilation(im)
return imdef multi_ero(im,num):
for i in range(num):
im = erosion(im)
return imim_cleaned = multi_ero(multi_dil(im_bw,5),5)
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)
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.