Python numpy counts the number of 0 elements in the array

Using numpy's own method to count the number of 0 elements in the array or the number of 0 pixel values ​​​​in the image is much better than traversing directly.

import numpy as np
a = np.array([[0,1,2],[2,0,0]])
cnt_array = np.where(a,0,1)
print(np.sum(cnt_array))

The elements equal to 0 are set to 1, and the sum is enough.

Guess you like

Origin blog.csdn.net/qq_36276587/article/details/120354138