您可以使用numpy
' 广播过滤掉高于阈值的像素。如果您事先模糊图像,这会更好。下面给出了一个完整的工作示例(没有模糊),只需适应您的需要:
import numpy as np
from pylab import *
# Generate random data with a "bright spot"
N = 100
line = np.linspace(-3,3,N)
X, Y = meshgrid(line,line)
Z = np.exp(-((X+1)**2+(Y-1)**2))
Z += np.random.random(Z.shape)*.5
subplot(121)
imshow(Z,cmap=gray(), origin="lower", extent=[-3,3,-3,3])
Z2 = Z.copy()
# Identify regions that are brighter than threshold on z_scale
threshold = .8
idx = Z2>threshold
Z2[~idx] = None
Z2[idx ] = 1
subplot(122)
imshow(Z2,cmap=gray(), origin="lower", extent=[-3,3,-3,3])
# Place a dot at the "center" of the pixels found
CM = [X[idx].mean(), Y[idx].mean()]
scatter(*CM, s=100,color='red')
show()