2017-07-19 101 views
3

我有一列描述所附圖像中所有像素的點。但是,只需要計算圓形區域中的像素。我知道如果這個區域是一個正方形,該如何做到這一點,但試圖用一個圓圈來做這件事有點卡住了。計算值從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

+0

,如果你知道在哪裏的中心是,你知道你的半徑是什麼,變得簡單。我無法理解你的代碼,如果你有這些值 – Nullman

+0

我正確地猜到(3984.9634,4146.6652)是圓心的座標,5664.85124是半徑? –

回答

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)) 
+0

爲了提高效率:在循環外執行'r2 = r ** 2',與循環內的'r2'進行比較。 – jasonharper

+0

@jasonharper誠然,但如果我們真的擔心效率問題,我們應該使用numpy並將其向量化。 – jpkotta

1
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] 
+0

出於某種原因,這兩個都給我的圖像中的所有像素的總價值。 –

+0

顯然,您定義的圓圈**包含**圖像中的所有像素。檢查你的數據。 –

+0

哦,等等,我明白了!謝謝你的工作!我剛寫下了錯誤的半徑。非常感謝! :) –