Get coordinates of local maxima in 2D array above certain value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from PIL import Image import numpy as np from scipy.ndimage.filters import maximum_filter import pylab # the picture (256 * 256 pixels) contains bright spots of which I wanna get positions # problem: data has high background around value 900 - 1000 im = Image.open('slice0000.png') data = np.array(im) # as far as I understand, data == maximum_filter gives True-value for pixels # being the brightest in their neighborhood (here 10 * 10 pixels) maxima = (data == maximum_filter(data,10)) # How can I get only maxima, outstanding the background a certain value, let's say 500 ? |
恐怕我不太了解
http://i.stack.imgur.com/rimhw.png(16位灰度图片,256*256像素)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import numpy as np import scipy import scipy.ndimage as ndimage import scipy.ndimage.filters as filters import matplotlib.pyplot as plt fname = '/tmp/slice0000.png' neighborhood_size = 5 threshold = 1500 data = scipy.misc.imread(fname) data_max = filters.maximum_filter(data, neighborhood_size) maxima = (data == data_max) data_min = filters.minimum_filter(data, neighborhood_size) diff = ((data_max - data_min) > threshold) maxima[diff == 0] = 0 labeled, num_objects = ndimage.label(maxima) slices = ndimage.find_objects(labeled) x, y = [], [] for dy,dx in slices: x_center = (dx.start + dx.stop - 1)/2 x.append(x_center) y_center = (dy.start + dy.stop - 1)/2 y.append(y_center) plt.imshow(data) plt.savefig('/tmp/data.png', bbox_inches = 'tight') plt.autoscale(False) plt.plot(x,y, 'ro') plt.savefig('/tmp/result.png', bbox_inches = 'tight') |
给定的data.png:
在以上
证明人:
- 塞巴斯蒂安计数核
- 乔认定该爪的照片
- 伊凡的局部maximums认定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import numpy as np import scipy import scipy.ndimage as ndimage import scipy.ndimage.filters as filters import matplotlib.pyplot as plt fname = '/tmp/slice0000.png' neighborhood_size = 5 threshold = 1500 data = scipy.misc.imread(fname) data_max = filters.maximum_filter(data, neighborhood_size) maxima = (data == data_max) data_min = filters.minimum_filter(data, neighborhood_size) diff = ((data_max - data_min) > threshold) maxima[diff == 0] = 0 labeled, num_objects = ndimage.label(maxima) xy = np.array(ndimage.center_of_mass(data, labeled, range(1, num_objects+1))) plt.imshow(data) plt.savefig('/tmp/data.png', bbox_inches = 'tight') plt.autoscale(False) plt.plot(xy[:, 1], xy[:, 0], 'ro') plt.savefig('/tmp/result.png', bbox_inches = 'tight') |
《超级有用的输入是以前的我,但是我的slowed回路的应用上。我发现这部ndimage.center _ _轨道()是一个伟大的和固定的工作,把两个坐标。hence这个建议。
这是我现在所做的与skimage。
1 2 | from skimage.feature import peak_local_max xy = peak_local_max(data, min_distance=2,threshold_abs=1500) |
在我的电脑,一个VGA图像尺寸它运行约4倍以上,比《阿姨也返回液和一个更准确的位置在某些用例。