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
|
# -*- coding: utf-8 -*-
"""
Custom denoising filter plugin
==============================
This is a simple example of a DataLab image processing plugin.
It is part of the DataLab custom function tutorial.
.. 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 scipy.ndimage as spi
import sigima.proc.image as sipi
import datalab.plugins
def weighted_average_denoise(data: np.ndarray) -> np.ndarray:
"""Apply a custom denoising filter to an image.
This filter averages the pixels in a 5x5 neighborhood, but gives less weight
to pixels that significantly differ from the central pixel.
"""
def filter_func(values: np.ndarray) -> float:
"""Filter function"""
central_pixel = values[len(values) // 2]
differences = np.abs(values - central_pixel)
weights = np.exp(-differences / np.mean(differences))
return np.average(values, weights=weights)
return spi.generic_filter(data, filter_func, size=5)
class CustomFilters(datalab.plugins.PluginBase):
"""DataLab Custom Filters Plugin"""
PLUGIN_INFO = datalab.plugins.PluginInfo(
name="My custom filters",
version="1.0.0",
description="This is an example plugin",
)
def create_actions(self) -> None:
"""Create actions"""
acth = self.imagepanel.acthandler
proc = self.imagepanel.processor
with acth.new_menu(self.PLUGIN_INFO.name):
for name, func in (("Weighted average denoise", weighted_average_denoise),):
# Wrap function to handle ``ImageObj`` objects instead of NumPy arrays
wrapped_func = sipi.Wrap1to1Func(func)
acth.new_action(
name,
triggered=lambda: proc.compute_1_to_1(wrapped_func, title=name),
)
|