question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feat] Provide scipy.ndimage.measurements and .morphology functions

See original GitHub issue

It would be nice if there were more performant versions of the functions in https://docs.scipy.org/doc/scipy-0.16.0/reference/ndimage.html#module-scipy.ndimage.measurements

For my use case, backpropagation isn’t needed.

I’m mostly interested in label, center_of_mass, and the binary erosion, dilation, hole filling, etc.

Here’s what I have been using (note that these are not exact reimplementations; they just work for my use case):

class MorphologySubstitute(nn.Module):
    def __init__(self):
        super().__init__()

        self.conv_list = nn.ModuleList([
            self._make_circle_conv(radius) for radius in range(1, 8)
        ])

    def _make_circle_conv(self, radius):
        diameter = 1 + radius * 2

        a = torch.linspace(-1, 1, steps=diameter)**2
        b = (a[None] + a[:, None])**0.5

        circle_weights = (b <= 1.0).to(torch.float32)

        conv = nn.Conv2d(1, 1, kernel_size=diameter, padding=radius, bias=False)
        conv.weight.data.fill_(1)
        conv.weight.data *= circle_weights / circle_weights.sum()

        return conv


    def erode(self, input_mask, radius, threshold=1):
        conv = self.conv_list[radius - 1]
        input_float = input_mask.to(torch.float32)
        result = conv(input_float)

        return result >= threshold

    def dilate(self, input_mask, radius, threshold=0):
        conv = self.conv_list[radius - 1]
        input_float = input_mask.to(torch.float32)
        result = conv(input_float)

        return result > threshold

    # Note that this is NOT fill_holes, since it doesn't handle complex boundaries well
    def fill_cavity(self, input_mask):
        cumsum = input_mask.cumsum(-1)
        filled_mask = (cumsum > 0)
        filled_mask &= (cumsum < cumsum[..., -1:])
        cumsum = input_mask.cumsum(-2)
        filled_mask &= (cumsum > 0)
        filled_mask &= (cumsum < cumsum[..., -1:, :])

        return filled_mask

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
edgarribacommented, Jan 20, 2021

from what I read, label would be the classical connected components algorithm, right ? can this be differentiable ?

As for center of mass, I believe that we already have some sort of approximation that could be used as a base: https://kornia.readthedocs.io/en/latest/geometry.subpix.html#kornia.geometry.subpix.spatial_expectation2d or https://kornia.readthedocs.io/en/latest/geometry.subpix.html#kornia.geometry.subpix.conv_soft_argmax2d

1reaction
edgarribacommented, Jan 20, 2021

@elistevens we included a morphology module. Please, check it out 😃 https://kornia.readthedocs.io/en/latest/morphology.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multidimensional image processing (scipy.ndimage)
This package contains various functions for multidimensional image processing. ... Gradient magnitude using a provided gradient function.
Read more >
Multidimensional image processing (scipy.ndimage)
The scipy.ndimage packages provides a number of general image processing and analysis ... functions for linear and non-linear filtering, binary morphology, ...
Read more >
scipy.ndimage.measurements.label
An integer ndarray where each unique feature in input has a unique label in the returned array. How many objects were found. If...
Read more >
Multi-dimensional image processing (scipy.ndimage)
This package contains various functions for multi-dimensional image processing. ... Gradient magnitude using a provided gradient function.
Read more >
scipy.ndimage.morphology.distance_transform_edt
Spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found