| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
""" |
| 4 |
Extract blobs (plugin example) |
| 5 |
============================== |
| 6 |
|
| 7 |
This is a simple example of a DataLab image processing plugin. |
| 8 |
|
| 9 |
It adds a new menu entry in "Plugins" menu, with a sub-menu "Extract blobs (example)". |
| 10 |
This sub-menu contains two actions, "Preprocess image" and "Detect blobs". |
| 11 |
|
| 12 |
.. note:: |
| 13 |
|
| 14 |
This plugin is not installed by default. To install it, copy this file to |
| 15 |
your DataLab plugins directory (see `DataLab documentation |
| 16 |
<https://datalab-platform.com/en/features/advanced/plugins.html>`_). |
| 17 |
""" |
| 18 |
|
| 19 |
import numpy as np |
| 20 |
import sigima.objects |
| 21 |
import sigima.params |
| 22 |
import skimage.draw |
| 23 |
|
| 24 |
import datalab.plugins |
| 25 |
|
| 26 |
|
| 27 |
class ExtractBlobs(datalab.plugins.PluginBase): |
| 28 |
"""DataLab Example Plugin""" |
| 29 |
|
| 30 |
PLUGIN_INFO = datalab.plugins.PluginInfo( |
| 31 |
name="Extract blobs (example)", |
| 32 |
version="1.0.0", |
| 33 |
description="This is an example plugin", |
| 34 |
) |
| 35 |
|
| 36 |
def generate_test_image(self) -> None: |
| 37 |
"""Generate test image""" |
| 38 |
newparam = self.edit_new_image_parameters( |
| 39 |
title="Test image", hide_dtype=True, shape=(2048, 2048) |
| 40 |
) |
| 41 |
if newparam is not None: |
| 42 |
# Create a NumPy array: |
| 43 |
shape = (newparam.height, newparam.width) |
| 44 |
arr = np.random.normal(10000, 1000, shape) |
| 45 |
for _ in range(10): |
| 46 |
row = np.random.randint(0, shape[0]) |
| 47 |
col = np.random.randint(0, shape[1]) |
| 48 |
rr, cc = skimage.draw.disk((row, col), min(shape) // 50, shape=shape) |
| 49 |
arr[rr, cc] -= np.random.randint(5000, 6000) |
| 50 |
center = (shape[0] // 2,) * 2 |
| 51 |
rr, cc = skimage.draw.disk(center, min(shape) // 10, shape=shape) |
| 52 |
arr[rr, cc] -= np.random.randint(5000, 8000) |
| 53 |
data = np.clip(arr, 0, 65535).astype(np.uint16) |
| 54 |
|
| 55 |
# Create a new image object and add it to the image panel |
| 56 |
obj = sigima.objects.create_image( |
| 57 |
newparam.title, data, units=("mm", "mm", "lsb") |
| 58 |
) |
| 59 |
self.proxy.add_object(obj) |
| 60 |
|
| 61 |
def preprocess(self) -> None: |
| 62 |
"""Preprocess image""" |
| 63 |
panel = self.imagepanel |
| 64 |
param = sigima.params.BinningParam.create(sx=2, sy=2) |
| 65 |
panel.processor.run_feature("binning", param) |
| 66 |
panel.processor.run_feature( |
| 67 |
"moving_median", sigima.params.MovingMedianParam.create(n=5) |
| 68 |
) |
| 69 |
|
| 70 |
def detect_blobs(self) -> None: |
| 71 |
"""Detect circular blobs""" |
| 72 |
panel = self.imagepanel |
| 73 |
param = sigima.params.BlobOpenCVParam() |
| 74 |
param.filter_by_color = False |
| 75 |
param.min_area = 600.0 |
| 76 |
param.max_area = 6000.0 |
| 77 |
param.filter_by_circularity = True |
| 78 |
param.min_circularity = 0.8 |
| 79 |
param.max_circularity = 1.0 |
| 80 |
panel.processor.run_feature("blob_opencv", param) |
| 81 |
|
| 82 |
def create_actions(self) -> None: |
| 83 |
"""Create actions""" |
| 84 |
acth = self.imagepanel.acthandler |
| 85 |
with acth.new_menu(self.PLUGIN_INFO.name): |
| 86 |
acth.new_action( |
| 87 |
"Generate test image", |
| 88 |
triggered=self.generate_test_image, |
| 89 |
select_condition="always", |
| 90 |
) |
| 91 |
acth.new_action("Preprocess image", triggered=self.preprocess) |
| 92 |
acth.new_action("Detect circular blobs", triggered=self.detect_blobs) |