ESF projekt Západočeské univerzity v Plzni reg. č. CZ.02.2.69/0.0/0.0/16 015/0002287

Morphological operations

Teorie bodovych mnozin, binarni transformace, posunuti, dilatace, eroze, otevreni(e+d), uzavreni(d+e), strukturni element, odstraneni sumu, malych objektu, zdurazneni struktury, popis objektu, otevreni a uzavreni je idempotentni.

Strukturni element - stejna vlastnost pro ruzne smery = izotropie

dilatace - zaplneni der a zalivu, kumutativni, asociatiovni, invariantni k posunuti

eroze - zmizi male objekty, objekty se zmensi o 1 slupku, odecteni od puvodniho obrazu ->obrysy, invariantni k posunuti

otevreni - oddeli objekty spojene uzkou siji, odstrani male detaily = E+D

uzavreni - spoji blizke objekty, zaplni mle diry a uzke zalivy = D+E

skelet - mnozina stredu kruznic, ktere jsou obsazeny v Y a dotykaji se Y alespon ve 2 bodech, lze vytvorit pomoci erozi a dilataci

In [1]:
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
In [2]:
# thresholding - prahování
text_color = skimage.io.imread('studentfuture.png')
text_gray = skimage.color.rgb2gray(text_color)
plt.imshow(text_gray, cmap = 'gray')
plt.show()
textb = text_gray*255 > 245
plt.imshow(textb, cmap = 'gray')
plt.show()
In [3]:
def dilatace(imageB, size):
    el = np.ones((size,size))
    im2 = np.zeros(imageB.shape, dtype=np.uint8)    
    for i in range(imageB.shape[0]-el.shape[0]):
        for j in range(imageB.shape[1]-el.shape[1]):
            if imageB[i, j] == el[0, 0]:
                im2[i:i + el.shape[0], j:j + el.shape[1]] = (el + im2[i:i + el.shape[0], j:j + el.shape[1]]) > 0
                
    return im2
In [4]:
def eroze(imageB, size):
    el = np.ones((size,size))
    im2 = np.zeros(imageB.shape, dtype=np.uint8)    
    for i in range(imageB.shape[0]-el.shape[0]):
        for j in range(imageB.shape[1]-el.shape[1]):
            im2[i, j] = np.array_equal(imageB[i:i + el.shape[0], j:j + el.shape[1]], el)
    return im2
In [5]:
def otevreni(imageB, size):
    el = np.ones((size,size))
    img2 = imageB
    img2 = eroze(img2, size)
    img2 = dilatace(img2, size)
    return img2
In [6]:
def uzavreni(imageB, size):
    el = np.ones((size,size))
    img2 = imageB
    img2 = dilatace(img2, size)        
    img2 = eroze(img2, size)
    return img2
In [7]:
dilatace1 = dilatace(textb, 3)
plt.imshow(dilatace1, cmap = 'gray')
plt.show()
In [8]:
dilatace2 = dilatace(textb, 5)
plt.imshow(dilatace2, cmap = 'gray')
plt.show()
In [9]:
dilatace3 = dilatace(textb, 7)
plt.imshow(dilatace3, cmap = 'gray')
plt.show()
In [10]:
size = 3
kernel = np.ones((size,size))
dilatace4 = skimage.morphology.binary_dilation(textb, kernel)
plt.imshow(dilatace4, cmap = 'gray')
plt.show()
In [11]:
kernel = skimage.morphology.diamond(1).astype(np.uint8)
print(kernel)
dilatace5 = skimage.morphology.binary_dilation(textb, kernel)
plt.imshow(dilatace5, cmap = 'gray')
plt.show()
[[0 1 0]
 [1 1 1]
 [0 1 0]]
In [12]:
eroze1 = eroze(textb, 3)
plt.imshow(eroze1, cmap = 'gray')
plt.show()
In [13]:
eroze2 = eroze(textb, 5)
plt.imshow(eroze2, cmap = 'gray')
plt.show()
In [14]:
eroze3 = eroze(textb, 7)
plt.imshow(eroze3, cmap = 'gray')
plt.show()
In [15]:
size = 3
kernel = np.ones((size,size))
erosion4 = skimage.morphology.binary_erosion(textb, kernel)
plt.imshow(erosion4, cmap = 'gray')
plt.show()
In [16]:
obrys = textb^erosion4
plt.imshow(obrys, cmap = 'gray')
plt.show()
In [17]:
otevreni1 = otevreni(textb, 3)
plt.imshow(otevreni1, cmap = 'gray')
plt.show()
In [18]:
uzavreni1 = uzavreni(textb, 3)
plt.imshow(uzavreni1, cmap = 'gray')
plt.show()
In [19]:
skeleton =  skimage.morphology.skeletonize(textb)
plt.imshow(skeleton, cmap = 'gray')
plt.show()
In [20]:
head_color = skimage.io.imread('http://www.kky.zcu.cz/uploads/courses/zdo/lesson5/original.jpg')
head_gray = skimage.color.rgb2gray(head_color)
headb = head_gray > 0.5
plt.imshow(headb, cmap='gray')
plt.show()
In [21]:
skeleton =  skimage.morphology.skeletonize(headb)
plt.imshow(skeleton, cmap = 'gray')
plt.show()
In [22]:
headh = scipy.ndimage.morphology.binary_fill_holes(headb)
plt.imshow(headh, cmap='gray')
plt.show()
In [23]:
size = 3
kernel = np.ones((size,size))
erosion5 = skimage.morphology.binary_erosion(headb, kernel)
plt.imshow(erosion5, cmap = 'gray')
plt.show()
In [24]:
headh = scipy.ndimage.morphology.binary_fill_holes(erosion5)
plt.imshow(headh, cmap='gray')
plt.show()
In [25]:
image_label_overlay = skimage.color.label2rgb(skimage.measure.label(headh), image=headh)
plt.imshow(image_label_overlay)
print ("Pocet pismen je:", skimage.measure.label(headh).max()-1)
Pocet pismen je: 4

Cviceni, oddelte nasledujici ctverce od sebe spoctete kolik jich tam je.