All problems
Image Entropy
Problem
Compute the Shannon entropy of a grayscale image in bits.
Build a 256-bin histogram, let p be each non-zero bin probability, then return round(-sum(p * log2(p)), 4). A constant image has entropy 0.0 and an empty image also returns 0.0.
Hint
Only non-zero histogram bins contribute to the sum of -p * log2(p).
Test cases
case 1
in: [[[0,0],[255,255]]]
expected: 1
case 2
in: [[[7,7],[7,7]]]
expected: 0
case 3
in: [[[0,1],[2,3]]]
expected: 2
case 4
in: [[[0,0,0,255]]]
expected: 0.8113
Starter codePython
def image_entropy(image):
# Your code here
pass4 test cases · run your code against them in the editor