phaseCorrelate
import { phaseCorrelate } from '@banou/opencv-wasm'Use after await initOpenCV(). See the initialization and named imports guide.
example: samples/cpp/snippets/phase_corr.cpp An example using the phaseCorrelate function The function is used to detect translational shifts that occur between two images.
The operation takes advantage of the Fourier shift theorem for detecting the translational shift in the frequency domain. It can be used for fast image registration as well as motion estimation. For more information please see https://en.wikipedia.org/wiki/Phase_correlation
Calculates the cross-power spectrum of two supplied source arrays. The arrays are padded if needed with getOptimalDFTSize.
The function performs the following equations:
- First it applies a Hanning window to each image to remove possible edge effects, if it's provided
by user. See
createHanningWindowand https://en.wikipedia.org/wiki/Hann_function. This window may be cached until the array size changes to speed up processing time. - Next it computes the forward DFTs of each source array:
\mathbf{G}_a = \mathcal{F}\{src_1\}, \; \mathbf{G}_b = \mathcal{F}\{src_2\}
where \mathcal{F} is the forward DFT.
- It then computes the cross-power spectrum of each frequency domain array:
R = \frac{ \mathbf{G}_a \mathbf{G}_b^*}{|\mathbf{G}_a \mathbf{G}_b^*|}
- Next the cross-correlation is converted back into the time domain via the inverse DFT:
r = \mathcal{F}^{-1}\{R\}
- Finally, it computes the peak location and computes a 5x5 weighted centroid around the peak to achieve sub-pixel accuracy.
(\Delta x, \Delta y) = \texttt{weightedCentroid} \{\arg \max_{(x, y)}\{r\}\}
- If non-zero, the response parameter is computed as the sum of the elements of r within the 5x5 centroid around the peak location. It is normalized to a maximum of 1 (meaning there is a single peak) and will be smaller when there are multiple peaks.
See: dft, getOptimalDFTSize, idft, mulSpectrums createHanningWindow
phaseCorrelate(src1: Mat, src2: Mat, window: Mat): phaseCorrelateResult;2 available overloads
phaseCorrelate(src1: Mat, src2: Mat): phaseCorrelateResult;phaseCorrelate(src1: Mat, src2: Mat, window: Mat): phaseCorrelateResult;src1Source floating point array (CV_32FC1 or CV_64FC1)
src2Source floating point array (CV_32FC1 or CV_64FC1)
windowFloating point array with windowing coefficients to reduce edge effects (optional).
detected phase shift (sub-pixel) between the two arrays. Scalar output parameters are returned as named fields in this object. Release returned native handles with using or delete(), including handles nested in results.
These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.