Counting pixels inside an ellipse from a numpy array

BACKGROUND I am writing a program for calculating the surface brightness of a galaxy as a function of elliptical radius. At first, this is related to reading in the .fits file, which is stored in the numpy array, so the [x] [y] array will return the surface brightness value on this (x, y) pixel.

To calculate surface brightness, I need to be able to fit the ellipse to the galaxy with a minimum size and find the average surface brightness inside this ellipse, then increase the size of the ellipse and find the surface brightness for each ring. Rings of various sizes will loop until the surface brightness drops below a certain ratio to background noise.

PROBLEM The indicated parameters for the ellipse include position angle, center coordinates for x and y pixels and B / A ratio. I am having trouble finding some method that will allow me to pick up an ellipse essentially an array of arrays. Help me please?

+4
source share
1 answer

, , , , , , ( ). , , . , , .., - . , , :

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

, , . , . , (), .

+1

All Articles