Image Processing

65 downloads 0 Views 3MB Size Report
python code import numpy as np import scipy.misc as msc import scipy.ndimage as img sigma = ... alpha = ... f = msc.imread('input.png').astype('float').
Image Processing Prof. Christian Bauckhage

outline lecture 16

fun with Gaussian smoothing

summary

fun with Gaussian smoothing

unsharp masking

given an image f [x, y] and α > 0, compute  s[x, y] = Gσ ∗ f [x, y] r[x, y] = f [x, y] − s[x, y] g[x, y] = f [x, y] + α r[x, y]

terminology 0 < α < 1 ⇔ unsharp masking

α > 1 ⇔ highboost filtering

example

f

s = Gσ ∗ f

r =f −s

g = f + αr

Gaussian glow

f

s = Gσ ∗ f

g = α f + (1 − α) s

examples for σ ∈ {2.5, 5.0, 7.5}, α ∈ {0.7, 0.8, 0.9}

python code

import numpy as np import scipy.misc as msc import scipy.ndimage as img

sigma = ... alpha = ... f = msc.imread(’input.png’).astype(’float’) s = img.filters.gaussian_filter(f, sigma=sigma) g = alpha * f + (1.-alpha) * s msc.toimage(g, cmin=0, cmax=255).save(’output.png’)

interesting effect for α > 1 (e.g. α = 4)

f

s = Gσ ∗ f

g = α f + (1 − α) s

Gaussian edge glow

f

g = k∇Gσ ∗ f k

h = f + α g, α = 6

python code

import numpy as np import scipy.misc as msc import scipy.ndimage as img

sigma = ... alpha = ... f = msc.imread(’input.png’).astype(’float’) g = img.filters.gaussian_gradient_magnitude(f, sigma=sigma) h = f + alpha * g msc.toimage(h, cmin=0, cmax=255).save(’output.png’)

Gaussian radioactive edge glow

python code

import numpy as np import scipy.misc as msc import scipy.ndimage as img

sigma = ... alpha = ... f = msc.imread(’input.png’).astype(’float’) g = img.filters.gaussian_gradient_magnitude(f, sigma=sigma) R = f G = f + alpha * g B = f h = np.array([R, G, B]) msc.toimage(h, cmin=0, cmax=255).save(’output.png’)

image aging

original

smoothed

noise added

sepia toning

python code

import numpy as np import scipy.ndimage as img

sigmanoise = ... sigmasmooth = ... y,x = f.shape # first blur, then add noise g = img.filters.gaussian_filter(f, sigma=simgasmooth) h = g + np.random.randn(y,x) * sigmanoise # m R G B

sepia toning = h.mean() # average = h - m + 162 # set = h - m + 138 # set = h - m + 101 # set

...

intensity in image h average red to 162 average green to 138 average blue to 101

angular Gaussian blur

f [x, y]

F[r, ϕ]

blur along ϕ axis

angular Gaussian blur

f [x, y]

F[r, ϕ]

blur along ϕ axis

g[x, y]

radial Gaussian blur

f [x, y]

F[r, ϕ]

blur along r axis

radial Gaussian blur

f [x, y]

F[r, ϕ]

blur along r axis

g[x, y]

angular Gaussian blur

f [x, y]

g[x, y]

radial Gaussian blur

f [x, y]

g[x, y]

python code

;-)

difference of Gaussians (DoG)

consider two Gaussians 2

− x2 1 Gσ1 (x) = √ e 2σ1 2πσ1

2

− x2 1 and Gσ2 (x) = √ e 2σ2 2πσ2

and their difference DoG(x) = Gσ1 (x) − Gσ2 (x) σ=3 σ=7 DoG

0.12 0.08 0.04 0 −0.04

−20

−10

0

10

20

example

original

σ1 = 4.5, σ2 = 9

σ1 = 9, σ2 = 18

σ1 = 18, σ2 = 36

derivatives as convolutions

if we define (contrary to what we did earlier in this course) d ! f [x] = [-11] ∗ f [x] dx then   d2 f [x] = [-11] ∗ [-11] ∗ f [x] = [1-21] ∗ f [x] dx2

Laplacians

given a continuous 2D function f (x, y), the function

∆ f (x, y) = ∇2 f (x, y) =

∂2 ∂2 f (x, y) + f (x, y) ∂x2 ∂y2

is called the Laplacian of f (x, y)

Laplacians

for discrete 2D function f [x, y] with ∂2 f = f [x − 1, y] − 2 f [x, y] + f [x + 1, y] ∂x2 ∂2 f = f [x, y − 1] − 2 f [x, y] + f [x, y + 1] ∂y2

we find ∇2 f [x, y] =f [x − 1, y] + f [x + 1, y]+

f [x, y − 1] + f [x, y + 1] − 4 f [x, y]

Laplacians

written as a discrete 2D filter kernel, the discrete Laplacian amounts to 

 0 1 0 L[x, y] = 1 -4 1 0 1 0

and we have  ∇2 f [x, y] = L ∗ f [x, y]

note

apparently, there are several possible definitions for discrete gradient operators . . . some of the more common operators are called Sobel, Canny, Preweitt, or Roberts gradient depending on the choice of gradient, the discrete Laplacian may assume different forms

note

the Laplacian of an image reveals regions of rapidly changing intensities and is thus often used for edge detection

note

the Laplacian of an image reveals regions of rapidly changing intensities and is thus often used for edge detection yet, as a second derivative operator, it is sensitive to noise

note

the Laplacian of an image reveals regions of rapidly changing intensities and is thus often used for edge detection yet, as a second derivative operator, it is sensitive to noise to increase robustness, one therefore often performs Gaussian smoothing prior to computing the Laplacian

g[x, y] = L ∗ G ∗ f [x, y]



Laplacians of Gaussians

when computing g = L ∗ G ∗ f , it is beneficial to compute the Laplacian of Gaussian  LoG[x, y] = L ∗ G [x, y]   2 2 x 2 + y2 1 − x +y2 2σ e =− 4 1− πσ 2σ2 beforehand and then to compute  g[x, y] = LoG ∗ f [x, y]

note

to further simplify computation, the Laplacian of Gaussian LoG is often approximated by means of a suitably chosen difference of Gaussians DoG

DoG for blob detection

DoG for blob detection

python code

import numpy as np import scipy.ndimage as img sigs = np.array([1.2, 1.4, 1.6, 1.8, 2.0, 2.4, 2.8, 3.2, 3.6, 4.0, 4.8, 5.6, 6.4, 7.2, 8.0]) * 5. # compute list of Gaussian blurred images imgs = [] for s in sigs: g = img.filters.gaussian_filter(f, s, ’constant’, cval=255) imgs.append(g) # compute list of DoG images dogs = [] for i in range(len(imgs)-1): d = (imgs[i] - imgs[i+1]) dogs.append(d)

python code (cont.)

# turn list of DoG images into 3D array stack = np.array(dogs) # determine strong local minima and maxima (i.e. keypoints) maxvals = img.maximum_filter(stack, 3) minvals = img.minimum_filter(stack, 3) maxvals[maxvals< 3.] = -np.inf minvals[minvals>-3.] = np.inf maxima = (stack == maxvals) minima = (stack == minvals) # determine array indices (i.e. locations) of keypoints kpinds = np.where((maxima==True) | (minima==True))

DoG for detecting focus objects

DoG for detecting focus objects

greyscale image

DoG for detecting focus objects

greyscale image

DoG, σ1 = 0.5, σ2 = 1.0

DoG for detecting focus objects

greyscale image

non-maximum suppression

DoG, σ1 = 0.5, σ2 = 1.0

DoG for detecting focus objects

greyscale image

DoG, σ1 = 0.5, σ2 = 1.0

non-maximum suppression

morphological hole filling

python code

# convert color image f to grey scale image h R, G, B = f[:,:,0], f[:,:,1], f[:,:,2] h = 0.2989 * R + 0.5870 * G + 0.1140 * B # i j k

compute DoG image k = img.filters.gaussian_filter(h, sigma=0.5, mode=’reflect’) = img.filters.gaussian_filter(h, sigma=1.0, mode=’reflect’) = i-j

# suppress small values in k and turn it into binary image k = np.abs(k) k = np.where(k