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)
评论列表
文章目录