我试图python
尽可能接近图像中最明显的聚类中心,如下图所示:
在我之前的问题中,我询问了如何获得二维数组的全局最大值和局部最大值,并且给出的答案非常有效。问题是我可以通过对不同 bin 大小获得的全局最大值进行平均得到的中心估计值总是比我用眼睛设置的值稍有偏差,因为我只考虑最大的bin而不是一组最大的 bin (就像用眼睛做的那样)。
我尝试根据我的问题调整这个问题的答案,但事实证明我的图像太嘈杂,以至于该算法无法工作。这是我实现该答案的代码:
import numpy as np
from scipy.ndimage.filters import maximum_filter
from scipy.ndimage.morphology import generate_binary_structure, binary_erosion
import matplotlib.pyplot as pp
from os import getcwd
from os.path import join, realpath, dirname
# Save path to dir where this code exists.
mypath = realpath(join(getcwd(), dirname(__file__)))
myfile = 'data_file.dat'
x, y = np.loadtxt(join(mypath,myfile), usecols=(1, 2), unpack=True)
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
rang = [[xmin, xmax], [ymin, ymax]]
paws = []
for d_b in range(25, 110, 25):
# Number of bins in x,y given the bin width 'd_b'
binsxy = [int((xmax - xmin) / d_b), int((ymax - ymin) / d_b)]
H, xedges, yedges = np.histogram2d(x, y, range=rang, bins=binsxy)
paws.append(H)
def detect_peaks(image):
"""
Takes an image and detect the peaks usingthe local maximum filter.
Returns a boolean mask of the peaks (i.e. 1 when
the pixel's value is the neighborhood maximum, 0 otherwise)
"""
# define an 8-connected neighborhood
neighborhood = generate_binary_structure(2,2)
#apply the local maximum filter; all pixel of maximal value
#in their neighborhood are set to 1
local_max = maximum_filter(image, footprint=neighborhood)==image
#local_max is a mask that contains the peaks we are
#looking for, but also the background.
#In order to isolate the peaks we must remove the background from the mask.
#we create the mask of the background
background = (image==0)
#a little technicality: we must erode the background in order to
#successfully subtract it form local_max, otherwise a line will
#appear along the background border (artifact of the local maximum filter)
eroded_background = binary_erosion(background, structure=neighborhood, border_value=1)
#we obtain the final mask, containing only peaks,
#by removing the background from the local_max mask
detected_peaks = local_max - eroded_background
return detected_peaks
#applying the detection and plotting results
for i, paw in enumerate(paws):
detected_peaks = detect_peaks(paw)
pp.subplot(4,2,(2*i+1))
pp.imshow(paw)
pp.subplot(4,2,(2*i+2) )
pp.imshow(detected_peaks)
pp.show()
这是结果(改变 bin 大小):
显然,我的背景太嘈杂以至于该算法无法工作,所以问题是:我怎样才能使该算法不那么敏感?如果存在替代解决方案,请告诉我。
编辑
按照 Bi Rico 的建议,我尝试在将二维数组传递给局部最大值查找器之前对其进行平滑处理,如下所示:
H, xedges, yedges = np.histogram2d(x, y, range=rang, bins=binsxy)
H1 = gaussian_filter(H, 2, mode='nearest')
paws.append(H1)
这些是 asigma
为 2、4 和 8 的结果:
编辑 2
Amode ='constant'
似乎比nearest
. sigma=2
对于最大的 bin 大小,它以 a 收敛到右中心:
那么,如何获得最后一张图像中显示的最大值的坐标?