简体   繁体   English

如何用从另一个数组中采样的数字填充数组的切片?

[英]How to fill a slice of an array with numbers sampled from another array?

I have a (very) big grayscale image and a mask, both ranging from 0 to 1, stored as numpy arrays.我有一个(非常)大的灰度图像和一个掩码,范围从 0 到 1,存储为 numpy arrays。 The mask is full of 0s and 1s.掩码中充满了 0 和 1。 I would like to turn all 1 pixels in the mask to resemble some noise from the image, which was already sampled to vector p .我想将掩码中的所有 1 个像素变成类似于图像中的一些噪声,这些噪声已经被采样到向量p

I've tried mask[mask == 1] = np.random.choice(p) , but of course it fills the whole area with only one of the values in p .我试过mask[mask == 1] = np.random.choice(p) ,但当然它只用p中的一个值填充整个区域。 I would like to avoid a for loop, and I would like to avoid creating a new image filled entirely from p , as they are really big for my tiny laptop.我想避免 for 循环,并且我想避免创建一个完全由p填充的新图像,因为它们对于我的小型笔记本电脑来说真的很大。

Any ideas?有任何想法吗?

Try to generate the random choice with size :尝试使用size生成随机选择:

mask[mask==1] = np.random.choice(p, size=mask.sum())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM