2017-07-31 86 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]