2017-07-31 88 views
1

我是新来的Python和编码。我有一个.fits图像,其中包含一系列值的像素。我想找到这个图像在以下要求的像素:如何找到阈值图像中最低像素的坐标?

1)个像素的值是上述的6900

2)阈值。如果(1)被满足时,像素具有尽可能低的y坐标-ordinates。这就是说,如果我发现我的图像有100个像素值> 6900,我希望找到那些看起来最靠近图像底部的100个像素。

我已通过添加我在下面包括了阈值规则实现的第一部分:

#open .fits file 
hdulist = fits.open(f, ignore_missing_end=True)  
hdulist.info() 

#close file and save data as numpy array 
scidata = hdulist[0].data 
hdulist.close() 
img = np.array(scidata) 

#determine threshold value and apply thresholding to image 
threshold = 6900 
test = np.greater_equal(img, threshold) 
np.count_nonzero(~np.isnan(test)) 

#plot image 
plt.imshow(img, cmap='magma') 
plt.show() 

然而,我有在实现(2)的难度。在numpy中是否有一个命令,用于确定某个阈值以上的最小可能的y像素坐标?

非常感谢!

+0

您可以尝试调换numpy的阵列,这一切铸造列表的列表,然后在每个迭代调用''col_list.index(value)'在阈值后的最小值上调用列。 – MLavrentyev

+0

1.问题:你有'nan'在你阵列吗?一行表明这一点,但这条线并没有摆脱'nan's。 2.“img”的形状是什么?我认为这是二维的。如果是这样,最小值可能不会被定义。你想要每个不同x值的最小值,还是你希望所有最小值都具有相同(最小)的y值? –

+0

1.我的数组中没有'nan'值 - 我认为'np.count_nonzero(〜np.isnan(test))'这行会设置为'nan'低于阈值的所有像素,尽管if我已经将'test'定义为阈值测试,该行可能没有用处。 –

回答

0

你可以找到与上述阈值的所有索引,然后整理这些并返回最小的一个

import numpy as np 

# Generate random data 
img = 10000 * np.random.rand(10, 10) 

# Find indices where the condition is met 
indices = np.where(img > 6900) 

# sort by y first then x 
sorted_indices = sorted((tpl for tpl in zip(*indices)), 
         key=lambda x: (x[1], x[0])) 

result = sorted_indices[0]