{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "from pylab import *\n", "import skimage\n", "from skimage import data, io, filters, exposure, feature\n", "from skimage.filters import rank\n", "from skimage.util.dtype import convert\n", "from skimage import img_as_float, img_as_ubyte\n", "from skimage.io import Image\n", "from skimage.color import rgb2hsv, hsv2rgb, rgb2gray\n", "from skimage.filters.edges import convolve\n", "from matplotlib import pylab as plt \n", "import numpy as np\n", "from numpy import array\n", "from IPython.display import display\n", "from ipywidgets import interact, interactive, fixed\n", "from IPython.core.display import clear_output\n", "\n", "import warnings\n", "warnings.simplefilter(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reprezentacja obrazów" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coins = data.coins()\n", "Image(coins)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coins" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(coins)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coins.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coins[20:30, 10:-10] = 255\n", "coins[::10, ::10] = 255\n", "Image(coins)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "chelsa = data.chelsea()\n", "Image(chelsa)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "chelsa.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "chelsa[:4,:4,:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tmp = rgb2hsv(chelsa)\n", "tmp[:,:,1] = 0\n", "tmp = hsv2rgb(tmp)\n", "Image(tmp)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(x=(0.0, 1.0, 0.01))\n", "def on_change(x=0):\n", " tmp = rgb2hsv(chelsa)\n", " tmp[:,:,0] = x\n", " display(Image(hsv2rgb(tmp)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tmp = rgb2gray(chelsa)\n", "display(Image(tmp))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rgb2gray??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inne operacje macierzowe" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Odbicie lustrzane\n", "Image(chelsa[:,::-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Swap axes 0 and 1 (do not chagne 2)\n", "Image(chelsa.transpose(1, 0, 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Wycięcie\n", "Image(chelsa[200:300,200:300])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Proste statystyki\n", "coins = data.coins()\n", "display(Image(coins))\n", "np.mean(coins), np.std(coins)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Proste przetwarzanie" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Histogram" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def plot_hist(img):\n", " img = img_as_ubyte(img)\n", " histo, x = np.histogram(img, range(0, 256), density=True)\n", " plot(histo)\n", " xlim(0, 255)\n", " \n", "plot_hist(data.coins())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operacje punktowe" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# B[i,j] = g(A[i,j]) dla każdego i,j\n", "# Negacja\n", "Image(255-coins)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Co się dzieje z histogramami?\n", "figure(figsize=(15,5))\n", "subplot(1,2,1); plot_hist(coins)\n", "subplot(1,2,2); plot_hist(255-coins)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "thresh = 120\n", "binary = (coins > thresh) * 255\n", "binary = np.uint8(binary) # unit64 => unit8\n", "Image(binary)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Normalizacja\n", "img = data.moon()\n", "figure(figsize=(15,5))\n", "subplot(1,2,1); io.imshow(Image(img))\n", "subplot(1,2,2); plot_hist(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = img_as_float(data.moon())\n", "MIN = 100 / 256\n", "MAX = 125 / 256\n", "\n", "norm = (img - MIN) / (MAX - MIN)\n", "norm[norm > 1] = 1\n", "norm[norm < 0] = 0\n", "\n", "figure(figsize=(15,5))\n", "subplot(1,2,1); io.imshow(Image(norm))\n", "subplot(1,2,2); plot_hist(norm)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = img_as_float(data.moon())\n", "@interact(perc=(0,10,0.1))\n", "def on_change(perc=0.0):\n", " MIN = np.percentile(img, perc)\n", " MAX = np.percentile(img, 100-perc)\n", "\n", " norm = (img - MIN) / (MAX - MIN)\n", " norm[norm[:,:] > 1] = 1\n", " norm[norm[:,:] < 0] = 0\n", " \n", " fig = figure(figsize=(15,5))\n", " subplot(1,2,1); io.imshow(norm)\n", " subplot(1,2,2); plot_hist(norm)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# Operacje punktowe można wygodnie wyświetlić\n", "def identity(v):\n", " return v\n", "\n", "def negate(v):\n", " return 255-v\n", "\n", "def get_thresh(th):\n", " def thresh(v):\n", " return 255*(v>th)\n", " return thresh\n", "\n", "def plot_point_op(fun):\n", " xlim(-5, 260)\n", " ylim(-5, 260)\n", " plot([fun(v) for v in np.arange(0,256)])\n", " \n", "figure(figsize=(15,4))\n", "subplot(2,2,1); plot_point_op(identity)\n", "subplot(2,2,2); plot_point_op(negate)\n", "subplot(2,2,3); plot_point_op(get_thresh(64))\n", "subplot(2,2,4); plot_point_op(get_thresh(128))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Krzywa gamma\n", "img = img_as_float(data.coins())\n", "\n", "Image(img**0.7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = img_as_float(data.coins())\n", "\n", "def get_gamma_fun(g):\n", " def gamma(v):\n", " return 255*((v/255)**g)\n", " return gamma\n", "\n", "@interact(gamma=(0,10,0.1))\n", "def on_change(gamma=1.0):\n", " tmp = img ** gamma\n", " \n", " figure(figsize=(15,5))\n", " \n", " subplot(1,3,1); io.imshow(tmp)\n", " subplot(1,3,2); plot_hist(tmp)\n", " subplot(1,3,3); plot_point_op(get_gamma_fun(gamma))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coins = img_as_float(data.coins())\n", "figure()\n", "plot_point_op(get_gamma_fun(0.3))\n", "plot_point_op(get_gamma_fun(0.5))\n", "plot_point_op(get_gamma_fun(1.0))\n", "plot_point_op(get_gamma_fun(4))\n", "legend((0.3, 0.5, 1.0, 4), loc='best')\n", "figure(figsize=(10,10))\n", "subplot(2,2,1); title('gamma = 0.3'); io.imshow(coins**0.3)\n", "subplot(2,2,2); title('gamma = 0.5'); io.imshow(coins**0.5)\n", "subplot(2,2,3); title('gamma = 1.0'); io.imshow(coins**1.0)\n", "subplot(2,2,4); title('gamma = 4.0'); io.imshow(coins**4.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Konwolucja (splot)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(io.imread('img/convolution.png'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtry" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = data.page()\n", "img = img_as_float(img)\n", "Image(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from skimage.filters.edges import convolve\n", "\n", "#K = array([[1,1,1],\n", "# [1,1,1],\n", "# [1,1,1]])\n", "K = ones([13,13])\n", "#print(K)\n", "K = K / sum(K) # Dlaczego przez 9?\n", "res = convolve(img, K)\n", "Image(res)\n", "# Co będzie jeśli zastosujemy wielokrotnie?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cam = img_as_float(data.camera())\n", "Image(cam)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "K = array([[ 1, 2, 1],\n", " [ 0, 0, 0],\n", " [-1,-2,-1]])\n", "K = K / 4 # Dlaczego przez 4?\n", "res = abs(convolve(cam, K)) # Dlaczego abs?\n", "Image(res**0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = Image(zeros([6,6]))\n", "img[2,:] = 1\n", "img[:,2] = 1\n", "figure(figsize=(8,8))\n", "io.imshow(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "K = array([[ 1, 2, 1],\n", " [ 0, 0, 0],\n", " [ -1,-2,-1]])\n", "K = K / 4\n", "res = abs(convolve(img, K))\n", "figure(figsize=(8,8))\n", "io.imshow(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "K = array([[ 1, 2, 1],\n", " [ 0, 0, 0],\n", " [-1,-2,-1]])\n", "K = K / 8\n", "res = abs(convolve(cam, K))\n", "figure(figsize=(8,8))\n", "io.imshow(res**0.7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = Image(zeros([6,6]))\n", "img[2,:] = 1\n", "img[:,2] = 1\n", "figure(figsize=(8,8))\n", "io.imshow(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "K = array([[ 1, 0,-1],\n", " [ 2, 0,-2],\n", " [ 1, 0,-1]])\n", "\n", "K = K / 4\n", "res = abs(convolve(img, K))\n", "figure(figsize=(8,8))\n", "io.imshow(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "figure(figsize=(16,8))\n", "subplot(1,2,1); io.imshow(img)\n", "\n", "Kh = array([[ 1, 2, 1],\n", " [ 0, 0, 0],\n", " [-1,-2,-1]]) / 4\n", "Kv = array([[ 1, 0,-1],\n", " [ 2, 0,-2],\n", " [ 1, 0,-1]]) / 4\n", "\n", "hor = abs(convolve(img, Kh))\n", "ver = abs(convolve(img, Kv))\n", "\n", "res = np.sqrt(hor + ver)\n", "\n", "res = np.clip(res, a_min=0.0, a_max=1.0)\n", "\n", "subplot(1,2,2); io.imshow(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Inne filtry wykrywające krawędzie: Roberts, Previtt\n", "K = array([[ 1, 0],\n", " [ 0, -1]])\n", "\n", "K = array([[ 1, 1, 0],\n", " [ 1, 0, -1],\n", " [ 0, -1, -1]])\n", "K = K / 2 \n", "res = abs(convolve(cam, K))\n", "Image(res**0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Używając skimage" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "figure(figsize=(30,30))\n", "subplot(1,3,1); io.imshow(abs(filters.sobel_v(cam))**0.5)\n", "subplot(1,3,2); io.imshow(abs(filters.sobel_h(cam))**0.5)\n", "subplot(1,3,3); io.imshow(filters.sobel(cam)**0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "figure(figsize=(20,20))\n", "subplot(1,2,1); io.imshow(data.coins())\n", "subplot(1,2,2); io.imshow(filters.sobel(data.coins()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Odszumianie" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = data.camera()\n", "noise = np.random.random(img.shape)\n", "img = img_as_ubyte(data.camera())\n", "img[noise > 0.99] = 255\n", "img[noise < 0.01] = 0\n", "Image(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(filters.rank.mean(img, ones([3,3], dtype=uint8)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "figure(figsize=(20,20))\n", "subplot(1,2,1)\n", "io.imshow(io.imread('http://www.mathworks.com/help/releases/R2013b/images/gaussian.png'))\n", "subplot(1,2,2)\n", "io.imshow(io.imread('http://4.bp.blogspot.com/_qEs9r36R5kw/S63QM-0V6kI/AAAAAAAAArY/9AQI1izF9Wk/s320/Picture+2.png'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import scipy\n", "Image(scipy.ndimage.gaussian_filter(img, sigma=3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(io.imread('http://tracer.lcc.uma.es/problems/mfp/MedianFilter.jpg'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(filters.rank.median(img, ones([3,3],dtype=uint8)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtr Canny" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = img_as_float(data.imread('img/noisy.png'))\n", "#img = im\n", "figure(figsize=(20,20))\n", "subplot(1,3,1)\n", "io.imshow(img**0.5)\n", "subplot(1,3,2)\n", "io.imshow(filters.sobel(img)**0.5)\n", "subplot(1,3,3)\n", "io.imshow(skimage.feature.canny(img, sigma=3))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }