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() );
323 if( maxLag_r > m_maxLag && m_maxLag != 0 )
325 if( maxLag_c > m_maxLag && m_maxLag != 0 )
328 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();
340 m_normIm = im.block( r0, c0, m_refIm.rows(), m_refIm.cols() ) * m_maskIm;
344 m_normIm = im.block( r0, c0, m_refIm.rows(), m_refIm.cols() );
351 m_ccIm( maxLag_r + rL, maxLag_c + cL ) = 0;
355 m_normIm = ( m_normIm - m ) / sv;
356 m_ccIm( maxLag_r + rL, maxLag_c + cL ) = ( m_normIm * m_refIm ).sum();
362 if( m_peakMethod == xcorrPeakMethod::gaussfit )
365 Scalar pk = m_ccIm.maxCoeff( &xLag0, &yLag0 );
366 Scalar mn = m_ccIm.minCoeff();
367 m_fitter.setArray( m_ccIm.data(), m_ccIm.rows(), m_ccIm.cols() );
368 m_fitter.setGuess( mn, pk, xLag0, yLag0, 0.2 * m_ccIm.rows(), 0.2 * m_ccIm.cols(), 0 );
371 xShift = m_fitter.x0() - maxLag_r;
372 yShift = m_fitter.y0() - maxLag_c;
374 else if( m_peakMethod == xcorrPeakMethod::interp )
376 m_magIm.resize( m_magSize, m_magSize );
379 m_magIm.maxCoeff( &x, &y );
380 xShift = x * m_tol - maxLag_r;
381 yShift = y * m_tol - maxLag_c;
387 xShift = x - maxLag_r;
388 yShift = y - maxLag_c;