Introduction to image processing#

Numpy is a library for processing arrays and matrices of numerical data. Images are exactly that.

See also

Let’s start by defining an image as a two dimensional array; a matrix.

raw_image_array = [
    [1, 0, 2, 1, 0, 0, 0],
    [0, 3, 1, 0, 1, 0, 1],
    [0, 5, 5, 1, 0, 1, 0],
    [0, 6, 6, 5, 1, 0, 2],
    [0, 0, 5, 6, 3, 0, 1],
    [0, 1, 2, 1, 0, 0, 1],
    [1, 0, 1, 0, 0, 1, 0]
]

We can turn this matrix into a numpy array. Processing numpy arrays is more convenient as introduced in lecture 02.

import numpy as np

image = np.asarray(raw_image_array)
image
array([[1, 0, 2, 1, 0, 0, 0],
       [0, 3, 1, 0, 1, 0, 1],
       [0, 5, 5, 1, 0, 1, 0],
       [0, 6, 6, 5, 1, 0, 2],
       [0, 0, 5, 6, 3, 0, 1],
       [0, 1, 2, 1, 0, 0, 1],
       [1, 0, 1, 0, 0, 1, 0]])

We can also create empty images containing zeros and random images, which is sometimes good for playing with algorithms.

image_size = (5, 10)

np.zeros(image_size)
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
np.zeros((5, 10))
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
np.random.random((3, 5))
array([[0.55460936, 0.65190428, 0.17707292, 0.48021746, 0.71172296],
       [0.59249835, 0.80077   , 0.93618394, 0.71594853, 0.6613176 ],
       [0.28979045, 0.98719214, 0.68702289, 0.51240876, 0.2534302 ]])

Pixel statistics#

Numpy also allows us to derive basic descriptive statistical measurements from images such as mean, minimum, maximum and standard deviation of intensities:

np.mean(image)
1.3265306122448979
np.min(image)
0
np.max(image)
6
np.std(image)
1.8448798987737995
image.std()
1.8448798987737995

Image visualization#

For visualizing images, we use the scikit-image library.

from skimage.io import imshow

imshow(image)
c:\programs\miniconda3\lib\site-packages\skimage\io\_plugins\matplotlib_plugin.py:150: UserWarning: Low image data range; displaying image with stretched contrast.
  lo, hi, cmap = _get_display_range(image)
<matplotlib.image.AxesImage at 0x2024f898730>
../_images/01_Introduction_to_image_processing_16_2.png