%pylab inline --no-import-all
import numpy as np
import matplotlib.pyplot as plt
import skimage
from skimage import data
import skimage.segmentation
import scipy
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
from skimage.filters import gaussian
from skimage.segmentation import active_contour
# import skimage.color
# import skimage.data
# import skimage.filters
skimage.__version__
Populating the interactive namespace from numpy and matplotlib
'0.16.2'
coins = skimage.data.coins()
hist = np.histogram(coins, bins=np.arange(0, 256))
plt.figure(figsize=(8, 3))
plt.subplot(121)
plt.imshow(coins, cmap=plt.cm.gray, interpolation='nearest')
plt.axis('off')
plt.subplot(122)
plt.plot(hist[1][:-1], hist[0], lw=2)
plt.title('histogram of grey values')
Text(0.5, 1.0, 'histogram of grey values')
plt.figure(figsize=(9, 3.5))
plt.subplot(121)
plt.imshow(coins > 100, cmap=plt.cm.gray, interpolation='nearest')
plt.title('coins > 100')
plt.axis('off')
plt.subplot(122)
plt.imshow(coins > 150, cmap=plt.cm.gray, interpolation='nearest')
plt.title('coins > 150')
plt.axis('off')
plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1)
from skimage import morphology
import matplotlib.pyplot as plt
from scipy import ndimage
import numpy as np
#
n = 12
l = 256
np.random.seed(1)
im = np.zeros((l, l))
points = l*np.random.random((2, n**2))
im[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
im = ndimage.gaussian_filter(im, sigma=l/(4.*n))
blobs = im > 0.7 * im.mean()
all_labels = morphology.label(blobs)
blobs_labels = morphology.label(blobs, background=0)
plt.figure(figsize=(9, 3.5))
plt.subplot(131)
plt.imshow(blobs, cmap='gray')
plt.axis('off')
plt.subplot(132)
plt.imshow(all_labels)
plt.axis('off')
plt.subplot(133)
plt.imshow(blobs_labels)
plt.axis('off')
plt.tight_layout()
plt.show()
from skimage.color import label2rgb
image_label_overlay = label2rgb(morphology.label(coins > 150), image=coins)
plt.imshow(image_label_overlay)
<matplotlib.image.AxesImage at 0x1ecc84c4f98>
Pro vizualizaci je možné využívat i barevný výstup
#%pylab --no-import-all inline
#Imports
import matplotlib.pyplot as plt
# Prahování
from skimage import data
from skimage import filters
from skimage import exposure
camera = data.camera()
val = filters.threshold_otsu(camera)
mask = camera < val
plt.figure(figsize=(9, 3.5))
plt.gray()
plt.subplot(131)
plt.imshow(camera)
plt.subplot(132)
plt.imshow(mask)
hist, bins_center = exposure.histogram(camera)
plt.subplot(133)
plt.plot(bins_center, hist, lw=2)
plt.axvline(val, color='k', ls='--')
plt.show()
#plt.tight_layout()
Původní obraz
dafa
určení hranice na základě znalosti tvaru
jestliže známe předpokládaný tvar objektu můžeme chybějící části doplnit
určení hranice na základě zpřesňování
spojíme koncové body hranic a další bod hranice hledáme na kolmici ve středu spojnice
gradientní pole
# from skimage.filters import canny
from skimage.feature import canny
edges = canny(coins/255.)
plt.figure(figsize=(6, 4))
plt.imshow(edges, cmap=plt.cm.gray, interpolation='nearest')
plt.axis('off')
plt.title('Canny detector')
Text(0.5, 1.0, 'Canny detector')
from scipy import ndimage
fill_coins = ndimage.binary_fill_holes(edges)
plt.figure(figsize=(6, 4))
plt.imshow(fill_coins, cmap=plt.cm.gray, interpolation='nearest')
plt.axis('off')
plt.title('Filling the holes')
Text(0.5, 1.0, 'Filling the holes')
from skimage.filters import sobel
import skimage.morphology
elevation_map = sobel(coins)
plt.figure(figsize=(4, 3))
plt.imshow(elevation_map, cmap=plt.cm.jet, interpolation='nearest')
plt.axis('off')
plt.title('elevation_map')
Text(0.5, 1.0, 'elevation_map')
markers = np.zeros_like(coins)
markers[coins < 30] = 1
markers[coins > 150] = 2
plt.figure(figsize=(4, 3))
plt.imshow(markers, cmap="jet", interpolation='nearest')
plt.axis('off')
plt.title('markers')
Text(0.5, 1.0, 'markers')