示例#1
0
def line_sum(image, sumflag="c", nbgr=0, width=0, pow=2.0, tangent=False, compress=1):
    """
    Sum down 'c'olumns or across 'r'ows
    
    This returns the summed data and background
    """
    if sumflag == "c":
        data = image.sum(axis=0)
    elif sumflag == "r":
        data = image.sum(axis=1)
    npts = len(data)
    data_idx = num.arange(npts, dtype=float)
    # data_err  = data**(0.5)

    ### compute background
    bgr = background(data, nbgr=nbgr, width=width, pow=pow, tangent=tangent, compress=compress)

    return (data, data_idx, bgr)
示例#2
0
def line_sum(image,sumflag='c',nbgr=0,width=0,pow=2.,tangent=False,compress=1):
    """
    Sum down 'c'olumns or across 'r'ows
    
    This returns the summed data and background
    """
    if sumflag == 'c':
        data = image.sum(axis=0)
    elif sumflag == 'r':
        data = image.sum(axis=1)
    npts     = len(data)
    data_idx = num.arange(npts,dtype=float)
    #data_err  = data**(0.5)

    ### compute background
    bgr = background(data,nbgr=nbgr,width=width,pow=pow,tangent=tangent,
                     compress=compress)
    
    return (data, data_idx, bgr)
示例#3
0
def image_bgr(image,lineflag='c',nbgr=3,width=100,pow=2.,tangent=False,
              nline=1,filter=False,compress=1,plot=False):
    """
    Calculate a 2D background for the image.

    Parameters:
    -----------
    * image is the (hopefully clipped) image data

    * lineflag ('c' or 'r') corresponds to to direction which is
      used to generate the background 

    * nbgr = number of end points to use in linear background determination
      (see background.background)
          
    * width should correspond roughly to the actual peak
      width in the lineflag direction. The background should fit
      features that are in general broader than this value
      Note that width = 0 corresponds to no polynomial bgr
      
    * pow is the power of the polynomial used in background determination
      (see background.background)
      
    * tangent is a flag to indicate if local slope of the data should be fitted 
      (see background.background)

    * nline = number of lines to average for each bgr line fit

    * filter (True/False) flag indicates if a spline filter should be applied
      before background subtraction.  (see scipy.ndimage.filters)

    * compress is a factor by which the number of points in each line is
      reduced.  This helps speed up the background fits.  
     
    * plot is a flag to indicate if a 'plot' should be made
    """
    bgr_arr = num.zeros(image.shape)

    # note this works poorly if the filter removes
    # too much intensity.  Use with caution!
    if filter == True:
        #image = ndimage.laplace(image)
        #print 'spline filter'
        image = ndimage.interpolation.spline_filter(image,order=3)

    # fit to rows
    if lineflag=='r':
        if nline > 1:
            ll = int(nline/2.)
            n = image.shape[0]
            line = num.zeros(len(image[0]))
            for j in range(n):
                idx = [k for k in range(j-ll,j+ll+1) if k>=0 and k<n]
                line = line * 0.0
                for k in idx:
                    line = line + image[k]
                line = line/float(len(idx))
                bgr_arr[j,:] = background(line,nbgr=nbgr,width=width,pow=pow,
                                          tangent=tangent,compress=compress)
        else:
            for j in range(image.shape[0]):
                bgr_arr[j,:] = background(image[j],nbgr=nbgr,width=width,pow=pow,
                                          tangent=tangent,compress=compress)

    # fit to cols
    if lineflag=='c':
        if nline > 1:
            ll = int(nline/2.)
            n = image.shape[1]
            line = num.zeros(len(image[:,0]))
            for j in range(n):
                idx = [k for k in range(j-ll,j+ll+1) if k>=0 and k<n]
                line = line * 0.0
                for k in idx:
                    line = line + image[:,k]
                line = line/float(len(idx))
                bgr_arr[:,j] = background(line,nbgr=nbgr,width=width,pow=pow,
                                          tangent=tangent,compress=compress)
        else:
            for j in range(image.shape[1]):
                bgr_arr[:,j] = background(image[:,j],nbgr=nbgr,width=width,pow=pow,
                                          tangent=tangent,compress=compress)
    #show
    if plot:
        
        pyplot.figure(3)
        pyplot.clf()
        pyplot.subplot(3,1,1)
        pyplot.imshow(image)
        pyplot.title("image")
        pyplot.colorbar()

        pyplot.subplot(3,1,2)
        pyplot.imshow(bgr)
        pyplot.title("background")
        pyplot.colorbar()

        pyplot.subplot(3,1,3)
        pyplot.imshow(image-bgr)
        pyplot.title("image - background")
        pyplot.colorbar()

    return bgr_arr