我一直在努力寻找一种更有效的方法来迭代图像并在阈值上拆分它们的属性.在网上搜索并与一些编程朋友讨论时,他们向我介绍了向量化(特别是使用numpy)函数的概念.经过大量的搜索和反复试验,我似乎无法掌握它.有人可以给我一个链接,或建议如何使以下代码更有效?
Im = plt.imread(img)
Imarray = np.array(Im)
for line in Imarray:
for pixel in line:
if pixel <= 20000:
dim_sum += pixel
dim_counter += 1
if pixel > 20000:
bright_sum += pixel
bright_counter += 1
bright_mean = bright_sum/bright_counter
dim_mean = dim_sum/dim_counter
Run Code Online (Sandbox Code Playgroud)
基本上,每个像素保持0到30000之间的亮度,我试图分别平均低于20000和高于20000的所有像素.我知道如何做到这一点的最好方法是使用for循环(在python中很慢)并使用if语句搜索每个像素.