Is there a way to remove all pixels that aren’t connected to at least 10 other pixels? I want this
to look something like
.
Advertisement
Answer
Using erosion and dilation to remove the grain works very well for this particular image
import numpy as np import matplotlib.pyplot as plt from scipy import ndimage img = np.dot(plt.imread('https://i.stack.imgur.com/ypBGu.png')[...,:3], [0.299, 0.587, 0.114]) img = 1 - (img > .6) img = ndimage.binary_erosion(img, structure=np.ones((2,2))).astype(int) img = ndimage.binary_dilation(img, structure=np.ones((3,3))).astype(int) labeled_array, num_features = ndimage.label(img) label, counts = np.unique(labeled_array, return_counts=True) labeled_array[np.isin(labeled_array, label[counts < 200])] = 0
This gives an array with every character’s island pixels labeled
plt.imshow(labeled_array, cmap='gray') plt.axis('off');
Output