Vstup: binární obraz nebo obraz s více úrovněmi jasu
Výstup: binární obraz nebo obraz s více úrovněmi jasu
Cíl: tvar objektů
Vlastnosti: snadné a rychlé předzpracování
%pylab inline --no-import-all
import skimage
import skimage.io
import skimage.color
import skimage.morphology
import matplotlib.pyplot as plt
import numpy as np
import scipy
import scipy.ndimage
Populating the interactive namespace from numpy and matplotlib
# thresholding - prahování
text_color = skimage.io.imread('http://www.kky.zcu.cz/uploads/courses/zdo/lesson5/original.jpg')
text_gray = skimage.color.rgb2gray(text_color)
textb = text_gray > 0.5
plt.imshow(textb, cmap='gray')
<matplotlib.image.AxesImage at 0x3f293d0>
a = np.zeros((5,5), dtype=np.int)
a[2, 2] = 1
#a[1:6, 2:5] = 1
print 'input \n', a
kernel = skimage.morphology.diamond(1).astype(np.uint8)
print 'kernel \n', kernel
ad = skimage.morphology.binary_dilation(a, kernel)
print 'dilation \n', ad
#dir(skimage.morphology)
#skimage.morphology.binary_dilation(a, morphology.diamond(1)).astype(np.uint8)
input [[0 0 0 0 0] [0 0 0 0 0] [0 0 1 0 0] [0 0 0 0 0] [0 0 0 0 0]] kernel [[0 1 0] [1 1 1] [0 1 0]] dilation [[0 0 0 0 0] [0 0 1 0 0] [0 1 1 1 0] [0 0 1 0 0] [0 0 0 0 0]]
kernel_big = skimage.morphology.diamond(5).astype(np.uint8)
textd = skimage.morphology.binary_dilation(textb, kernel_big)
plt.imshow(textd, cmap='gray')
<matplotlib.image.AxesImage at 0x420acd0>
a = np.zeros((7,7), dtype=np.int)
a[1:6, 2:5] = 1
print 'input \n', a
ad = skimage.morphology.binary_erosion(a, kernel)
print 'erosion \n', ad
input [[0 0 0 0 0 0 0] [0 0 1 1 1 0 0] [0 0 1 1 1 0 0] [0 0 1 1 1 0 0] [0 0 1 1 1 0 0] [0 0 1 1 1 0 0] [0 0 0 0 0 0 0]] erosion [[0 0 0 0 0 0 0] [0 0 0 0 0 0 0] [0 0 0 1 0 0 0] [0 0 0 1 0 0 0] [0 0 0 1 0 0 0] [0 0 0 0 0 0 0] [0 0 0 0 0 0 0]]
#kernel_big = skimage.morphology.diamond(3).astype(np.uint8)
textd = skimage.morphology.binary_erosion(textb, kernel_big)
plt.imshow(textd, cmap='gray')
<matplotlib.image.AxesImage at 0x4730750>
Kombinace obou předchozích metod
# Otevření
#kernel_big = skimage.morphology.diamond(4).astype(np.uint8)
textd = skimage.morphology.binary_opening(textb, kernel_big)
#plt.subplot(121)
#plt.imshow(textb, cmap='gray')
#plt.subplot(122)
plt.imshow(textd, cmap='gray')
<matplotlib.image.AxesImage at 0x4767210>
# Uzavření
textd = skimage.morphology.binary_closing(textb, kernel_big)
plt.imshow(textd, cmap='gray')
<matplotlib.image.AxesImage at 0x4a42610>
a = np.zeros((200, 300), dtype=np.int)
#a[1:6, 2:5] = 1
a[50:100, 50:200] = 1
a[30:150, 100:150] = 1
skela = skimage.morphology.skeletonize(a)
plt.figure(figsize=(9, 4))
plt.subplot(121)
plt.imshow(a, cmap='gray')
plt.subplot(122)
plt.imshow(skela, cmap='gray')
<matplotlib.image.AxesImage at 0x4f0c890>
textd = skimage.morphology.skeletonize(textb)
plt.imshow(textd, cmap='gray')
<matplotlib.image.AxesImage at 0x5445950>
texth = scipy.ndimage.morphology.binary_fill_holes(textb)
plt.imshow(texth, cmap='gray')
<matplotlib.image.AxesImage at 0x5477d90>
Spočítejte čtverce na obrázku z kamery