Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
different ways to calculate black pixel ratio in a binray image
>>> image
array([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 1],
[0, 0, 1, 0, 0],
[1, 0, 0, 1, 0]])
>>> black_pixel_count = (image == 0).sum()
>>> total_pixel_count
20
>>> total_pixel_count = image.shape[0] * image.shape[1]
>>> total_pixel_count
20
>>> black_pixel_ratio = black_pixel_count / float(total_pixel_count)
>>> black_pixel_ratio
0.65000000000000002
>>> import collections
>>> k, v = np.unique(y, return_counts=True)
>>> k
array([0, 1])
>>> v
array([13, 7])
>>> d = dict(zip(k,v))
>>> if 0 not in d:
... d[0] = 0
...
>>> if 1 not in d:
... d[1] = 0
...
>>>
>>> d
{0: 13, 1: 7}
>>> d[0]
13
>>> d[1]
7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment