UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

33 lines 1.33 kB
import { getHammingDistance } from './getHammingDistance.js'; /** * Find the best match for each of the source descriptors using brute force matching. * @param source - Source descriptors. * @param destination - Destination descriptors. * @param options - Brute force amtch options. * @returns The best match for each source descriptor. */ export function bruteForceOneMatch(source, destination, options = {}) { const { sort = !!options.nbBestMatches, nbBestMatches = source.length } = options; const matches = []; for (let sourceIndex = 0; sourceIndex < source.length; sourceIndex++) { let minDistance = Number.POSITIVE_INFINITY; let index = 0; for (let destinationIndex = 0; destinationIndex < destination.length; destinationIndex++) { const distance = getHammingDistance(source[sourceIndex], destination[destinationIndex]); if (distance < minDistance) { minDistance = distance; index = destinationIndex; } } matches.push({ sourceIndex, destinationIndex: index, distance: minDistance, }); } if (sort) { matches.sort((a, b) => a.distance - b.distance); } return matches.slice(0, nbBestMatches); } //# sourceMappingURL=bruteForceMatch.js.map