gpt4 book ai didi

python - 从 numpy 数组计算椭圆内的像素

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:36 27 4
gpt4 key购买 nike

背景我正在编写一个程序来计算星系的表面亮度作为椭圆半径的函数。这首先涉及读取一个 .fits 文件,该文件存储在一个 numpy 数组中,这样 array[x][y] 将返回该 (x,y) 像素处的表面亮度值。

要计算表面亮度,我需要能够以某个最小尺寸将椭圆拟合到星系,并找到该椭圆内的表面亮度中值,然后增加椭圆的尺寸并找到每个环的表面亮度。不同大小的环将循环通过,直到表面亮度下降到与背景噪声的特定比率以下。

问题给定的椭圆参数包括位置角、x 和 y 像素的中心坐标以及 B/A 比率。我很难找到任何一种方法来让我将一个椭圆拟合到一个数组数组中。请帮助??

最佳答案

我想我可能在涉及劳厄衍射图案( Material 科学)的研究项目中处理过与您的问题类似的问题。我的任务是在给定每个峰的中心坐标的情况下,找出衍射图中每个峰的长度、宽度和倾斜角。我的解决方案是在峰值和阈值、过滤器等周围选择一个感兴趣的区域,这样子图像中只有一个峰值。然后我构建了一个函数来将这些参数拟合到生成的椭圆中:

from xml.etree.cElementTree import parse
import numpy as np
from os import listdir, getcwd
from scipy import ndimage
import h5py
import multiprocessing
import time
#from dicttoxml import dicttoxml
#from xml.dom.minidom import parseString
import sys
import cPickle as pickle
from threading import Thread
from skimage.measure import moments

def fitEllipse(data):
'''
Returns the length of the long and short axis and the angle measure
of the long axis to the horizontal of the best fit ellipsebased on
image moments.

usage: longAxis, shortAxis, angle = fitEllipse(N_by_M_image_as_array)
'''
# source:
# Kieran F. Mulchrone, Kingshuk Roy Choudhury,
# Fitting an ellipse to an arbitrary shape:
# implications for strain analysis, Journal of
# Structural Geology, Volume 26, Issue 1,
# January 2004, Pages 143-153, ISSN 0191-8141,
# <http://dx.doi.org/10.1016/S0191-8141(03)00093-2.>
# Lourena Rocha, Luiz Velho, Paulo Cezar P. Carvalho
# Image Moments-Based Structuring and Tracking of
# Objects, IMPA-Instituto Nacional de Matematica Pura
# e Aplicada. Estrada Dona Castorina, 110, 22460
# Rio de Janeiro, RJ, Brasil,
# <http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1167130>

m = moments(data, 2) # super fast compated to anything in pure python
xc = m[1,0] / m[0,0]
yc = m[0,1] / m[0,0]
a = (m[2,0] / m[0,0]) - (xc**2)
b = 2 * ((m[1,1] / m[0,0]) - (xc * yc))
c = (m[0,2] / m[0,0]) - (yc**2)
theta = .5 * (np.arctan2(b, (a - c)))
w = np.sqrt(6 * (a + c - np.sqrt(b**2 + (a-c)**2)))
l = np.sqrt(6 * (a + c + np.sqrt(b**2 + (a-c)**2)))
return l, w, theta

我只是把它放在一起,以防它与您要找的东西相似。如果您需要更多解释,请随时发表评论。我使用的来源(数学)在评论中。

关于python - 从 numpy 数组计算椭圆内的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31163133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com