I tried to find the entropy of the image with python

Introduction

Recently, I became a specialist in image processing in a university laboratory, and I had the opportunity to review the entropy of images, so I summarized it.

What is entropy?

Entropy stands for "messiness" and can also be found in thermodynamics and statistical mechanics. Entropy is also used as an indicator of the amount of information in an image.

The definition of image entropy is as follows. If the tone (level) of the image is a K-value image with 0 to (k-1) and the probability of appearance of level $ i $ is $ P_i , the amount of information per pixel is $ I = log_2(1/P_i) = -log_2P_i [bit] $$ Since the amount of information on the entire screen is entropy, if the total number of pixels is $ N $ and the number of pixels at level $ i $ is $ N_i $, $ P_i $ is $ P_i = N_i/N $ Entropy $ H =-\ sum P_ilog_2P_i [bit / pixel] $

Find the entropy function of a binary image (black and white image)

I tried to draw the entropy function of the binary image.

import numpy as np
import matplotlib.pyplot as plt

P0 = np.arange(0,1, 0.01) #Level 0 appearance probability P0
P1 = 1 - P0 #Level 1 appearance probability P1

#Calculation of entropy
H = -P0 * np.log2(P0) -P1 * np.log2(P1)

#Graph drawing settings
plt.plot(P0, H)
plt.xlabel("P0")
plt.ylabel("H")
plt.title('Entropy')

#Label drawing
plt.legend()
#Graph drawing execution
plt.show()

Execution result From the above figure, it can be seen that the closer to $ P_0 = 0.0 $ or $ P_0 = 1.0 $, the smaller the entropy, and when $ P_0 = 0.5 $, the entropy becomes maximum. In other words, the closer it is to white or black, the smaller the entropy, and when the probability of appearance of white and black is 50% each, the entropy increases.

This means that the more complicated the color usage of an image, the greater the amount of information, and the simpler the color usage of an image, the smaller the amount of information, which is intuitive.

Find the entropy of a black and white image

Next, let's find the entropy of Lenna's image in black and white.

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('./img_data/lena_gray.jpg') #Please change the file bus accordingly

#Calculation of histogram (number of pixels of each color)
histgram = [0]*256
for i in range(256):
    for j in range(256):
        histgram[img[i, j, 0]] += 1

#Calculation of entropy
size = img.shape[0] * img.shape[1]
entropy = 0

for i in range(256):
    #Probability of appearance of level i p
    p = histgram[i]/size
    if p == 0:
        continue
    entropy -= p*math.log2(p)

plt.imshow(img)
print('Entropy:{}'.format(entropy))

Execution result

Summary

I could only write the super basics of entropy, but I'm glad I was able to review it through programming. If you have any mistakes, please point them out.

Recommended Posts

I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to find the average of the sequence with TensorFlow
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried to correct the keystone of the image
I tried "binarizing" the image with Python + OpenCV
I tried to improve the efficiency of daily work with Python
I tried to get the authentication code of Qiita API with Python.
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried to touch the CSV file with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to solve the soma cube with python
I tried to solve the problem with Python Vol.1
I tried to summarize the string operations of Python
[Python] I tried to judge the member image of the idol group using Keras
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to simulate how the infection spreads with Python
[Python] I tried to visualize the follow relationship of Twitter
I tried to divide the file into folders with Python
I tried playing with the image with Pillow
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to put out the frequent word ranking of LINE talk with Python
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to find out how to streamline the work flow with Excel x Python ①
I tried to find out how to streamline the work flow with Excel x Python ③
I tried to compare the processing speed with dplyr of R and pandas of Python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to solve the ant book beginner's edition with python
I tried to automate the watering of the planter with Raspberry Pi
I tried to build the SD boot image of LicheePi Nano
I want to output the beginning of the next month with Python
I tried to create a list of prime numbers with python
I tried to process the image in "sketch style" with OpenCV
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to find out if ReDoS is possible with Python
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to make an image similarity function with Python + OpenCV
PhytoMine-I tried to get the genetic information of plants with Python
How to write offline real time I tried to solve the problem of F02 with Python
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried hundreds of millions of SQLite with python
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried to save the data with discord
I tried to find 100 million digits of pi
I tried to touch the API of ebay
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to automate sushi making with python
I tried using the image filter of OpenCV
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!