[英]How to replace nan values of a column based on certain values of other column
[英]How to replace certain percentage of values with other values in image?
我有一个 RGB 图像,我必须为每个通道分配一定百分比的最亮像素为 255,将一定百分比的最暗像素分配为 0。 有没有一种简单的方法可以在 numpy 中做到这一点?
这是一种通过排序和查找阈值并替换它们来完成的方法,但是如果您正在寻找一种有效的方法,您确实不需要对图像进行排序,您可以使用np.partition
来实现它(您的图像是arr
我假设最后一个维度是针对通道的。更新 0.05 以获得所需的百分比。对于暗点,这将是类似的解决方案,但有细微的变化):
b = arr.reshape(-1,arr.shape[-1])
n = int(0.05 * b.shape[0])
threshold = np.sort(b,0)[n]
arr[arr>threshold] = 100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.