auto-image-diff supports advanced feature-based image alignment using OpenCV.js. This is an optional feature that provides more sophisticated alignment capabilities beyond the default ImageMagick methods.
OpenCV support is automatically set up when you install auto-image-diff:
npm install -g auto-image-diffThe @techstark/opencv-js package is included as a dependency and will be installed automatically.
OpenCV uses computer vision algorithms to detect distinctive features in images:
- ORB (Oriented FAST and Rotated BRIEF) - Fast and efficient
- AKAZE (Accelerated-KAZE) - More accurate but slower
- BRISK (Binary Robust Invariant Scalable Keypoints) - Good balance
The detected features are matched between images to find correspondences.
A transformation matrix is calculated to align the images based on matched features.
To use OpenCV-based alignment, specify the -m opencv option:
# Basic OpenCV alignment
aid compare image1.png image2.png output/ -m opencv
# Use AKAZE detector for better accuracy
aid compare image1.png image2.png output/ -m opencv --opencv-detector akaze
# Use BRISK detector
aid compare image1.png image2.png output/ -m opencv --opencv-detector briskOpenCV alignment is beneficial for:
- Images with rotation or perspective changes
- Complex transformations beyond simple translation
- Images with distinctive features (corners, edges, patterns)
- When ImageMagick methods fail to find good alignment
If OpenCV fails to initialize or find alignment, the tool automatically falls back to ImageMagick methods:
- Subimage search
- Phase correlation
- Edge-based alignment
If you see "OpenCV feature alignment failed", check:
- Node.js version (requires Node 14+)
- Reinstall the package:
npm install -g auto-image-diff
If OpenCV alignment produces poor results:
- Try different detectors (ORB → AKAZE → BRISK)
- Ensure images have sufficient distinctive features
- Use ImageMagick methods for simple translations
OpenCV feature matching is more computationally intensive than ImageMagick methods:
- ORB: Fastest, good for most cases
- BRISK: Moderate speed, better accuracy
- AKAZE: Slowest, best accuracy
OpenCV.js is a WebAssembly module that requires asynchronous initialization. The implementation:
- Lazy loads OpenCV on first use
- Caches the loaded module
- Falls back gracefully if loading fails
OpenCV operations use more memory than ImageMagick. For batch processing of many images, consider:
- Using
--no-parallelto process sequentially - Reducing concurrency with
-c 2
# 1. Try default alignment first
aid compare before.png after.png results/
# 2. If alignment is poor, try OpenCV
aid compare before.png after.png results-opencv/ -m opencv
# 3. Try different detector if needed
aid compare before.png after.png results-akaze/ -m opencv --opencv-detector akaze
# 4. Compare results
open results/diff.png results-opencv/diff.png results-akaze/diff.pngconst { ImageProcessor } = require('auto-image-diff');
const processor = new ImageProcessor();
// Use OpenCV alignment
const result = await processor.alignImages(
'reference.png',
'target.png',
'aligned.png',
{
method: 'opencv',
opencvDetector: 'akaze' // optional: 'orb', 'akaze', 'brisk'
}
);