def fixpix2(data, mask, iterations=3, out=None):
    """Substitute pixels in mask by a bilinear least square fitting.
    """
    out = out if out is not None else data.copy()

    # A binary mask, regions are ones
    binry = mask != 0

    # Label regions in the binary mask
    lblarr, labl = ndimage.label(binry)

    # Structure for dilation is 8-way
    stct = ndimage.generate_binary_structure(2, 2)
    # Pixels in the background
    back = lblarr == 0
    # For each object
    for idx in range(1, labl + 1):
        # Pixels of the object
        segm = lblarr == idx
        # Pixels of the object or the background
        # dilation will only touch these pixels
        dilmask = numpy.logical_or(back, segm)
        # Dilation 3 times
        more = ndimage.binary_dilation(segm, stct,
                                       iterations=iterations,
                                       mask=dilmask)
        # Border pixels
        # Pixels in the border around the object are
        # more and (not segm)
        border = numpy.logical_and(more, numpy.logical_not(segm))
        # Pixels in the border
        xi, yi = border.nonzero()
        # Bilinear leastsq calculator
        calc = FitOne(xi, yi, out[xi, yi])
        # Pixels in the region
        xi, yi = segm.nonzero()
        # Value is obtained from the fit
        out[segm] = calc(xi, yi)

    return out
def fixpix2 ( data , mask , iterations = 3 , out = None ) : out = out if out is not None else data . copy ( ) binry = mask != 0 lblarr , labl = ndimage . label ( binry ) stct = ndimage . generate_binary_structure ( 2 , 2 ) back = lblarr == 0 for idx in range ( 1 , labl + 1 ) : segm = lblarr == idx dilmask = numpy . logical_or ( back , segm ) more = ndimage . binary_dilation ( segm , stct , iterations = iterations , mask = dilmask ) border = numpy . logical_and ( more , numpy . logical_not ( segm ) ) xi , yi = border . nonzero ( ) calc = FitOne ( xi , yi , out [ xi , yi ] ) xi , yi = segm . nonzero ( ) out [ segm ] = calc ( xi , yi ) return out
Substitute pixels in mask by a bilinear least square fitting.