Commit 1b664797 authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Alignment: move peak extraction to dedicated function



Signed-off-by: Nicola Vigano's avatarNicola VIGANÒ <nicola.vigano@esrf.fr>
parent cc030af1
......@@ -224,6 +224,50 @@ class AlignmentBase(object):
)
return vertex_x
@staticmethod
def extract_peak_region(cc, peak_radius=1, cc_vs=None, cc_hs=None):
"""
Extracts a region around the maximum value.
Parameters
----------
cc: numpy.ndarray
Correlation image.
peak_radius: int, optional
The l_inf radius of the area to extract around the peak. The default is 1.
cc_vs: numpy.ndarray, optional
The vertical coordinates of `cc`. The default is None.
cc_hs: numpy.ndarray, optional
The horizontal coordinates of `cc`. The default is None.
Returns
-------
f_vals: numpy.ndarray
The extracted function values.
fv: numpy.ndarray
The vertical coordinates of the extracted values.
fh: numpy.ndarray
The horizontal coordinates of the extracted values.
"""
img_shape = np.array(cc.shape)
# get pixel having the maximum value of the correlation array
pix_max_corr = np.argmax(cc)
pv, ph = np.unravel_index(pix_max_corr, img_shape)
# select a n x n neighborhood for the sub-pixel fitting (with wrapping)
pv = np.arange(pv - peak_radius, pv + peak_radius + 1) % img_shape[-2]
ph = np.arange(ph - peak_radius, ph + peak_radius + 1) % img_shape[-1]
# extract the (v, h) pixel coordinates
fv = None if cc_vs is None else cc_vs[pv]
fh = None if cc_hs is None else cc_hs[ph]
# extract the correlation values
pv, ph = np.meshgrid(pv, ph, indexing="ij")
f_vals = cc[pv, ph]
return (f_vals, fv, fh)
@staticmethod
def _determine_roi(img_shape, roi_yxhw, do_truncate_horz_pow2):
if roi_yxhw is None:
......@@ -505,22 +549,7 @@ class CenterOfRotation(AlignmentBase):
cc_vs = np.fft.fftfreq(img_shape[-2], 1 / img_shape[-2])
cc_hs = np.fft.fftfreq(img_shape[-1], 1 / img_shape[-1])
# get pixel having the maximum value of the correlation array
pix_max_corr = np.argmax(cc)
pv, ph = np.unravel_index(pix_max_corr, img_shape)
# select a n x n neighborhood for the sub-pixel fitting (with wrapping)
pv = np.arange(pv - peak_fit_radius, pv + peak_fit_radius + 1) % img_shape[-2]
ph = np.arange(ph - peak_fit_radius, ph + peak_fit_radius + 1) % img_shape[-1]
# extract the (v, h) pixel coordinates
fv = cc_vs[pv]
fh = cc_hs[ph]
# extract the correlation values
pv, ph = np.meshgrid(pv, ph, indexing="ij")
f_vals = cc[pv, ph]
(f_vals, fv, fh) = self.extract_peak_region(cc, peak_radius=peak_fit_radius, cc_vs=cc_vs, cc_hs=cc_hs)
fitted_shifts_vh = self.refine_max_position_2d(f_vals, fv, fh)
return fitted_shifts_vh[-1] / 2.0
......@@ -638,22 +667,7 @@ class DetectorTranslationAlongBeam(AlignmentBase):
shifts_vh = np.empty((num_imgs - 1, 2))
for ii, cc in enumerate(ccs):
# get pixel having the maximum value of the correlation array
pix_max_corr = np.argmax(cc)
pv, ph = np.unravel_index(pix_max_corr, img_shape)
# select a n x n neighborhood for the sub-pixel fitting (with wrapping)
pv = np.arange(pv - peak_fit_radius, pv + peak_fit_radius + 1) % img_shape[-2]
ph = np.arange(ph - peak_fit_radius, ph + peak_fit_radius + 1) % img_shape[-1]
# extract the (v, h) pixel coordinates
fv = cc_vs[pv]
fh = cc_hs[ph]
# extract the correlation values
pv, ph = np.meshgrid(pv, ph, indexing="ij")
f_vals = cc[pv, ph]
(f_vals, fv, fh) = self.extract_peak_region(cc, peak_fit_radius=peak_fit_radius, cc_vs=cc_vs, cc_hs=cc_hs)
shifts_vh[ii, :] = self.refine_max_position_2d(f_vals, fv, fh)
if equispaced_increments:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment