# let's make a test for subpixel localization
import numpy as np
import matplotlib.pyplot as plt
from openpiv.pyprocess import find_first_peak
N = 64
corr = np.zeros((N,N))
corr[2:5,2:5] = 1
corr[3,3] = 2
corr[3,4] = 3
corr[3,5] = 1
corr
array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 1., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]])
pos,height = find_first_peak(corr)
pos,height
((3, 4), 3.0)
from openpiv.pyprocess import find_subpixel_peak_position
find_subpixel_peak_position(corr)
(3.0, 3.769577293545741)
# peak on the border
corr = np.zeros((N,N))
corr[:3,:3] = 1
corr[0,0] = 2
corr[0,2] = 3
corr[0,3] = 1
corr
array([[2., 1., 3., ..., 0., 0., 0.], [1., 1., 1., ..., 0., 0., 0.], [1., 1., 1., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]])
it is disregarded in our function because we cannot define well the subpixel position. Or do we?
find_subpixel_peak_position(corr)
(32.0, 32.0)
# peak on the border
corr = np.flipud(corr)
corr
array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [1., 1., 1., ..., 0., 0., 0.], [1., 1., 1., ..., 0., 0., 0.], [2., 1., 3., ..., 0., 0., 0.]])
find_subpixel_peak_position(corr)
(32.0, 32.0)
corr = np.fliplr(corr)
corr[-2,-1]=5
corr
array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 1., 1., 1.], [0., 0., 0., ..., 1., 1., 5.], [0., 0., 0., ..., 3., 1., 2.]])
find_subpixel_peak_position(corr)
(32.0, 32.0)
## Corner case 2: zero next to the peak - the log(0) fails
corr = np.zeros((N,N))
corr[2:5,2:5] = 1
corr[3,3] = 2
corr[3,4] = 3
# corr[3,5] = 1
corr
array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 1., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]])
find_subpixel_peak_position(corr)
(3.0, 3.5230088020336483)
fig, ax = plt.subplots(figsize=(12,12))
ax.pcolor(corr[:8,:8])
for eps in np.logspace(-15,5):
i,j = find_subpixel_peak_position(corr+eps)
# print(i,j)
ax.plot(i,j,'rx')
import matplotlib.pyplot as plt
plt.pcolor(corr[:8,:8])
plt.plot(i,j,'ro')
[<matplotlib.lines.Line2D at 0x7f29add10d90>]
for method in ['gaussian','parabolic','centroid']:
i,j = find_subpixel_peak_position(corr,method)
print(i,j)
i,j = find_subpixel_peak_position(corr+eps,method)
print(i,j)
3.0 3.5239508804084627 3.0 3.7499981250225445 3.0 3.75 3.0 3.75 3.0 3.600000095999977 3.0 3.9999933334444426