27 #ifndef imageXCorrDiscrete_hpp
28 #define imageXCorrDiscrete_hpp
30 #include "../mxError.hpp"
31 #include "../math/fit/fitGaussian.hpp"
39 enum class xcorrPeakMethod{ centroid, gaussfit, interp };
62 template<
class _ccImT>
67 typedef typename _ccImT::Scalar
Scalar;
79 bool m_haveMask {
false};
98 xcorrPeakMethod m_peakMethod {xcorrPeakMethod::centroid};
194 template<
class ccImT>
199 template<
class ccImT>
205 template<
class ccImT>
211 template<
class ccImT>
218 template<
class ccImT>
225 template<
class ccImT>
229 m_magSize = ceil(((2.*m_maxLag + 1) - 1.0)/ nt)+1;
231 Scalar mag = (m_magSize-1.0)/((2.*m_maxLag + 1) - 1.0);
236 template<
class ccImT>
245 template<
class ccImT>
251 template<
class ccImT>
257 if(im.rows()!=m_maskIm.rows() && im.cols() != m_maskIm.cols())
259 mxError(
"imageXCorFit::setReference", MXE_SIZEERR,
"reference and mask are not the same size");
272 m_refIm = (im0 - m)/sqrt(v);
277 template<
class ccImT>
283 template<
class ccImT>
289 template<
class ccImT>
295 template<
class ccImT>
301 template<
class ccImT>
308 if( im.rows() <= m_refIm.rows() )
310 mxError(
"imageXCorrDiscrete", MXE_SIZEERR,
"reference must be smaller than target image (rows)");
314 if( im.cols() <= m_refIm.cols() )
316 mxError(
"imageXCorrDiscrete", MXE_SIZEERR,
"reference must be smaller than target image (cols)");
320 int maxLag_r = (im.rows()-m_refIm.rows());
321 int maxLag_c = (im.cols()-m_refIm.cols());
324 if(maxLag_r > m_maxLag && m_maxLag != 0) maxLag_r = m_maxLag;
325 if(maxLag_c > m_maxLag && m_maxLag != 0) maxLag_c = m_maxLag;
327 m_ccIm.resize(2*maxLag_r + 1, 2*maxLag_c + 1);
330 for(
int rL = -maxLag_r; rL <= maxLag_r; ++rL)
332 for(
int cL = -maxLag_c; cL <= maxLag_c; ++cL)
335 int r0 = 0.5*im.rows() + rL-0.5*m_refIm.rows();
336 int c0 = 0.5*im.cols() + cL-0.5*m_refIm.cols();
341 m_normIm = im.block(r0,c0, m_refIm.rows(), m_refIm.cols())*m_maskIm;
345 m_normIm = im.block(r0,c0, m_refIm.rows(), m_refIm.cols());
352 m_ccIm(maxLag_r + rL, maxLag_c + cL) = 0;
356 m_normIm = (m_normIm-m)/sv;
357 m_ccIm(maxLag_r + rL, maxLag_c + cL) = (m_normIm*m_refIm).sum();
365 if(m_peakMethod == xcorrPeakMethod::gaussfit)
368 Scalar pk = m_ccIm.maxCoeff(&xLag0, &yLag0);
369 Scalar mn = m_ccIm.minCoeff();
370 m_fitter.setArray(m_ccIm.data(), m_ccIm.rows(), m_ccIm.cols());
371 m_fitter.setGuess(mn, pk, xLag0, yLag0, 0.2*m_ccIm.rows(), 0.2*m_ccIm.cols(), 0);
374 xShift = m_fitter.x0() - maxLag_r;
375 yShift = m_fitter.y0() - maxLag_c;
377 else if(m_peakMethod == xcorrPeakMethod::interp)
379 m_magIm.resize(m_magSize, m_magSize);
382 m_magIm.maxCoeff(&x,&y);
383 xShift = x*m_tol - maxLag_r;
384 yShift = y*m_tol - maxLag_c;
390 xShift = x - maxLag_r;
391 yShift = y - maxLag_c;
Find the optimum shift to align two images using the discrete cross correlation.
ccImT m_normIm
The normalized image.
_ccImT::Scalar Scalar
the scalar type of the image type
const ccImT & refIm()
Get a reference to the reference image.
int m_maxLag
The maximum lag to consider in the initial cross-correlation.
ccImT m_maskIm
Mask image to use, may be needed for proper normalization even if refIm has 0 mask applied.
ccImT m_magIm
The magnified image, used if m_peakMethod == xcorrPeakMethod::interp.
int maxLag()
Get the current maximum lag.
ccImT m_ccIm
The cross-correlation image.
Scalar m_tol
The tolerance of the interpolated-magnified image, in pixels.
Scalar m_magSize
Magnified size of the ccIm when using interp. Set as function of m_tol and m_maxLag.
_ccImT ccImT
the Eigen-like array type used for image processing
ccImT m_refIm
The normalized reference image.
Scalar tol()
Get the tolerance of the interpolated-magnified image, in pixels.
int operator()(Scalar &xShift, Scalar &yShift, const imT &im)
Conduct the cross correlation to a specified tolerance.
const ccImT & maskIm()
Get a reference to the mask image.
imageXCorrDiscrete()
Default c'tor.
const ccImT & magIm()
Get a reference to the magnified image.
const ccImT & normIm()
Get a reference to the normalized image.
int resize(int nrows, int ncols)
Set the size of the cross-correlation images.
const ccImT & ccIm()
Get a reference to the cross correlation image.
Class to manage fitting a 2D Gaussian to data via the levmarInterface.
@ imageMean
The mean of each image (within the search region) is subtracted from itself.
int imageCenterOfLight(typename imageT::Scalar &x, typename imageT::Scalar &y, const imageT &im)
Calculate the center of light of an image.
imageT::Scalar imageVariance(imageT &im, typename imageT::Scalar mean)
Calculate the variance of an image w.r.t. a given value.
Header for the image processing utilities.