如何根据每个子阵列中的白色像素计数对NumPy子阵列进行排序?

时间:2013-08-21 14:15:49

标签: opencv python-2.7 numpy scipy

  img = np.asarray(Image.open("testtwo.tif").convert('L'))# reading and converting    image                         
  img = 1 * (img < 127)

   arraysplit = np.split(img.ravel(), 24) # here we are splitting converted to 1D array

如何在某个顺序中包含包含多个白色像素的子数组?

2 个答案:

答案 0 :(得分:2)

您可以使用key sorted关键字参数来完成此操作:

arraysplit = np.split(img.ravel(), 24)

splits_by_white_count = sorted(arraysplit, key=lambda a: (a == 256).sum())

然后splits_by_white_count将是您的图像数据分割列表,通过增加白色像素数来排序(假设您有8位图像数据)。

如果你只是想要一个白色像素计数列表,那么你可以按顺序排列Christian的解决方案:

white_counts = sorted((a == 256).sum() for a in arraysplit)

答案 1 :(得分:0)

要计算子阵列中的零数,可以执行以下操作:

zeros = [(q==0).sum() for q in arraysplit]