Correlation, Convolution, Filtering COMPSCI 527 Computer Vision - - PowerPoint PPT Presentation

correlation convolution filtering
SMART_READER_LITE
LIVE PREVIEW

Correlation, Convolution, Filtering COMPSCI 527 Computer Vision - - PowerPoint PPT Presentation

Correlation, Convolution, Filtering COMPSCI 527 Computer Vision COMPSCI 527 Computer Vision Correlation, Convolution, Filtering 1 / 26 Outline 1 Template Matching and Correlation 2 Image Convolution 3 Filters 4 Separable Convolution


slide-1
SLIDE 1

Correlation, Convolution, Filtering

COMPSCI 527 — Computer Vision

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 1 / 26

slide-2
SLIDE 2

Outline

1 Template Matching and Correlation 2 Image Convolution 3 Filters 4 Separable Convolution

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 2 / 26

slide-3
SLIDE 3

Template Matching and Correlation

Template Matching

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 3 / 26

slide-4
SLIDE 4

Template Matching and Correlation

Normalized Cross-Correlation

ρ(r, c) = τ Tω(r, c) τ = t − mt t − mt and ω(r, c) = w(r, c) − mw(r,c) w(r, c) − mw(r,c) −1 ≤ ρ(r, c) ≤ 1 ρ = 1 ⇔ W(r, c) = αT + β , α > 0 ρ = −1 ⇔ W(r, c) = αT + β , α < 0

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 4 / 26

slide-5
SLIDE 5

Template Matching and Correlation

Results

A numpy warning: Slices are views, not copies

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 5 / 26

slide-6
SLIDE 6

Template Matching and Correlation

Cross-Correlation

(without normalization) j(r, c) = tTw(r, c)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 6 / 26

slide-7
SLIDE 7

Template Matching and Correlation

Code, Math

for r = 1:m for c = 1:n J(r, c) = 0 for u = -h:h for v = -h:h J(r, c) = J(r, c) + T(u, v) * I(r+u, c+v) end end end end

J(r, c) =

h

  • u=−h

h

  • v=−h

I(r + u, c + v)T(u, v)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 7 / 26

slide-8
SLIDE 8

Image Convolution

Convolution

Correlation: J(r, c) =

h

  • u=−h

h

  • v=−h

I(r + u, c + v)T(u, v) Convolution: J(r, c) =

h

  • u=−h

h

  • v=−h

I(r−u, c−v)H(u, v) Same as J(r, c) =

h

  • u=−h

h

  • v=−h

I(r+u, c+v)H(−u, −v) Convolution with H(u, v) is correlation with H(−u, −v)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 8 / 26

slide-9
SLIDE 9

Image Convolution

What’s the Big Deal?

Simplify J(r, c) =

h

  • u=−h

h

  • v=−h

I(r − u, c − v)H(u, v) to J(r, c) =

  • u=−∞

  • v=−∞

I(r − u, c − v)H(u, v) Changes of variables u ← r − u and v ← c − v J(r, c) =

  • u=−∞

  • v=−∞

H(r − u, c − v)I(u, v) Convolution commutes: I ∗ H = H ∗ I (Correlation does not)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 9 / 26

slide-10
SLIDE 10

Image Convolution

Importance of Convolution in Mathematics

  • Polynomials: coefficients of product are “full” convolutions
  • f coefficients:

P(x) = p0 + p1x + . . . + pmxm Q(x) = q0 + q1x + . . . + qnxn R(x) = p0q0 + (p0q1 + p1q0)x + . . . + pmqnxm+n

  • Example:

P(x) = p0 + p1x + p2x2 + p3x3 → (p0, p1, p2, p3) Q(x) = q0 + q1x + q2x2 → (q0, q1, q2)

Convolve (p0, p1, p2, p3) with (q0, q1, q2) to get (r0, r1, r2, r3, r4, r5)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 10 / 26

slide-11
SLIDE 11

Image Convolution

Important Consequence

  • Discrete Fourier transform is a polynomial:

p = (p0, . . . , pn−1)

  • F[p](ℓ) = p0 + p1z + . . . + pn−1zn−1 where z = 1

ne−i2πℓ/n

  • All of spectral signal theory follows
  • Example: The Fourier transform of a convolution is the

product of the Fourier transforms

  • [We will not see this]

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 11 / 26

slide-12
SLIDE 12

Image Convolution

Point-Spread Function

T was a template H is called a (convolutional) kernel A.k.a. point-spread function If the image I is a point, then H spreads the point: δ(u, v) = 1 for u = v = 0 elsewhere J(r, c) =

  • u=−∞

  • v=−∞

H(r − u, c − v)δ(u, v) = H(r, c)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 12 / 26

slide-13
SLIDE 13

Image Convolution

More Generally:

δa,b(u, v) = 1 for u = a and v = b elsewhere J(r, c) =

  • u=−∞

  • v=−∞

H(r − u, c − v)δa,b(u, v) = H(r − a, c − b) (No flip in the output!)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 13 / 26

slide-14
SLIDE 14

Image Convolution

Image Boundaries: “Valid” Convolution

If I is m × n and H is k × ℓ, then J is (m − k + 1) × (n − ℓ + 1)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 14 / 26

slide-15
SLIDE 15

Image Convolution

Image Boundaries: “Full” Convolution

If I is m × n and H is k × ℓ, then J is (m+k−1) × (n+ℓ−1) [Pad with either zeros or copies of boundary pixels]

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 15 / 26

slide-16
SLIDE 16

Image Convolution

Image Boundaries: “Same” Convolution

If I is m × n and H is k × ℓ, then J is m × n

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 16 / 26

slide-17
SLIDE 17

Filters

Filters

  • What is convolution for?
  • Smoothing for noise reduction
  • Image differentiation
  • Convolutional Neural Networks (CNNs)
  • . . .
  • We’ll see the first two next, CNNs later
  • Smoothing and differentiation are examples of filtering:

Local, linear image → image transformations

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 17 / 26

slide-18
SLIDE 18

Filters

Smoothing for Noise Reduction

  • Assume: Image varies slowly enough to be locally linear
  • Assume: Noise is zero-mean and white

x x x x x x x x x x x x x x x x x x x x x x x x x 1/5

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 18 / 26

slide-19
SLIDE 19

Filters

2 Dimensions: The Pillbox Kernel

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 19 / 26

slide-20
SLIDE 20

Filters

Issues with the Pillbox

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 20 / 26

slide-21
SLIDE 21

Filters

The Gaussian Kernel

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 21 / 26

slide-22
SLIDE 22

Filters

Gaussian versus Pillbox

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 22 / 26

slide-23
SLIDE 23

Filters

Truncation

G(u, v) = e− 1

2 u2+v2 σ2

  • The larger σ, the more smoothing
  • u, v integer, and cannot keep them all
  • Truncate at 3σ or so

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 23 / 26

slide-24
SLIDE 24

Filters

Normalization

G(u, v) = e− 1

2 u2+v2 σ2

  • We want I ∗ G ≈ I
  • For I = c (constant), I ∗ G = I
  • Normalize by computing γ = 1 ∗ G, and then let G ← G/γ

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 24 / 26

slide-25
SLIDE 25

Separable Convolution

Separability

  • A kernel that satisfies H(u, v) = h(u)ℓ(v) is separable
  • The Gaussian is separable with h = ℓ:

G(u, v) = e− 1

2 u2+v2 σ2

= g(u) g(v) with g(u) = e− 1

2( u σ) 2

  • A separable kernel leads to efficient convolution:

J(r, c) =

h

  • u=−h

k

  • v=−k

H(u, v) I(r − u, c − v) =

h

  • u=−h

h(u)

k

  • v=−k

ℓ(v) I(r − u, c − v) =

h

  • u=−h

h(u) φ(r − u, c) where φ(r, c) =

h

  • v=−h

ℓ(v)I(r, c − v)

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 25 / 26

slide-26
SLIDE 26

Separable Convolution

Computational Complexity

General: J(r, c) = h

u=−h

k

v=−k H(u, v) I(r − u, c − v)

Separable: J(r, c) = h

u=−h h(u) φ(r − u, c) where

φ(r, c) = h

v=−h ℓ(v)I(r, c − v)

Let m = 2h + 1 and n = 2k + 1 General: About 2mn operations per pixel Separable: About 2m + 2n operations per pixel Example:

When m = n (square kernel), the gain is 2m2/4m = m/2 With m = 20: About 80 operations per pixel instead of 800

COMPSCI 527 — Computer Vision Correlation, Convolution, Filtering 26 / 26