Skip to content

Digital image’s characteristics

A digital image can be seen as a tensor \bold{M} with a size of l * r * c where l is the rows, r is the columns and c 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 l_{i}, r_{j}, c_{k} can have a value s(l_{i}, r_{j}, c_{k}), 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.imread('lenna.png')
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.

m_{s} = \frac{1}{M} \sum_{x = 0}^{l - 1} \sum_{y = 0}^{r - 1} s(x, y)

average_value = np.mean(gray_image)

For the famous image of the swedish model Lenna, we get an average value of 124.

Second measurement, Standard Deviation.

q_{s} = \frac{1}{M} \sum_{x = 0}^{l - 1} \sum_{y = 0}{r - 1} (s(x, y) - ms) ^ 2

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.

p_{s}(g) = \frac{a_{g}}{M}

Since the histogram is normed over the number of pixels M of an image, we can say:

\sum{g = 0}^{255}p_{s}(g) = 1

grey_frequency = [0] * 255
for i in range(gray_image.shape[0]):
  for j in range(gray_image.shape[1]):
    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 H can not be encoded losslessly with fewer than H bits per pixel. We can derivate the entropy of an image from its histogram, taking the log2 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 H = log2(248) \approx 8 bits to encode the image.

H = - \sum_{g = 0}^{k} (p_{s}(g) * log2 p_{s}(g)

marg = np.histogramdd(np.ravel(grey_image), bins = 256)[0] / 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

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *