sample.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:groundfailure 作者: usgs 项目源码 文件源码
def sampleNoPoints(sampleimg, N, xvar, yvar):
    '''
    Sample from our "no" sample grid, where that grid contains 1s where no
    pixels should be sampled from, and 0s where they should not.

    Args:
        sampleimg: Grid of shape (len(yvar),len(xvar)) where 1's represent
            pixels from which "no" values can be sampled.
        N: Number of pixels to sample (without replacement from sampleimg.
        xvar: Numpy array of centers of columns of sampling grid.
        yvar: Numpy array of centers of rows of sampling grid.

    Returns:
        Tuple of
          - nopoints sequence of x,y tuples representing "no" samples.
          - Modified version of input sampleimg, with nopoints pixels set to 0.

    '''

    # get N points from sampleimg without replacement
    # avoid nosampleidx indices
    # return an sequence of X,Y tuples from those indices
    npixels = len(xvar) * len(yvar)
    allidx = np.arange(0, npixels)
    sampleimg = sampleimg.flatten()  # flatten out the input image
    sampleidx = np.random.choice(allidx[sampleimg == 1], size=N, replace=False)
    sampleidx.sort()
    sampleimg[sampleidx] = 0
    sampleimg.shape = (len(yvar), len(xvar))
    sampley, samplex = np.unravel_index(sampleidx, sampleimg.shape)
    xp = xvar[samplex]
    yp = yvar[sampley]
    nopoints = list(zip(xp, yp))
    return (nopoints, sampleimg, sampleidx)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号