scikit-image: Image processing in Python

Marianne Corvellec

Wajam

@PyLadiesMTL

In [1]:
import skimage
In [2]:
skimage.__version__
Out[2]:
'0.9.1'
In [3]:
from matplotlib import pyplot as plt
In [4]:
import numpy as np
In [5]:
bright_raw = plt.imread('Pos60051.png')
In [6]:
plt.imshow(bright_raw)
Out[6]:
<matplotlib.image.AxesImage at 0xa4e826c>
In [7]:
bright_raw.dtype
Out[7]:
dtype('float32')
In [8]:
bright_raw.shape
Out[8]:
(1536, 2048, 3)

Filter

In [9]:
from skimage.filter import gaussian_filter
In [10]:
bright_blurred = gaussian_filter(bright_raw, sigma=30)
/usr/local/lib/python2.7/dist-packages/scikit_image-0.9.1-py2.7-linux-i686.egg/skimage/filter/_gaussian.py:96: RuntimeWarning: Images with dimensions (M, N, 3) are interpreted as 2D+RGB by default. Use `multichannel=False` to interpret as  3D image with last dimension of length 3.
  warnings.warn(RuntimeWarning(msg))
In [11]:
plt.imshow(bright_blurred)
Out[11]:
<matplotlib.image.AxesImage at 0xa82c38c>
In [12]:
np.max(bright_blurred)
Out[12]:
0.18542657233256457
In [13]:
np.max(bright_raw)
Out[13]:
0.99610895
In [14]:
# skimage.filter.

Morphology & Measure

In [15]:
from skimage.morphology import label
In [16]:
# Threshold
bright_regions = bright_blurred > 0.12
In [17]:
bright_regions.dtype
Out[17]:
dtype('bool')
In [18]:
np.unique(bright_regions[:, :, 2])
# Only G channel contains data
Out[18]:
array([False], dtype=bool)
In [19]:
label_regions = label(bright_regions[:, :, 1], neighbors=4, background=0)
In [20]:
label_regions.dtype
Out[20]:
dtype('int32')
In [21]:
# Number of labels
np.unique(label_regions)
# or, simply,
np.max(label_regions)
# + 1
Out[21]:
4
In [22]:
# plt.imshow(label_regions)
plt.colorbar(plt.imshow(label_regions))
Out[22]:
<matplotlib.colorbar.Colorbar instance at 0xa996a0c>
In [23]:
from skimage import measure
In [24]:
intensity_regions = measure.regionprops(label_regions, ['MeanIntensity'], intensity_image=bright_raw[:, :, 1])
In [25]:
len(intensity_regions)
Out[25]:
4
In [26]:
coord_regions = measure.regionprops(label_regions, ['coords'], intensity_image=bright_raw[:, :, 1])
In [27]:
coord_regions[0]['coords']
Out[27]:
array([[291, 374],
       [291, 375],
       [291, 376],
       ..., 
       [357, 431],
       [357, 432],
       [357, 433]])
In [28]:
np.where(label_regions==0)
Out[28]:
(array([ 0,  0,  0, ..., 25, 25, 25]),
 array([384, 385, 386, ..., 416, 417, 418]))
In [29]:
for i in xrange(len(intensity_regions)):
    print(intensity_regions[i]['MeanIntensity'])
0.205465755261
0.155044210443
0.275592330443
0.187728478964

Cross-correlation for tracking

In [30]:
from skimage.feature import match_template
In [31]:
np.where(label_regions==4)
Out[31]:
(array([721, 721, 721, ..., 783, 783, 783]),
 array([819, 820, 821, ..., 803, 804, 805]))
In [32]:
region_4 = bright_raw[720:780, 780:840]
In [33]:
plt.imshow(region_4)
Out[33]:
<matplotlib.image.AxesImage at 0xadc09ac>
In [34]:
region_matching = match_template(bright_raw[:, :, 1], region_4[:, :, 1])
In [35]:
plt.imshow(region_matching)
Out[35]:
<matplotlib.image.AxesImage at 0xae3f0ac>
In [36]:
plt.gray()
In [37]:
plt.imshow(region_matching)
Out[37]:
<matplotlib.image.AxesImage at 0xafc6f4c>
In [38]:
plt.imshow(plt.imread('longtime_z006.png'))
plt.axis('off')
Out[38]:
(-0.5, 2399.5, 1174.5, -0.5)

Edge detection, Segmentation, ...

--