在Python中,计算距离圆形区域中心一定距离的值

2024-04-25 08:01:07 发布

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

我有一个点的列表,它描述了我附加的图像中的所有像素。但是,只需要计算圆形区域中的像素。我知道如果这个区域是正方形的话该怎么做,但是试着用一个圆来做这个有点困难。你知道吗

以下是我目前掌握的情况:

#Count the photons within a certain region
from collections import Counter

#Read in Data
RA = []
DEC = []

with open('ChandraXraysources2.txt') as f:
     for row in f.readlines():  
        row.strip('\n')
        if not row.startswith("#"):
            spaces=row.split(',')
            RA.append(float(spaces[0]))
            DEC.append(float(spaces[1]))
list_a = RA
list_b = DEC
coord_list=zip(list_a, list_b)


#This is the most important part I think. Above is just reading my data in. 
#Basically what I tried to do was specify the center of the circle, then count 
#only the points a certain distance from that center. 

points=[]
[(x,y) for x,y in coord_list if x==3984.9634 and y==4146.6652]
if i in coord_list:
    d in 5664.85124
    points.append(i)

Image in Which I'm Trying to Count Pixels, Green Circular Region is Where I Want to Count


Tags: theinfrom区域if像素decpoints
2条回答

像这样的

points = []
xc,yc = 3984.9634, 4146.6652
r2 = 5664.85124**2 # radius squared
for x,y in zip(RA,DEC):
    if (x-xc)**2 + (y-yc)**2 < r2:
        points.append((x,y))
coord_list = []
with open('ChandraXraysources2.txt') as f:
     for row in f:
        if not row.startswith("#"):
            coord_list.append(map(float, row.split(',')))
points = [(x,y) for (x,y) in coord_list if (x-3984.9634)**2 + (y-4146.6652)**2 <= 5664.85124**2]

相关问题 更多 >