[英]How to find co-ordinates of lowest pixel in thresholded image?
我是Python和編碼的新手。 我有一個.fits圖像,其中包含具有一定范圍值的像素。 我想在此圖像中找到符合要求的像素:
1)像素值高於閾值6900。
2)如果滿足(1),則像素具有最低的y坐標。
也就是說,如果我發現我的圖像有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坐標?
非常感謝!
您可以找到所有值都高於閾值的索引,然后對它們進行排序並返回最小的一個
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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.