Skip to content

The code calculate the Shannon entropy of an image, allowing us to measure the level of randomness or color diversity within the image.

License

Notifications You must be signed in to change notification settings

robertoaleman/ShannonEntropyCalculator

Repository files navigation

PHP Class Calculate Shannon Entropy in an Image

Author: Roberto Aleman, ventics.com , license: GNU AGPLv3

Shannon Entropy Level Table

Here is a simplified table to interpret entropy levels in the context of images:
  • 0 to 1 : Very low randomness (little variability in colors).
  • 1 to 3 : Low to moderate randomness (little color diversity).
  • 3 to 5 : Moderate to high randomness (acceptable color diversity).
  • 5 to 7 : High randomness (good color diversity).
  • 7 to 8 : Very high randomness (almost all colors are equally likely).

Explanation of the Code to Calculate Shannon Entropy in an Image

The code calculate the Shannon entropy of an image, allowing us to measure the level of randomness or color diversity within the image. Below is a detailed explanation of how the code works and why it generates scales in the image.

1. Loading the Image

The code begins by loading an image from a PNG file using the imagecreatefrompng() function. This allows the script to work with the image in a format that PHP can manipulate.
php
$this->image = imagecreatefrompng($imagePath);

2. Obtaining Dimensions

The dimensions of the image (width and height) are obtained to iterate over each pixel:
php
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);

3. Calculating the Color Histogram

The next step is to count the frequency of each color in the image. This is done through a loop that traverses each pixel and uses imagecolorat() to get the color index of each pixel. The frequency of each color is stored in an array called $histogram.
php
for ($y = 0; $y < $this->height; $y++) {
    for ($x = 0; $x < $this->width; $x++) {
        $colorIndex = imagecolorat($this->image, $x, $y);
        if (!isset($histogram[$colorIndex])) {
            $histogram[$colorIndex] = 0;
        }
        $histogram[$colorIndex]++;
    }
}

4. Calculating Shannon Entropy

Once the histogram is obtained, Shannon entropy is calculated using the previously mentioned formula. For each color, its probability is calculated, and the entropy formula is applied:
php
foreach ($histogram as $count) {
    $probability = $count / $totalPixels;
    if ($probability > 0) {
        $entropy -= $probability * log($probability, 2);
    }
}

5. Randomness Diagnosis

Finally, a diagnosis about the level of randomness of the image is provided based on the calculated entropy value. This is done by comparing the entropy value against predefined scales that reflect different levels of randomness.
php
if ($entropy < 1) {
    return "Low entropy (little randomness)";
} elseif ($entropy < $maxEntropy / 2) {
    return "Moderate entropy (acceptable level of randomness)";
} elseif ($entropy < $maxEntropy * 0.75) {
    return "High entropy (good randomness)";
} else {
    return "Very high entropy (high randomness)";
}

Why Does It Generate Scales in an Image?

Shannon entropy is used to measure the amount of information or uncertainty in a dataset. In the context of images, this translates to the diversity of colors present.
  • Low Entropy: Indicates that the image has few colors or that the colors are very similar to each other, resulting in a less visually interesting image.
  • High Entropy: Suggests that the image has a wide variety of colors, which can make it more attractive and complex.
The entropy scale allows for classifying images into different categories of randomness, which can be useful in applications such as image compression, quality analysis, and in creating image processing algorithms.

About

The code calculate the Shannon entropy of an image, allowing us to measure the level of randomness or color diversity within the image.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published