Detecting blobs on an image#
This example shows how to detect blobs on an image with DataLab, and also covers other features such as the plugin system:
Add a new plugin to DataLab
Denoise an image
Detect blobs on an image
Save the workspace to a file
First, we open DataLab, and open the settings dialog (using āFile > Settingsā¦ā,
or the icon in the toolbar).
![../../_images/01.png](../../_images/01.png)
In the āGeneralā tab, we can see the āPlugins pathā field. This is the path where DataLab will look for plugins. We can add a new plugin by copying/pasting the plugin file in this directory.#
See also
The plugin system is described in the Plugins section.
Letās add the cdl_example_imageproc.py plugin to DataLab (this is an example plugin that is shipped with DataLab source package, or may be downloaded from here on GitHub).
If we close and reopen DataLab, we can see that the plugin is now available in the āPluginsā menu: there is a new āExtract blobs (example)ā entry.
![../../_images/02.png](../../_images/02.png)
The āAbout DataLabā dialog shows the list of available plugins.#
![../../_images/03.png](../../_images/03.png)
Letās click on āExtract blobs (example) > Generate test imageā#
For information, the image is generated by the plugin using the following code:
def generate_test_image(self) -> None:
"""Generate test image"""
# Create a NumPy array:
arr = np.random.normal(10000, 1000, (2048, 2048))
for _ in range(10):
row = np.random.randint(0, arr.shape[0])
col = np.random.randint(0, arr.shape[1])
rr, cc = skimage.draw.disk((row, col), 40, shape=arr.shape)
arr[rr, cc] -= np.random.randint(5000, 6000)
icenter = arr.shape[0] // 2
rr, cc = skimage.draw.disk((icenter, icenter), 200, shape=arr.shape)
arr[rr, cc] -= np.random.randint(5000, 8000)
data = np.clip(arr, 0, 65535).astype(np.uint16)
# Create a new image object and add it to the image panel
image = cdl.obj.create_image("Test image", data, units=("mm", "mm", "lsb"))
self.proxy.add_object(image)
![../../_images/04.png](../../_images/04.png)
The plugin has generated a test image, and added it to the āImagesā panel. The image shows a few blobs, with a central dark disk, and a noisy background.#
The plugin has other features, such as denoising the image, and detecting blobs on the image, but we wonāt cover them here: we will use the same DataLab native features as the plugin, manually.
The image is a bit noisy, and also quite large. Letās reduce the size of the image while denoising it a bit by binning the image by a factor of 2.
![../../_images/05.png](../../_images/05.png)
Click on āOperations > Pixel binningā.#
![../../_images/06.png](../../_images/06.png)
The āBinningā dialog opens. Set the binning factor to 2, and click on āOKā.#
![../../_images/07.png](../../_images/07.png)
The binned image is added to the āImagesā panel. It is now easier to see the blobs (even if they were already quite visible on the original image: this is just an example), and the image will be faster to process.#
Letās apply a moving median filter to the image, to denoise it a bit more.
![../../_images/08.png](../../_images/08.png)
Click on āProcessing > Moving medianā entry, and set the window size to 5
.#
![../../_images/09.png](../../_images/09.png)
The filtered image is added to the āImagesā panel. Denoising is quite efficient.#
Now, letās detect the blobs on the image.
![../../_images/10.png](../../_images/10.png)
Click on āComputing > Blob detection > Blob detection (OpenCV)ā.#
![../../_images/11.png](../../_images/11.png)
The āBlob detection (OpenCV)ā dialog opens. Set the parameters as shown on the screenshot, and click on āOKā.#
![../../_images/12.png](../../_images/12.png)
The āResultsā dialog opens, showing the detected blobs: one line per blob, with the blob coordinates and radius.#
Note
If you want to show the computing results again, you can select the āShow resultsā
entry in the āComputingā menu, or the āShow resultsā
button, below the image list:
![../../_images/121.png](../../_images/121.png)
![../../_images/13.png](../../_images/13.png)
The detected blobs are also added to the image metadata, and can be seen in the visualization panel on the left.#
Finally, we can save the workspace to a file. The workspace contains all the images
that were loaded in DataLab, as well as the processing results. It also contains the
visualization settings (colormaps, contrast, etc.), the metadata, and the annotations.
To save the workspace, click on āFile > Save to HDF5 fileā¦ā, or the
button in the toolbar.
If you want to load the workspace again, you can use the āFile > Open HDF5 fileā¦ā
(or the button in the toolbar) to load the whole workspace, or the
āFile > Browse HDF5 fileā¦ā (or the
button in the toolbar) to load
only a selection of data sets from the workspace.