%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")
coins = data.coins()
Image(coins)
coins
type(coins)
coins.shape
coins[20:30, 10:-10] = 255
coins[::10, ::10] = 255
Image(coins)
chelsa = data.chelsea()
Image(chelsa)
chelsa.shape
chelsa[:4,:4,:]
tmp = rgb2hsv(chelsa)
tmp[:,:,1] = 0
tmp = hsv2rgb(tmp)
Image(tmp)
@interact(x=(0.0, 1.0, 0.01))
def on_change(x=0):
tmp = rgb2hsv(chelsa)
tmp[:,:,0] = x
display(Image(hsv2rgb(tmp)))
tmp = rgb2gray(chelsa)
display(Image(tmp))
rgb2gray??
# Odbicie lustrzane
Image(chelsa[:,::-1])
# Swap axes 0 and 1 (do not chagne 2)
Image(chelsa.transpose(1, 0, 2))
# Wycięcie
Image(chelsa[200:300,200:300])
# Proste statystyki
coins = data.coins()
display(Image(coins))
np.mean(coins), np.std(coins)
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())
# B[i,j] = g(A[i,j]) dla każdego i,j
# Negacja
Image(255-coins)
# Co siÄ™ dzieje z histogramami?
figure(figsize=(15,5))
subplot(1,2,1); plot_hist(coins)
subplot(1,2,2); plot_hist(255-coins)
thresh = 120
binary = (coins > thresh) * 255
binary = np.uint8(binary) # unit64 => unit8
Image(binary)
# Normalizacja
img = data.moon()
figure(figsize=(15,5))
subplot(1,2,1); io.imshow(Image(img))
subplot(1,2,2); plot_hist(img)
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)
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)
# 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))
# Krzywa gamma
img = img_as_float(data.coins())
Image(img**0.7)
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))
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)
Image(io.imread('img/convolution.png'))
img = data.page()
img = img_as_float(img)
Image(img)
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?
cam = img_as_float(data.camera())
Image(cam)
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)
img = Image(zeros([6,6]))
img[2,:] = 1
img[:,2] = 1
figure(figsize=(8,8))
io.imshow(img)
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)
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)
img = Image(zeros([6,6]))
img[2,:] = 1
img[:,2] = 1
figure(figsize=(8,8))
io.imshow(img)
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)
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)
# 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)
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)
figure(figsize=(20,20))
subplot(1,2,1); io.imshow(data.coins())
subplot(1,2,2); io.imshow(filters.sobel(data.coins()))
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)
Image(filters.rank.mean(img, ones([3,3], dtype=uint8)))
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'))
import scipy
Image(scipy.ndimage.gaussian_filter(img, sigma=3))
Image(io.imread('http://tracer.lcc.uma.es/problems/mfp/MedianFilter.jpg'))
Image(filters.rank.median(img, ones([3,3],dtype=uint8)))
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))