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