登录

获取二维数组中高于某一值的局部最大值的坐标

内容来源于 Stack Overflow,遵循 CC BY-SA 4.0 许可协议进行翻译与使用。IT领域专用引擎提供翻译支持

腾讯云小微IT领域专用引擎提供翻译支持

原文
Stack Overflow用户 修改于2012-02-02
  • 该问题已被编辑
  • 提问者: Stack Overflow用户
  • 提问时间: 2012-02-02 19:41
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 ?

恐怕我不是很了解 scipy.ndimage.filters.maximum_filter() 函数。有没有一种方法可以只在斑点内而不在背景内获得像素坐标?

http://i.stack.imgur.com/RImHW.png (16位灰度图片,256*256像素)

浏览 179 关注 0 得票数 30
  • 得票数为Stack Overflow原文数据
原文
修改于2014-03-26
  • 该回答已被编辑
  • 回答者: Stack Overflow用户
  • 回答时间: 2014-03-25 18:22
得票数 13
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)