In [1]:
%matplotlib inline
from pylab import *
import skimage
from skimage import data, io, filters, exposure, feature
from skimage.filters import rank
from skimage.util.dtype import convert
from skimage import img_as_float, img_as_ubyte
from skimage.io import Image
from skimage.color import rgb2hsv, hsv2rgb, rgb2gray
from skimage.filters.edges import convolve
from matplotlib import pylab as plt  
import numpy as np
from numpy import array
from IPython.display import display
from ipywidgets import interact, interactive, fixed
from IPython.core.display import clear_output

import warnings
warnings.simplefilter("ignore")

Reprezentacja obrazów

In [2]:
coins = data.coins()
Image(coins)
Out[2]:
In [3]:
coins
Out[3]:
array([[ 47, 123, 133, ...,  14,   3,  12],
       [ 93, 144, 145, ...,  12,   7,   7],
       [126, 147, 143, ...,   2,  13,   3],
       ..., 
       [ 81,  79,  74, ...,   6,   4,   7],
       [ 88,  82,  74, ...,   5,   7,   8],
       [ 91,  79,  68, ...,   4,  10,   7]], dtype=uint8)
In [4]:
type(coins)
Out[4]:
numpy.ndarray
In [5]:
coins.shape
Out[5]:
(303, 384)
In [6]:
coins[20:30, 10:-10] = 255
coins[::10, ::10] = 255
Image(coins)
Out[6]:
In [7]:
chelsa = data.chelsea()
Image(chelsa)
Out[7]:
In [8]:
chelsa.shape
Out[8]:
(300, 451, 3)
In [9]:
chelsa[:4,:4,:]
Out[9]:
array([[[143, 120, 104],
        [143, 120, 104],
        [141, 118, 102],
        [141, 118, 102]],

       [[146, 123, 107],
        [145, 122, 106],
        [143, 120, 104],
        [142, 119, 103]],

       [[148, 126, 112],
        [147, 125, 111],
        [146, 122, 109],
        [145, 121, 108]],

       [[151, 129, 116],
        [149, 127, 114],
        [147, 125, 112],
        [147, 125, 112]]], dtype=uint8)
In [10]:
tmp = rgb2hsv(chelsa)
tmp[:,:,1] = 0
tmp = hsv2rgb(tmp)
Image(tmp)
Out[10]:
In [11]:
@interact(x=(0.0, 1.0, 0.01))
def on_change(x=0):
    tmp = rgb2hsv(chelsa)
    tmp[:,:,0] = x
    display(Image(hsv2rgb(tmp)))
In [12]:
tmp = rgb2gray(chelsa)
display(Image(tmp))
In [13]:
rgb2gray??

Inne operacje macierzowe

In [14]:
# Odbicie lustrzane
Image(chelsa[:,::-1])
Out[14]:
In [15]:
# Swap axes 0 and 1 (do not chagne 2)
Image(chelsa.transpose(1, 0, 2))
Out[15]:
In [16]:
# Wycięcie
Image(chelsa[200:300,200:300])
Out[16]:
In [17]:
# Proste statystyki
coins = data.coins()
display(Image(coins))
np.mean(coins), np.std(coins)
Out[17]:
(96.855516020352042, 52.879818619868239)

Proste przetwarzanie

Histogram

In [18]:
def plot_hist(img):
    img = img_as_ubyte(img)
    histo, x = np.histogram(img, range(0, 256), density=True)
    plot(histo)
    xlim(0, 255)
    
plot_hist(data.coins())

Operacje punktowe

In [19]:
# B[i,j] = g(A[i,j]) dla każdego i,j
# Negacja
Image(255-coins)
Out[19]:
In [20]:
# Co siÄ™ dzieje z histogramami?
figure(figsize=(15,5))
subplot(1,2,1); plot_hist(coins)
subplot(1,2,2); plot_hist(255-coins)
In [21]:
thresh = 120
binary = (coins > thresh) * 255
binary = np.uint8(binary) # unit64 => unit8
Image(binary)
Out[21]:
In [22]:
# Normalizacja
img = data.moon()
figure(figsize=(15,5))
subplot(1,2,1); io.imshow(Image(img))
subplot(1,2,2); plot_hist(img)
In [23]:
img = img_as_float(data.moon())
MIN = 100 / 256
MAX = 125 / 256

norm = (img - MIN) / (MAX - MIN)
norm[norm > 1] = 1
norm[norm < 0] = 0

figure(figsize=(15,5))
subplot(1,2,1); io.imshow(Image(norm))
subplot(1,2,2); plot_hist(norm)
In [24]:
img = img_as_float(data.moon())
@interact(perc=(0,10,0.1))
def on_change(perc=0.0):
    MIN = np.percentile(img, perc)
    MAX = np.percentile(img, 100-perc)

    norm = (img - MIN) / (MAX - MIN)
    norm[norm[:,:] > 1] = 1
    norm[norm[:,:] < 0] = 0
    
    fig = figure(figsize=(15,5))
    subplot(1,2,1); io.imshow(norm)
    subplot(1,2,2); plot_hist(norm)
In [25]:
# Operacje punktowe można wygodnie wyświetlić
def identity(v):
    return v

def negate(v):
    return 255-v

def get_thresh(th):
    def thresh(v):
        return 255*(v>th)
    return thresh

def plot_point_op(fun):
    xlim(-5, 260)
    ylim(-5, 260)
    plot([fun(v) for v in np.arange(0,256)])
    
figure(figsize=(15,4))
subplot(2,2,1); plot_point_op(identity)
subplot(2,2,2); plot_point_op(negate)
subplot(2,2,3); plot_point_op(get_thresh(64))
subplot(2,2,4); plot_point_op(get_thresh(128))
In [26]:
# Krzywa gamma
img = img_as_float(data.coins())

Image(img**0.7)
Out[26]:
In [27]:
img = img_as_float(data.coins())

def get_gamma_fun(g):
    def gamma(v):
        return 255*((v/255)**g)
    return gamma

@interact(gamma=(0,10,0.1))
def on_change(gamma=1.0):
    tmp = img ** gamma
    
    figure(figsize=(15,5))
 
    subplot(1,3,1); io.imshow(tmp)
    subplot(1,3,2); plot_hist(tmp)
    subplot(1,3,3); plot_point_op(get_gamma_fun(gamma))
In [28]:
coins = img_as_float(data.coins())
figure()
plot_point_op(get_gamma_fun(0.3))
plot_point_op(get_gamma_fun(0.5))
plot_point_op(get_gamma_fun(1.0))
plot_point_op(get_gamma_fun(4))
legend((0.3, 0.5, 1.0, 4), loc='best')
figure(figsize=(10,10))
subplot(2,2,1); title('gamma = 0.3'); io.imshow(coins**0.3)
subplot(2,2,2); title('gamma = 0.5'); io.imshow(coins**0.5)
subplot(2,2,3); title('gamma = 1.0'); io.imshow(coins**1.0)
subplot(2,2,4); title('gamma = 4.0'); io.imshow(coins**4.0)
Out[28]:
<matplotlib.image.AxesImage at 0x111c596d8>

Konwolucja (splot)

In [29]:
Image(io.imread('img/convolution.png'))
Out[29]:

Filtry

In [30]:
img = data.page()
img = img_as_float(img)
Image(img)
Out[30]:
In [31]:
from skimage.filters.edges import convolve

#K = array([[1,1,1],
#           [1,1,1],
#           [1,1,1]])
K = ones([13,13])
#print(K)
K = K / sum(K) # Dlaczego przez 9?
res = convolve(img, K)
Image(res)
# Co będzie jeśli zastosujemy wielokrotnie?
Out[31]:
In [32]:
cam = img_as_float(data.camera())
Image(cam)
Out[32]:
In [33]:
K = array([[ 1, 2, 1],
           [ 0, 0, 0],
           [-1,-2,-1]])
K = K / 4                       # Dlaczego przez 4?
res = abs(convolve(cam, K))  # Dlaczego abs?
Image(res**0.5)
Out[33]:
In [34]:
img = Image(zeros([6,6]))
img[2,:] = 1
img[:,2] = 1
figure(figsize=(8,8))
io.imshow(img)
Out[34]:
<matplotlib.image.AxesImage at 0x111ccc518>
In [35]:
K = array([[ 1, 2, 1],
           [ 0, 0, 0],
           [ -1,-2,-1]])
K = K / 4
res = abs(convolve(img, K))
figure(figsize=(8,8))
io.imshow(res)
Out[35]:
<matplotlib.image.AxesImage at 0x10a006cc0>
In [36]:
K = array([[ 1, 2, 1],
           [ 0, 0, 0],
           [-1,-2,-1]])
K = K / 8
res = abs(convolve(cam, K))
figure(figsize=(8,8))
io.imshow(res**0.7)
Out[36]:
<matplotlib.image.AxesImage at 0x10f3807b8>
In [37]:
img = Image(zeros([6,6]))
img[2,:] = 1
img[:,2] = 1
figure(figsize=(8,8))
io.imshow(img)
Out[37]:
<matplotlib.image.AxesImage at 0x10a46df60>
In [38]:
K = array([[ 1, 0,-1],
           [ 2, 0,-2],
           [ 1, 0,-1]])

K = K / 4
res = abs(convolve(img, K))
figure(figsize=(8,8))
io.imshow(res)
Out[38]:
<matplotlib.image.AxesImage at 0x10f4d9dd8>
In [39]:
figure(figsize=(16,8))
subplot(1,2,1); io.imshow(img)

Kh = array([[ 1, 2, 1],
            [ 0, 0, 0],
            [-1,-2,-1]]) / 4
Kv = array([[ 1, 0,-1],
            [ 2, 0,-2],
            [ 1, 0,-1]]) / 4

hor = abs(convolve(img, Kh))
ver = abs(convolve(img, Kv))

res = np.sqrt(hor + ver)

res = np.clip(res, a_min=0.0, a_max=1.0)

subplot(1,2,2); io.imshow(res)
Out[39]:
<matplotlib.image.AxesImage at 0x10f3a6b00>
In [40]:
# Inne filtry wykrywające krawędzie: Roberts, Previtt
K = array([[ 1,  0],
           [ 0, -1]])

K = array([[ 1,  1,  0],
           [ 1,  0, -1],
           [ 0, -1, -1]])
K = K / 2                       
res = abs(convolve(cam, K))
Image(res**0.5)
Out[40]:

Używając skimage

In [41]:
figure(figsize=(30,30))
subplot(1,3,1); io.imshow(abs(filters.sobel_v(cam))**0.5)
subplot(1,3,2); io.imshow(abs(filters.sobel_h(cam))**0.5)
subplot(1,3,3); io.imshow(filters.sobel(cam)**0.5)
Out[41]:
<matplotlib.image.AxesImage at 0x10dc42b38>
In [42]:
figure(figsize=(20,20))
subplot(1,2,1); io.imshow(data.coins())
subplot(1,2,2); io.imshow(filters.sobel(data.coins()))
Out[42]:
<matplotlib.image.AxesImage at 0x1081bac18>

Odszumianie

In [43]:
img = data.camera()
noise = np.random.random(img.shape)
img = img_as_ubyte(data.camera())
img[noise > 0.99] = 255
img[noise < 0.01] = 0
Image(img)
Out[43]:
In [44]:
Image(filters.rank.mean(img, ones([3,3], dtype=uint8)))
Out[44]:
In [45]:
figure(figsize=(20,20))
subplot(1,2,1)
io.imshow(io.imread('http://www.mathworks.com/help/releases/R2013b/images/gaussian.png'))
subplot(1,2,2)
io.imshow(io.imread('http://4.bp.blogspot.com/_qEs9r36R5kw/S63QM-0V6kI/AAAAAAAAArY/9AQI1izF9Wk/s320/Picture+2.png'))
Out[45]:
<matplotlib.image.AxesImage at 0x10f27a828>
In [46]:
import scipy
Image(scipy.ndimage.gaussian_filter(img, sigma=3))
Out[46]:
In [47]:
Image(io.imread('http://tracer.lcc.uma.es/problems/mfp/MedianFilter.jpg'))
Out[47]:
In [48]:
Image(filters.rank.median(img, ones([3,3],dtype=uint8)))
Out[48]:

Filtr Canny

In [49]:
img = img_as_float(data.imread('img/noisy.png'))
#img = im
figure(figsize=(20,20))
subplot(1,3,1)
io.imshow(img**0.5)
subplot(1,3,2)
io.imshow(filters.sobel(img)**0.5)
subplot(1,3,3)
io.imshow(skimage.feature.canny(img, sigma=3))
Out[49]:
<matplotlib.image.AxesImage at 0x11447c0f0>