A digital image can be seen as a tensor with a size of where is the rows, is the columns and indicates the number of channels an image has (Which regularly is three for normal image or four for images with transparent background). A cell at the position can have a value , which indicates which color an individual pixel has on a certain channel. If the number of channels is one (which is the minimum), each pixel’s value presents its brightness from black to white. In this blog, we want to discover mathematical definitions for some characteristics values of an image tensor and their Python implementation. For the sake of simplicity, we will only talk about an image with only one channel, which also appears as a grey image.

# Read image
import cv2
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Transform from 3-channel to 1-channel
import numpy as np
gray_image = np.dot(image, [0.2989, 0.5870, 0.1140]) First measurement, Average, which is a very simple measurement of a tensor, which we all know from school. average_value = np.mean(gray_image)

For the famous image of the swedish model Lenna, we get an average value of Second measurement, Standard Deviation. std = np.mean((gray_image - average_value) ** 2)

For the visualization of pixel value’s distribution, we can employ Histogram, which basically tells us how often a pixel’s value from zero to 255 appears in an image tensor. Since the histogram is normed over the number of pixels of an image, we can say: grey_frequency =  * 255
for i in range(gray_image.shape):
for j in range(gray_image.shape):
grey_frequency[int(gray_image[i][j])] += 1 Last but not least, the Entropy of an image is a number of bits which are needed to display each pixel. An image with an entropy of can not be encoded losslessly with fewer than bits per pixel. We can derivate the entropy of an image from its histogram, taking the of the range of an image, we can roughly round the result up to the entropy. For example, if an image’s histogram has a range from 2 to 250, we will need about bits to encode the image. marg = np.histogramdd(np.ravel(grey_image), bins = 256) / grey_image.size
marg = list(filter(lambda p: p > 0, np.ravel(marg)))
entropy = -np.sum(np.multiply(marg, np.log2(marg)))
Published inSoftware Engineer