Skip to content

Instantly share code, notes, and snippets.

@aarshtalati
Last active October 16, 2017 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarshtalati/0a148a840e3de129e26eb8648c06257d to your computer and use it in GitHub Desktop.
Save aarshtalati/0a148a840e3de129e26eb8648c06257d to your computer and use it in GitHub Desktop.
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