【发布时间】:2016-07-08 17:25:14
【问题描述】:
我一直在尝试找到一种更有效的方法来遍历图像并在阈值上分割它们的属性。在网上搜索并与一些编程朋友讨论时,他们向我介绍了向量化(特别是使用 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
基本上,每个像素的亮度值都在 0 到 30000 之间,我试图分别平均低于 20000 和高于 20000 的所有像素。我知道如何做到这一点的最好方法是使用 for 循环(在 python 中很慢)并使用 if 语句搜索每个像素。
【问题讨论】:
-
你的缩进不正确
-
感谢 marko,我在发布后注意到了这一点。我刚刚在 C++ 中输入了一个代码,却忘了仔细检查。
标签: python arrays numpy image-processing vectorization