Working with images#

For analysing image data, we need to open them, apply filters to them, segment objects in the image and do measurements.

See also

Opening images#

Most images read using the imread function. In case your image doesn’t you should consult the documentation of the given file format.

from skimage.io import imread

image = imread("blobs.tif")

As shown earlier, images are just matrices of intensities. However, showing them as such is not convenient.

image
array([[ 40,  32,  24, ..., 216, 200, 200],
       [ 56,  40,  24, ..., 232, 216, 216],
       [ 64,  48,  24, ..., 240, 232, 232],
       ...,
       [ 72,  80,  80, ...,  48,  48,  48],
       [ 80,  80,  80, ...,  48,  48,  48],
       [ 96,  88,  80, ...,  48,  48,  48]], dtype=uint8)

As a recap, we can show images using imshow in scikit image.

from skimage.io import imshow

imshow(image)
<matplotlib.image.AxesImage at 0x245e7e5b220>
../_images/02_Working_with_images_5_1.png

Lookup tables (a.k.a. color maps)#

We can also change the look-up table, a.k.a. “color map” for the visualization.

imshow(image, cmap="hot")
<matplotlib.image.AxesImage at 0x245e7fb7520>
../_images/02_Working_with_images_7_1.png
imshow(image, cmap="gray")
<matplotlib.image.AxesImage at 0x245e8019910>
../_images/02_Working_with_images_8_1.png

Image statistics#

As a recap, we can use numpy to determine mean intensity and standard deviation of the image, because it is just a matrix of numbers.

import numpy as np
np.mean(image)
103.26857775590551
np.std(image)
71.05654744558498

The histogram of an image represents its grey value distribution. It tells us if there are bright and dark regions.

number_of_bins = 256
min_max = [0,255]
histogram,bins = np.histogram(image.ravel(),number_of_bins,min_max)
histogram
array([    0,     0,     0,     0,     0,     0,     0,     0,    53,
           0,     0,     0,     0,     0,     0,     0,   304,     0,
           0,     0,     0,     0,     0,     0,  1209,     0,     0,
           0,     0,     0,     0,     0,  3511,     0,     0,     0,
           0,     0,     0,     0,  7731,     0,     0,     0,     0,
           0,     0,     0, 10396,     0,     0,     0,     0,     0,
           0,     0,  7456,     0,     0,     0,     0,     0,     0,
           0,  3829,     0,     0,     0,     0,     0,     0,     0,
        1992,     0,     0,     0,     0,     0,     0,     0,  1394,
           0,     0,     0,     0,     0,     0,     0,  1158,     0,
           0,     0,     0,     0,     0,     0,  1022,     0,     0,
           0,     0,     0,     0,     0,   984,     0,     0,     0,
           0,     0,     0,     0,   902,     0,     0,     0,     0,
           0,     0,     0,   840,     0,     0,     0,     0,     0,
           0,     0,   830,     0,     0,     0,     0,     0,     0,
           0,   926,     0,     0,     0,     0,     0,     0,     0,
         835,     0,     0,     0,     0,     0,     0,     0,   901,
           0,     0,     0,     0,     0,     0,     0,  1025,     0,
           0,     0,     0,     0,     0,     0,  1180,     0,     0,
           0,     0,     0,     0,     0,  1209,     0,     0,     0,
           0,     0,     0,     0,  1614,     0,     0,     0,     0,
           0,     0,     0,  1609,     0,     0,     0,     0,     0,
           0,     0,  2220,     0,     0,     0,     0,     0,     0,
           0,  2037,     0,     0,     0,     0,     0,     0,     0,
        2373,     0,     0,     0,     0,     0,     0,     0,  1568,
           0,     0,     0,     0,     0,     0,     0,  1778,     0,
           0,     0,     0,     0,     0,     0,   774,     0,     0,
           0,     0,     0,     0,     0,  1364,     0,     0,     0,
           0,     0,     0,     0], dtype=int64)
bins
array([  0.        ,   0.99609375,   1.9921875 ,   2.98828125,
         3.984375  ,   4.98046875,   5.9765625 ,   6.97265625,
         7.96875   ,   8.96484375,   9.9609375 ,  10.95703125,
        11.953125  ,  12.94921875,  13.9453125 ,  14.94140625,
        15.9375    ,  16.93359375,  17.9296875 ,  18.92578125,
        19.921875  ,  20.91796875,  21.9140625 ,  22.91015625,
        23.90625   ,  24.90234375,  25.8984375 ,  26.89453125,
        27.890625  ,  28.88671875,  29.8828125 ,  30.87890625,
        31.875     ,  32.87109375,  33.8671875 ,  34.86328125,
        35.859375  ,  36.85546875,  37.8515625 ,  38.84765625,
        39.84375   ,  40.83984375,  41.8359375 ,  42.83203125,
        43.828125  ,  44.82421875,  45.8203125 ,  46.81640625,
        47.8125    ,  48.80859375,  49.8046875 ,  50.80078125,
        51.796875  ,  52.79296875,  53.7890625 ,  54.78515625,
        55.78125   ,  56.77734375,  57.7734375 ,  58.76953125,
        59.765625  ,  60.76171875,  61.7578125 ,  62.75390625,
        63.75      ,  64.74609375,  65.7421875 ,  66.73828125,
        67.734375  ,  68.73046875,  69.7265625 ,  70.72265625,
        71.71875   ,  72.71484375,  73.7109375 ,  74.70703125,
        75.703125  ,  76.69921875,  77.6953125 ,  78.69140625,
        79.6875    ,  80.68359375,  81.6796875 ,  82.67578125,
        83.671875  ,  84.66796875,  85.6640625 ,  86.66015625,
        87.65625   ,  88.65234375,  89.6484375 ,  90.64453125,
        91.640625  ,  92.63671875,  93.6328125 ,  94.62890625,
        95.625     ,  96.62109375,  97.6171875 ,  98.61328125,
        99.609375  , 100.60546875, 101.6015625 , 102.59765625,
       103.59375   , 104.58984375, 105.5859375 , 106.58203125,
       107.578125  , 108.57421875, 109.5703125 , 110.56640625,
       111.5625    , 112.55859375, 113.5546875 , 114.55078125,
       115.546875  , 116.54296875, 117.5390625 , 118.53515625,
       119.53125   , 120.52734375, 121.5234375 , 122.51953125,
       123.515625  , 124.51171875, 125.5078125 , 126.50390625,
       127.5       , 128.49609375, 129.4921875 , 130.48828125,
       131.484375  , 132.48046875, 133.4765625 , 134.47265625,
       135.46875   , 136.46484375, 137.4609375 , 138.45703125,
       139.453125  , 140.44921875, 141.4453125 , 142.44140625,
       143.4375    , 144.43359375, 145.4296875 , 146.42578125,
       147.421875  , 148.41796875, 149.4140625 , 150.41015625,
       151.40625   , 152.40234375, 153.3984375 , 154.39453125,
       155.390625  , 156.38671875, 157.3828125 , 158.37890625,
       159.375     , 160.37109375, 161.3671875 , 162.36328125,
       163.359375  , 164.35546875, 165.3515625 , 166.34765625,
       167.34375   , 168.33984375, 169.3359375 , 170.33203125,
       171.328125  , 172.32421875, 173.3203125 , 174.31640625,
       175.3125    , 176.30859375, 177.3046875 , 178.30078125,
       179.296875  , 180.29296875, 181.2890625 , 182.28515625,
       183.28125   , 184.27734375, 185.2734375 , 186.26953125,
       187.265625  , 188.26171875, 189.2578125 , 190.25390625,
       191.25      , 192.24609375, 193.2421875 , 194.23828125,
       195.234375  , 196.23046875, 197.2265625 , 198.22265625,
       199.21875   , 200.21484375, 201.2109375 , 202.20703125,
       203.203125  , 204.19921875, 205.1953125 , 206.19140625,
       207.1875    , 208.18359375, 209.1796875 , 210.17578125,
       211.171875  , 212.16796875, 213.1640625 , 214.16015625,
       215.15625   , 216.15234375, 217.1484375 , 218.14453125,
       219.140625  , 220.13671875, 221.1328125 , 222.12890625,
       223.125     , 224.12109375, 225.1171875 , 226.11328125,
       227.109375  , 228.10546875, 229.1015625 , 230.09765625,
       231.09375   , 232.08984375, 233.0859375 , 234.08203125,
       235.078125  , 236.07421875, 237.0703125 , 238.06640625,
       239.0625    , 240.05859375, 241.0546875 , 242.05078125,
       243.046875  , 244.04296875, 245.0390625 , 246.03515625,
       247.03125   , 248.02734375, 249.0234375 , 250.01953125,
       251.015625  , 252.01171875, 253.0078125 , 254.00390625,
       255.        ])

Using matplotlibs’ hist function, we can visualize the histogram as plot.

from matplotlib import pyplot as plt

plt.hist(image.ravel(), number_of_bins, min_max)
plt.show()
../_images/02_Working_with_images_17_0.png

Exercise#

Open the banana020.tif data set, visualize it in a yellowish lookup table side by side with its histogram and measure the minimum and maximum intensity of the image.