寻找图像中最亮像素的坐标并将其输入数组

2024-04-30 02:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我被要求编写一个程序,通过将图像文件转换为numpy数组并生成图像中最亮像素的坐标数组(表示背景干扰),来查找图像中的“星星”。 一旦我找到了图像中最亮的像素,我必须记录它的x,y坐标,并将该像素和周围10X10像素区域的值设置为零,从而有效地从图像中移除恒星。 我已经有了一个助手代码,可以将图像转换成数组,并试图解决以下问题:

我定义了一个变量

    Max = array.max()

使用while循环

    while Max >= threshold
        coordinates = numpy.where(array == Max) # find the maximum value

不过,我想让它在所有坐标的整个数组中循环,而不仅仅是找到第一个最大值,并且在找到最大值时删除每个最大值,并将周围的10X10区域设置为零。我考虑过使用for循环来完成这项工作,但由于我对Python还不熟悉,所以不确定应该如何使用它。

我很感激你的建议, 谢谢


Tags: 代码图像程序numpy区域定义图像文件助手
2条回答

有很多不同的方法来做它,只有核,等等

有一种“暴力”的方式:

import Image
import numpy as np

im = Image.open('test.bmp')
data = np.array(im)
threshold = 200
window = 5 # This is the "half" window...
ni, nj = data.shape 
new_value = 0

for i, j in zip(*np.where(data > threshold)):
    istart, istop = max(0, i-window), min(ni, i+window+1)
    jstart, jstop = max(0, j-window), min(nj, j+window+1)
    data[istart:istop, jstart:jstop] = new_value

或者更快的方法。。。

import Image
import numpy as np
import scipy.ndimage

im = Image.open('test.bmp')
data = np.array(im)
threshold = 200
window = 10 # This is the "full" window...
new_value = 0

mask = data > threshold
mask = scipy.ndimage.uniform_filter(mask.astype(np.float), size=window)
mask = mask > 0
data[mask] = new_value

Astronomy.net将为您执行以下操作:

If you have astronomical imaging of the sky with celestial coordinates you do not know—or do not trust—then Astrometry.net is for you. Input an image and we'll give you back astrometric calibration meta-data, plus lists of known objects falling inside the field of view.

We have built this astrometric calibration service to create correct, standards-compliant astrometric meta-data for every useful astronomical image ever taken, past and future, in any state of archival disarray. We hope this will help organize, annotate and make searchable all the world's astronomical information.

你甚至不需要上传图片到他们的网站。您可以下载source。它是在GPL下授权的,并且使用NumPy,因此如果需要的话,可以使用它。

请注意,您需要首先将位图转换为以下格式之一:JPEG、GIF、PNG或FITS image。

相关问题 更多 >