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
JavaScript
x
15
15
1
import numpy as np
2
import matplotlib.pyplot as plt
3
from scipy import ndimage
4
5
6
img = np.dot(plt.imread('https://i.stack.imgur.com/ypBGu.png')[ ,:3], [0.299, 0.587, 0.114])
7
8
img = 1 - (img > .6)
9
img = ndimage.binary_erosion(img, structure=np.ones((2,2))).astype(int)
10
img = ndimage.binary_dilation(img, structure=np.ones((3,3))).astype(int)
11
12
labeled_array, num_features = ndimage.label(img)
13
label, counts = np.unique(labeled_array, return_counts=True)
14
labeled_array[np.isin(labeled_array, label[counts < 200])] = 0
15
This gives an array with every character’s island pixels labeled
JavaScript
1
3
1
plt.imshow(labeled_array, cmap='gray')
2
plt.axis('off');
3
Output