Created
October 19, 2021 00:31
-
-
Save yangyushi/07ed9f44f0b6539f88f7e100a5a023f7 to your computer and use it in GitHub Desktop.
demonstrate `ndimage.label`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
yangyushi
commented
Oct 19, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment