本質上,你要做的是識别你的圖像中的所有對象。這可以通過ndimage.measurements.labelfrom scipy.來完成,本質上它在圖像中搜尋連續的像素組,并為它們配置設定一個标簽。然後,您可以在這些标記的扇區中循環,計算對象的大小(以像素為機關),并在此基礎上進行過濾。在
即使你從Excel中提取資料,你實際上得到的是一個249x250像素的“圖像”。Excel中的每個單元格實際上是一個包含值的“像素”。為了使這一點回到原點,您可以直接使用matplotlib中的圖像顯示函數(例如plt.imshow)import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
xn = 250
yn = 249
# fake data to illustrate that images are just matrices of values
X = np.stack([np.arange(xn)] * yn)
Y = np.stack([np.arange(yn)] * xn).transpose()
Z = np.sin(3*np.pi * X/xn) * np.cos(4*np.pi * Y/yn) * np.sin(np.pi * X/xn)
Z[Z <.5>
fig,axes = plt.subplots(1,2)
axes[0].contourf(Z)
axes[0].set_title("Before Removing Features")
# now identify the objects and remove those above a threshold
Zlabeled,Nlabels = ndimage.measurements.label(Z)
label_size = [(Zlabeled == label).sum() for label in range(Nlabels + 1)]
for label,size in enumerate(label_size): print("label %s is %s pixels in size" % (label,size))
# now remove the labels
for label,size in enumerate(label_size):
if size < 1800:
Z[Zlabeled == label] = 0
axes[1].contourf(Z)
axes[1].set_title("After Removing Features")
結果說明: