简体   繁体   English

如何对图像应用阈值

[英]How to apply threshold to image

I have an image and would like to use the thresholding to reduce its noise.我有一张图像,想使用阈值来减少它的噪音。 I try to flatten its grid into 1D array, and then would like to use 0 to replace the pixel's actual intensity if it is below the threshold, finally reshape it into the original grid.我尝试将其网格展平为一维数组,然后如果低于阈值则想使用 0 替换像素的实际强度,最后将其重塑为原始网格。 Here is my code:这是我的代码:

Threshold_flat = np.array(Threshold_flat)
Threshold = Threshold_flat.flatten()

Threshold_1 = []
for i in range(len(Threshold)):           
        x = [i if i>20 else 0 for i in Threshold]        
        Threshold_1.append(x)
        
Threshold_1 = np.array(Threshold_1)
Threshold_1 = Threshold_1.reshape(326,481) 

But it doesn't work when I run the code, and the processing is also very slow.但是当我运行代码时它不起作用,并且处理也很慢。

I am confused about it and have no idea how to resolve it.我对此感到困惑,不知道如何解决它。

Thanks in advance for any help!提前感谢您的帮助!

np.where does exactly what you're looking for: np.where完全符合您的要求:

output = np.where(Threshold > 20, Threshold, 0)

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

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