![Page 1: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/1.jpg)
Section 6
Template Matching and Scale Invariant
Feature Descriptors
Presentation by Asem Alaa
SBE404 Computer Vision (Spring 2020) - By Asem Alaa
1 / 15
![Page 2: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/2.jpg)
Template matching
distance metric
output: intensity image
application: crop a single
object (e.g cell) and predict
where other such similar
objects are in the image.
Not rotation invariant
good for approximately
rounded objects
good for regular shapes in
consistent direction.
Find ( ) in
{Dataset: Simulated 24-well plate with synthetic cells (Lehmussola et al., 2007)}
2 / 15
![Page 3: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/3.jpg)
Template matching
Example
h = I =
h = I =
3 / 15
![Page 4: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/4.jpg)
Method 0: Direct 2D correlation of the image
with the template
import numpy as np
from scipy.signal import correlate2d
def match_template_corr( x , temp ):
y = np.empty(x.shape)
y = correlate2d(x,temp,'same')
return y
y[m, n] = ∑k,l
h[k, l]x[m + k, n + l]
False Positives↑↑ False Positives↑↑
4 / 15
![Page 5: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/5.jpg)
Method 1: Direct 2D correlation of the image
with the zero-mean template
def match_template_corr_zmean( x , temp ):
return match_template_corr(x , temp - temp.mean())
y[m, n] = ∑k,l
(h[k, l] − h̄)x[m + k, n + l]
fewer FP
FP↓
5 / 15
![Page 6: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/6.jpg)
Method 2: SSD
We need to avoid for loops!
np.sum(h*h) correlate2d(x,h) correlate2d(x*x,np.ones(h.shape))
y[m, n] = ∑k,l
(h[k, l] − x[m + k, n + l])2
= ∑k,l
h[k, l]2 − 2∑k,l
h[k, l]x[m + k, n + l] +∑k,l
x[m + k, n + l]2
∑k,l h[k, l]2 ∑k,l h[k, l]x[m + k, n + l] ∑k,l x[m + k, n + l]2
6 / 15
![Page 7: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/7.jpg)
Method 2: SSD (cont'd)
We need to avoid for loops!
def match_template_ssd( x , temp ):
term1 = np.sum( np.square( temp ))
term2 = -2*correlate2d(x, temp,'same')
term3 = correlate2d( np.square( x ), np.ones(temp.shape),'same' )
return 1 - np.sqrt(ssd)
Numerical stability?
y[m, n] = ∑k,l
(h[k, l] − x[m + k, n + l])2
= ∑k,l
h[k, l]2 − 2∑k,l
h[k, l]x[m + k, n + l] +∑k,l
x[m + k, n + l]2
7 / 15
![Page 8: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/8.jpg)
Method 2: SSD (cont'd)
We need to avoid for loops!
def match_template_ssd( x , temp ):
term1 = np.sum( np.square( temp ))
term2 = -2*correlate2d(x, temp,'same')
term3 = correlate2d( np.square( x ), np.ones(temp.shape),'same' )
ssd = np.maximum( term1 + term2 + term3 , 0 )
return 1 - np.sqrt(ssd)
y[m, n] = ∑k,l
(h[k, l] − x[m + k, n + l])2
= ∑k,l
h[k, l]2 − 2∑k,l
h[k, l]x[m + k, n + l] +∑k,l
x[m + k, n + l]2
8 / 15
![Page 9: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/9.jpg)
Method 2: SSD (cont'd)
def match_template_ssd( x , temp ):
term1 = np.sum( np.square( temp ))
term2 = -2*correlate2d(x, temp,'same')
term3 = correlate2d( np.square( x ), np.ones(temp.shape),'same' )
ssd = np.maximum( term1 + term2 + term3 , 0 )
return 1 - np.sqrt(ssd)
9 / 15
![Page 10: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/10.jpg)
Method 3: Normalized cross-correlation
In the above image , , , and
γ[u, v] =∑x,y(f[x, y] − ¯fu,v)(t[x − u, y − v] − t̄ )
√∑x,y(f[x, y] − ¯fu,v)2 ∑x,y(t[x − u, y − v] − t̄ )2
u = 2 v = 4 x ∈ 1, 2, 3 y ∈ 3, 4, 5
10 / 15
![Page 11: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/11.jpg)
Method 3: Normalized cross-correlation
(cont'd)
Formula Python
f_c=f-correlate2d(f,np.ones(t.shape)/t.size)
t_c=t-t.mean()
correlate2d( f_c , t_c , 'same' )
correlate2d( f_c * f_c , np.ones(t.shape))
np.sum(t_c * t_c)
γ[u, v] =∑x,y(f[x, y] − ¯fu,v)(t[x − u, y − v] − t̄ )
√∑x,y(f[x, y] − ¯fu,v)2 ∑x,y(t[x − u, y − v] − t̄ )2
fc = (f[x, y] − ¯fu,v)
tc = (t[x − u, y − v] − t̄ )
∑x,y fctc
∑x,y f 2c
∑x,y t2c
11 / 15
![Page 12: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/12.jpg)
Method 3: Normalized cross-correlation
(cont'd)
Division by zero?
def match_template_xcorr( f , t ):
f_c = f - correlate2d( f , np.ones(t.shape)/np.prod(t.shape), 'same')
t_c = t - t.mean()
numerator = correlate2d( f_c , t_c , 'same' )
d1 = correlate2d( np.square(f_c) , np.ones(t.shape), 'same')
d2 = np.sum( np.square( t_c ))
# to avoid sqrt of negative
denumerator = np.sqrt( np.maximum( d1 * d2 , 0 ))
return numerator/denumerator
γ[u, v] =∑x,y(f[x, y] − ¯fu,v)(t[x − u, y − v] − t̄ )
√∑x,y(f[x, y] − ¯fu,v)2 ∑x,y(t[x − u, y − v] − t̄ )2
12 / 15
![Page 13: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/13.jpg)
Method 3: Normalized cross-correlation
(cont'd)
def match_template_xcorr( f , t ):
f_c = f - correlate2d( f , np.ones(t.shape)/np.prod(t.shape), 'same')
t_c = t - t.mean()
numerator = correlate2d( f_c , t_c , 'same' )
d1 = correlate2d( np.square(f_c) , np.ones(t.shape), 'same')
d2 = np.sum( np.square( t_c ))
# to avoid sqrt of negative
denumerator = np.sqrt( np.maximum( d1 * d2 , 0 ))
response = np.zeros( f.shape )
# mask to avoid division by zero
valid = denumerator > np.finfo(np.float32).eps
response[valid] = numerator[valid]/denumerator[valid]
return response
13 / 15
![Page 14: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/14.jpg)
Method 3: Normalized cross-correlation
(cont'd)
14 / 15
![Page 15: S e c t i o n 6 - Tutorials for SBME Students · M e t h o d 2 : S S D ( c o n t ' d ) W e n e e d t o a v o i d f o r l o o p s ! def match_template_ssd( x , temp ): term1 = np.sum(](https://reader035.vdocuments.us/reader035/viewer/2022070901/5f4b2201cc45de11202907d6/html5/thumbnails/15.jpg)
{template_matching.ipnyb}
15 / 15