Images as Functions

In order to work with images in python we make use of two established python libraries:

MatPlotLib

The installation of matplotlib is done in anaconda using the following command1:

conda install -c conda-forge matplotlib

To get better interactive support in jupyter notebooks, like the one you are seeing, use one of the following two magic command lines after installing the package ipympl. For more infos on matplotlib renderes and jupyter notebook interaction follow these references.

Another useful package is pillow which gives you a larger selection of image file formats (not only only *.png)1.

#%matplotlib inline
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

OpenCV

To install the opencv library with anaconda in a python 3 environment use the following command1

conda install -c menpo opencv3
# import the opencv library
import cv2

# this will print the version of your opencv3
cv2.__version__
'3.1.0'

Reading Images [matplotlib]

An image can be loaded using the imread command from matplotlib.

img = mpimg.imread('norway.jpg')

The output of imread is a numpy array with type uint8

print(img.dtype)
img
uint8





array([[[ 27,  28, 108],
        [ 27,  28, 108],
        [ 27,  28, 108],
        ...,
        [ 23,  24, 104],
        [ 23,  24, 104],
        [ 23,  24, 104]],

       [[ 27,  28, 108],
        [ 27,  28, 108],
        [ 27,  28, 108],
        ...,
        [ 23,  24, 104],
        [ 23,  24, 104],
        [ 23,  24, 104]],

       [[ 27,  28, 108],
        [ 27,  28, 108],
        [ 27,  28, 108],
        ...,
        [ 23,  24, 104],
        [ 23,  24, 104],
        [ 23,  24, 104]],

       ...,

       [[161, 119, 143],
        [158, 116, 140],
        [157, 115, 139],
        ...,
        [197, 156, 150],
        [194, 153, 147],
        [191, 150, 144]],

       [[122,  87, 119],
        [117,  82, 114],
        [120,  85, 115],
        ...,
        [199, 142, 148],
        [188, 132, 135],
        [175, 119, 122]],

       [[ 84,  54,  92],
        [ 83,  53,  89],
        [ 89,  59,  95],
        ...,
        [199, 137, 140],
        [182, 118, 119],
        [179, 115, 115]]], dtype=uint8)

Reading Images [opencv]

To get started with image processing in opencv read the py image display guide. To load an image in opencv, one uses the imread command, which returns also a numpy array.

# Load an color image in grayscale
img = cv2.imread('norway.jpg')
img
array([[[108,  28,  27],
        [108,  28,  27],
        [108,  28,  27],
        ...,
        [104,  24,  23],
        [104,  24,  23],
        [104,  24,  23]],

       [[108,  28,  27],
        [108,  28,  27],
        [108,  28,  27],
        ...,
        [104,  24,  23],
        [104,  24,  23],
        [104,  24,  23]],

       [[108,  28,  27],
        [108,  28,  27],
        [108,  28,  27],
        ...,
        [104,  24,  23],
        [104,  24,  23],
        [104,  24,  23]],

       ...,

       [[143, 119, 161],
        [140, 116, 158],
        [139, 115, 157],
        ...,
        [150, 156, 197],
        [147, 153, 194],
        [144, 150, 191]],

       [[119,  87, 122],
        [114,  82, 117],
        [115,  85, 120],
        ...,
        [148, 142, 199],
        [135, 132, 188],
        [122, 119, 175]],

       [[ 92,  54,  84],
        [ 89,  53,  83],
        [ 95,  59,  89],
        ...,
        [140, 137, 199],
        [119, 118, 182],
        [115, 115, 179]]], dtype=uint8)

Image Size

A color image usually consists of three dimensions

\[M \times N \times D\]

where $M$ is the number of rows, $N$ the number of columns and $D$ is the number of color channels or layers.

The number of dimensions of a numpy array can be found using ndim.

nImgDim = img.ndim
print('Image dimensions: ', nImgDim)
Image dimensions:  3

The width, height and number of channels of an image can be found using python’s len command.

nImgHeight = len(img)
print('Image height: ', nImgHeight)
Image height:  639
nImgHeight = len(img[:,1,:])
print('Image height: ', nImgHeight)
Image height:  639
nImgWidth = len(img[0])
print('Image width: ', nImgWidth)
Image width:  960
nImgWidth = len(img[1,:,:])
print('Image width: ', nImgWidth)
Image width:  960
nChannels = len(img[1,1,:])
print('Channels: ', nChannels)
print('Color values: ', img[1,1,:])
Channels:  3
Color values:  [ 27  28 108]
ImgShape = np.shape(img)
print('Numpy ', type(ImgShape), ' shape output: ', ImgShape)

Numpy  <class 'tuple'>  shape output:  (639, 960, 3)

The storage size of the image can be computed using the image dimensions and the size of a single pixel, which is 8 byte in this case because of the type uint8.

print('Image size:', 639*960*3*8, 'bits')
print('Image size:', 639*960*3*8/8/1024, 'kilobyte')
Image size: 14722560 bits
Image size: 1797.1875 kilobyte

However, the actual size of the png image is smaller due to compression.

import sys
print('Image size:', sys.getsizeof(img), 'byte')
Image size: 128 byte

Display Images

To display an image in opencv one would use the following command:

cv2.imshow('image',img)

However, it is common to display images using matplotlib especially in jupyter notebooks. Plotting images loaded with opencv using matplotlib is also mentioned in the opencv image tutorial.

Warning Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV.

imgplot = plt.imshow(img)
<IPython.core.display.Javascript object>

imgplot = plt.imshow(img[200:500,200:500,:])
<IPython.core.display.Javascript object>

# create the x and y coordinate arrays (here we just use pixel indices)
xx, yy = np.mgrid[0:img.shape[0], 0:img.shape[1]]
xx
array([[  0,   0,   0, ...,   0,   0,   0],
       [  1,   1,   1, ...,   1,   1,   1],
       [  2,   2,   2, ...,   2,   2,   2],
       ...,
       [636, 636, 636, ..., 636, 636, 636],
       [637, 637, 637, ..., 637, 637, 637],
       [638, 638, 638, ..., 638, 638, 638]])
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(xx, yy, img[:,:,0], cmap=plt.cm.gray)
<IPython.core.display.Javascript object>

<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x118e67240>

Comments