Několik odkazů do začátku

Pusťme si ipython notebook

In [1]:
%pylab inline

# from pylab import *
# import cStringIO
import urllib
import scipy
import scipy.misc
import skimage
import skimage.data
from skimage.filters import threshold_otsu, gaussian_filter
# from skimage.filter import threshold_otsu, gaussian_filter
from skimage.morphology import label
from scipy.ndimage.morphology import binary_closing, binary_erosion, binary_opening, binary_dilation

from skimage.measure import regionprops
from skimage.color import label2rgb
from skimage.io import imread
# import skimage.filter
# from skimage.filters import 
Populating the interactive namespace from numpy and matplotlib

Trocha Pythonu

In [3]:
print("hello")
hello
In [4]:
def mojeFunkce(vstup):
    vystup = vstup + 6
    return vystup

mojeFunkce(5)
Out[4]:
11
In [5]:
for i in range(2,5):
    print(i)
2
3
4
In [6]:
pole = ['Franta', 'Jakub', 'Marta']

for jmeno in pole:
    print(jmeno)
Franta
Jakub
Marta

Načtení a zobrazení obrázku z URL

In [19]:
URL = "http://plzen.cz/cameraFeed.php"
img = imread(URL)

imshow(img)
# show()
Out[19]:
<matplotlib.image.AxesImage at 0x150e9498470>
In [20]:
plt.figure(figsize=[10, 5])
subplot(131)
imshow(img[:, :, 0], cmap="Reds")
plt.axis("off")
subplot(132)
imshow(img[:, :, 1], cmap="Greens")
plt.axis("off")
subplot(133)
imshow(img[:, :, 2], cmap="Blues")
plt.axis("off")
img.shape
Out[20]:
(960, 1280, 3)

Zobrazení obrázku z kamery

In [66]:
URL = "http://uc452cam01-kky.fav.zcu.cz/snapshot.jpg"
In [67]:
img = imread(URL, as_grey=True)
imshow(img, cmap='gray')
Out[67]:
<matplotlib.image.AxesImage at 0x150f1df70f0>
In [31]:
# ukazkova data
img = skimage.data.coins() / 255.0
imshow(img, cmap='gray')
Out[31]:
<matplotlib.image.AxesImage at 0x150e9cf50b8>

Práce s obrazovými daty

In [32]:
img.shape
Out[32]:
(303, 384)
In [33]:
img[50, 10]
Out[33]:
0.43529411764705883
In [34]:
imgi = img.astype(np.int)
imgi
Out[34]:
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]])
In [35]:
img[10:20,15:20]
Out[35]:
array([[ 0.49411765,  0.48627451,  0.49411765,  0.50196078,  0.50588235],
       [ 0.49411765,  0.4745098 ,  0.4745098 ,  0.48627451,  0.49411765],
       [ 0.49019608,  0.49411765,  0.48627451,  0.48627451,  0.49019608],
       [ 0.48627451,  0.49803922,  0.48627451,  0.48235294,  0.49019608],
       [ 0.47843137,  0.47843137,  0.47843137,  0.48235294,  0.49019608],
       [ 0.4745098 ,  0.48235294,  0.48627451,  0.49411765,  0.49411765],
       [ 0.48627451,  0.47843137,  0.48235294,  0.48627451,  0.48627451],
       [ 0.48627451,  0.4745098 ,  0.47843137,  0.48235294,  0.48627451],
       [ 0.48627451,  0.47843137,  0.47843137,  0.48235294,  0.48235294],
       [ 0.47843137,  0.47843137,  0.48235294,  0.48235294,  0.48235294]])
In [36]:
# img[10:200, 10:-100] = 100
imshow(img, cmap='gray')
colorbar()
Out[36]:
<matplotlib.colorbar.Colorbar at 0x150e9c3f470>

Segmentace

In [38]:
imthr = img > 0.2
imshow(imthr, cmap='gray')
Out[38]:
<matplotlib.image.AxesImage at 0x150e9a6b6a0>
In [43]:
imthr = img > 0.50
imshow(imthr, cmap='gray')
Out[43]:
<matplotlib.image.AxesImage at 0x150e9dd4d68>
In [ ]:
 

Barveni obrazu - label

In [44]:
# blobs_labels = skimage.morphology.label(img, background=0)
imlabel = label(imthr, background=0)
imshow(imlabel, cmap='gray')
Out[44]:
<matplotlib.image.AxesImage at 0x150e9e81358>
In [45]:
np.unique(imlabel)
Out[45]:
array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
        13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
        26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
        39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
        52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
        65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
        78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
        91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
       104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
       117, 118, 119], dtype=int64)
In [46]:
# pocet labelu
print(np.max(imlabel))
119

Zobrazení jednoho objektu

In [47]:
imshow(imlabel==35)
Out[47]:
<matplotlib.image.AxesImage at 0x150e9f22518>
In [58]:
fig, axes = subplots(1,3, figsize=(15,4))
axes[0].imshow(imlabel==35)
axes[1].imshow(imlabel==36)
axes[2].imshow(imlabel==64)
Out[58]:
<matplotlib.image.AxesImage at 0x150f00bf7b8>

Barevná vizualizace

In [61]:
# Barevne provedeni
image_label_overlay = label2rgb(imlabel, image=img)
plt.imshow(image_label_overlay)
Out[61]:
<matplotlib.image.AxesImage at 0x150f19351d0>

Filtrace

In [62]:
coins_zoom = img[10:80, 300:370]
from scipy import ndimage
gaussian_coins1 = gaussian_filter(coins_zoom, sigma=1)
gaussian_coins2 = gaussian_filter(coins_zoom, sigma=15)

fig, axes = subplots(1,3, figsize=(15,4))
axes[0].imshow(coins_zoom, cmap='gray')
axes[1].imshow(gaussian_coins1, cmap='gray')
axes[2].imshow(gaussian_coins2, cmap='gray')
C:\Users\miros\Miniconda3\envs\lisa36\lib\site-packages\skimage\filters\_gaussian.py:22: skimage_deprecation: Function ``gaussian_filter`` is deprecated. Use ``skimage.filters.gaussian`` instead.
  multichannel=None, preserve_range=False, truncate=4.0):
C:\Users\miros\Miniconda3\envs\lisa36\lib\site-packages\skimage\filters\_gaussian.py:22: skimage_deprecation: Function ``gaussian_filter`` is deprecated. Use ``skimage.filters.gaussian`` instead.
  multichannel=None, preserve_range=False, truncate=4.0):
Out[62]:
<matplotlib.image.AxesImage at 0x150f1d6c3c8>