Jarartur
Jarartur

Reputation: 159

Masking in ndimage binary_closing

I have two images:

  1. A binary outline of some structure (outline).
  2. Some surfaces inside this structure (surface).

I want to merge those two images in a way that will leave no holes between them. To this end I:

  1. made a copy of the surface and dilated it 10 times
  2. merged the undilated, original surface and outline
  3. run ndi.binary_closing with the dilated surface as a mask
outline = ...
surface = ...

merged = np.logical_or(outline, surface)
dilated_surface = ndimage.binary_dilation(surface, iterations=10)
merged = ndimage.binary_closing(merged, mask=dilated_surface)

According to documentation mask should only impact which pixels can be changed. So Why am I getting two completley different results when running it this way in comparison to without mask? I mean the holes on the edges of the mask.

I know I can instead do:

closed = ndi.binary_closing(merged)
merged[neighborhood_mask] = closed[neighborhood_mask]

but it gives slightly different results (understandably) and I want to understand the masking better.

Here are the results:

Results

And there is the merged image alone along with surface:

enter image description here

enter image description here

Upvotes: 0

Views: 72

Answers (1)

Banana
Banana

Reputation: 1

import numpy as np
from scipy import ndimage

outline = ...  # binary outline image
surface = ...  # binary surface image

# Merge the outline and surface
merged = np.logical_or(outline, surface)

# Dilate the surface
dilated_surface = ndimage.binary_dilation(surface, iterations=10)

# Apply binary closing globally
closed_global = ndimage.binary_closing(merged)

# Apply the closed result to the neighborhood of the dilated surface
merged[dilated_surface] = closed_global[dilated_surface]

# Now `merged` contains the final result with no gaps

Upvotes: 0

Related Questions