Instalace Pythonu
PythonXY https://code.google.com/p/pythonxy/
WinPython http://winpython.github.io/
Anaconda https://store.continuum.io/cshop/anaconda/
Programování v Pythonu http://pythonic.eu/fjfi/
Stránky našeho předmětu ZDO http://www.kky.zcu.cz/cs/courses/zdo
Ukazky metod zpracovani obrazu http://scipy-lectures.github.io/packages/scikit-image/
%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
# from skimage.filters import gaussian_filter
from skimage.filters import gaussian as 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
print("hello")
hello
def mojeFunkce(vstup):
vystup = vstup + 6
return vystup
mojeFunkce(5)
11
for i in range(2,5):
print(i)
2 3 4
pole = ['Franta', 'Jakub', 'Marta']
for jmeno in pole:
print(jmeno)
Franta Jakub Marta
# URL = "http://plzen.cz/cameraFeed.php"
URL = "http://vysilani.zaktv.cz/camera/namesti.jpg"
img = imread(URL)
imshow(img)
# show()
<matplotlib.image.AxesImage at 0x150e9498470>
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
(960, 1280, 3)
URL = "http://uc452cam01-kky.fav.zcu.cz/snapshot.jpg"
img = imread(URL, as_grey=True)
imshow(img, cmap='gray')
<matplotlib.image.AxesImage at 0x150f1df70f0>
# ukazkova data
img = skimage.data.coins() / 255.0
imshow(img, cmap='gray')
<matplotlib.image.AxesImage at 0x150e9cf50b8>
img.shape
(303, 384)
img[50, 10]
0.43529411764705883
imgi = img.astype(np.int)
imgi
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]])
img[10:20,15:20]
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]])
# img[10:200, 10:-100] = 100
imshow(img, cmap='gray')
colorbar()
<matplotlib.colorbar.Colorbar at 0x150e9c3f470>
imthr = img > 0.2
imshow(imthr, cmap='gray')
<matplotlib.image.AxesImage at 0x150e9a6b6a0>
imthr = img > 0.50
imshow(imthr, cmap='gray')
<matplotlib.image.AxesImage at 0x150e9dd4d68>
# blobs_labels = skimage.morphology.label(img, background=0)
imlabel = label(imthr, background=0)
imshow(imlabel, cmap='gray')
<matplotlib.image.AxesImage at 0x150e9e81358>
np.unique(imlabel)
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)
# pocet labelu
print(np.max(imlabel))
119
imshow(imlabel==35)
<matplotlib.image.AxesImage at 0x150e9f22518>
fig, axes = subplots(1,3, figsize=(15,4))
axes[0].imshow(imlabel==35)
axes[1].imshow(imlabel==36)
axes[2].imshow(imlabel==64)
<matplotlib.image.AxesImage at 0x150f00bf7b8>
# Barevne provedeni
image_label_overlay = label2rgb(imlabel, image=img)
plt.imshow(image_label_overlay)
<matplotlib.image.AxesImage at 0x150f19351d0>
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):
<matplotlib.image.AxesImage at 0x150f1d6c3c8>
thr = threshold_otsu(img)
thr
107
imthr = img > thr
imshow(imthr, cmap='gray')
<matplotlib.image.AxesImage at 0x169cb209f28>
Spocitame pocet objektu?
# plt.figure(figsize(10,10))
# imshow(imthr[0:20,260:310], interpolation='nearest')
imer = binary_erosion(imthr, iterations=1)
imdil = binary_dilation(imthr, iterations=2)
fig, axes = subplots(1,3, figsize=(15,5))
axes[0].imshow(imdil[160:230,240:310], interpolation='nearest')
axes[1].imshow(imthr[160:230,240:310], interpolation='nearest')
axes[2].imshow(imer[160:230,240:310], interpolation='nearest')
<matplotlib.image.AxesImage at 0x169d0ee3080>
ones([5,5])
array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]])
Pocet prekryvajicich se objektu?
Využijeme funkce regionprops.
objnumber = 38
imshow(imlabel==objnumber)
# print np.unique(imlabel)
props = regionprops(imlabel+1)
print("Centroid ", props[objnumber].centroid)
print("Plocha ", props[objnumber].area)
print("Obvod ", props[objnumber].perimeter)
Centroid (54.141833810888251, 44.160458452722061) Plocha 1396 Obvod 155.160425587
for objnumber in range(1, len(props)):
if props[objnumber].area > 2000:
print("id ", objnumber)
print("Centroid ", props[objnumber].centroid)
print("Plocha ", props[objnumber].area)
print("Obvod ", props[objnumber].perimeter)
id 1 Centroid (9.5152317880794701, 70.17748344370861) Plocha 3020 Obvod 705.209198822 id 20 Centroid (43.830220713073004, 334.4095925297114) Plocha 2356 Obvod 579.71277016 id 55 Centroid (185.86056868790681, 347.8036998972251) Plocha 2919 Obvod 470.49347007
Spocitejte kolik objektu urciteho typu se objevuje na obrazku z webkamery. Rozeznavame nasledujici typy objektu:
Trenovaci data naleznete zde
.ipynb
nebo .py
train_target = [1,1,1,2,2,2,3,3,3]
# Features - příznaky
train_data = np.zeros([9,2])
print(train_data)
for objnumber in range(1, len(props)):
train_data[objnumber - 1, 0] = props[objnumber].area
train_data[objnumber - 1, 1] = props[objnumber].perimeter
print(train_data)
## Training
from sklearn import svm
svc = svm.SVC()
svc.fit(train_data, train_target)
## Testing
y_pred = svc.predict(test_data)
# print("Number of mislabeled points : %d" % (test_target != y_pred).sum())
y_pred = svc.predict([[300, 7800]])
y_pred