Skip to content

Instantly share code, notes, and snippets.

@yangyushi
Created October 19, 2021 00:31
Show Gist options
  • Save yangyushi/07ed9f44f0b6539f88f7e100a5a023f7 to your computer and use it in GitHub Desktop.
Save yangyushi/07ed9f44f0b6539f88f7e100a5a023f7 to your computer and use it in GitHub Desktop.
demonstrate `ndimage.label`
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
x = np.zeros((30, 30))
x[10, 10] = 1
x[20, 10] = 1
x[10, 20] = 1
x[20, 20] = 1
label, num = ndimage.label(x)
plt.figure(figsize=(8, 5))
plt.suptitle(f"`ndimage.label` return [{num}] features\nIgnoring background.")
im = plt.subplot(121).imshow(x, cmap='gray')
plt.axis('off')
plt.colorbar(im)
plt.title('input')
im = plt.subplot(122).imshow(label, cmap='rainbow')
plt.axis('off')
plt.colorbar(im)
plt.title('label')
plt.tight_layout()
plt.show()
@yangyushi
Copy link
Author

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment