labels = [2,3,7]
# or whatever you have
good_labels = []
for label in labels: m = matrix == label
non_empty_columns = sum(sum(m)>0)
non_empty_rows = sum(sum(m.transpose())>0)
if 1.0 * non_empty_rows / (non_empty_columns+0.001) > threshold:
good_labels.append(label)
That will remove very long (vertically) blobs, turn rows and columns around to remove horizontally stretched blobs.
参考的连接