繁体   English   中英

如何找到阈值图像中最低像素的坐标?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM