Determine the coordinates of local maximas in a two-dimensional array using derivative
我有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import numpy as np import scipy.nimage as ndimage from astropy.wcs import WCS from astropy import units as u from astropy import coordinates as coord from astropy.io import fits import scipy.ndimage.filters as filters from scipy.ndimage.filters import maximum_filter hdulist=fits.open("MapSNR.fits") #reading a two dimensional array from fits file d=hdulist[0].data w=WCS("MapSNR.fits") idx,idy=np.where(d==np.max(d)) rr,dd=w.all_pix2word(idx,idy,o) c=coord.SkyCoord(ra=rr*u.degree, dec=dd*u.degree) #The sky coordinate of the image maximum print c.ra print c.dec |
这就是我如何找到图像的全局最大值的方法,但我想得到具有大于3的意义的局部最大值的坐标。
我在网上查到的是下面这个答案,在我的情况下不能正常工作。更新:我使用了这个函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def detect_peaks(data, threshold=1.5, neighborhood_size=5): 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 # sets values <= threshold as background 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 y_center = (dy.start + dy.stop - 1)/2 x.append(x_center) y.append(y_center) return x,y |
号
我想找到一种使用更好的方法的方法,比如数组中的导数或分而治之的方法。我会选择一个更好的推荐解决方案。
所以我有了这个,使用skimage自适应阈值。希望有帮助:
原件。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from skimage.filters import threshold_adaptive import matplotlib.pyplot as plt from scipy import misc, ndimage import numpy as np im = misc.imread('\Desktop\MapSNR.jpg') # Apply a threshold binary_adaptive = threshold_adaptive(im, block_size=40, offset=-20).astype(np.int) # Label regions and find center of mass lbl = ndimage.label(binary_adaptive) points = ndimage.measurements.center_of_mass(binary_adaptive, lbl[0], [i+1 for i in range(lbl[1])]) for i in points: p = [int(j) for j in i] binary_adaptive[i] += 5 plt.figure() plt.imshow(im, interpolation='nearest', cmap='gray') plt.show() plt.figure() plt.imshow(binary_adaptive, interpolation='nearest', cmap='gray') plt.show() |
产量
氧化镁
改变阈值的参数将对局部极大值的发现位置和极大值的发现数量有很大的影响。
您可以使用photutils.detection.find揤peaks函数,这是photutils检测方法之一。
如果你看photutils.detection.find摼peaks实现,您将看到它使用scipy.ndimage.maximum_过滤器计算最大图像(默认为3x3盒大小的示意图),并找到原始图像等于最大图像的像素。
该功能的其余部分主要用于两件您可能感兴趣的事情: