如何使用 Node.js 比较两个图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18510897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two images using Node.js
提问by SergeyB
I am looking for a way to compare two images to see how similar they are. Googling it produces tons of image processing results (cropping, re-sizing, etc.), but nothing that would do approximate comparisons of images. There is one Node.js library, but it is version 0.0.1 and relies on various 3rd party system packages, so not stable or portable.
我正在寻找一种方法来比较两个图像以查看它们的相似程度。谷歌搜索它会产生大量的图像处理结果(裁剪、重新调整大小等),但没有任何东西可以对图像进行近似比较。有一个 Node.js 库,但它是 0.0.1 版本,并且依赖于各种 3rd 方系统包,因此不稳定或可移植。
Something along these lines:
沿着这些路线的东西:
var imgComparator = require('some-awesome-image-comparator-module');
// result would be between 1.0 and 0.0, where 1.0 would mean exact match
var result = imgComparator.compare('/path/to/image/1.png', '/path/to/image/2.png');
采纳答案by moka
There is node-opencvmodule, you might use it in order to perform heavy operation like image comparison. Good topic on that is here: Simple and fast method to compare images for similarity
有node-opencv模块,你可以使用它来执行像图像比较这样的繁重操作。好话题就在这里:简单快速的方法来比较图像的相似性
回答by Linus Unneb?ck
There is also image-diffwhich looks very promising, it's made by Uber.
还有image-diff看起来很有前途,它是由 Uber 制作的。
var imageDiff = require('image-diff')
imageDiff({
actualImage: 'checkerboard.png',
expectedImage: 'white.png'
}, function (err, imagesAreSame) {
// error will be any errors that occurred
// imagesAreSame is a boolean whether the images were the same or not
// diffImage will have an image which highlights differences
})
回答by Dan
I have found this library, which may be useful for you
我找到了这个库,它可能对你有用
回答by Emerica
image-diffis deprecated.
不推荐使用图像差异。
From their github:
从他们的github:
We no longer have any active maintainers on this project and as a result have halted maintenance.
As a replacement, please see alternative projects like looks-same and pixelmatch:
我们不再有此项目的任何活跃维护者,因此已停止维护。
作为替代,请查看替代项目,如外观相同和像素匹配:
I personally use pixelmatch:
我个人使用pixelmatch:
The smallest, simplest and fastest JavaScript pixel-level image comparison library, originally created to compare screenshots in tests.
Features accurate anti-aliased pixels detection and perceptual color difference metrics.
Inspired by Resemble.js and Blink-diff. Unlike these libraries, pixelmatch is around 150 lines of code, has no dependencies, and works on raw typed arrays of image data, so it's blazing fast and can be used in any environment (Node or browsers).
最小、最简单、最快的 JavaScript 像素级图像比较库,最初创建用于比较测试中的屏幕截图。
具有准确的抗锯齿像素检测和感知色差指标。
受 Resemble.js 和 Blink-diff 的启发。与这些库不同,pixelmatch 大约有 150 行代码,没有依赖项,并且可以处理原始类型的图像数据数组,因此它非常快,可以在任何环境(Node 或浏览器)中使用。
const fs = require('fs');
const PNG = require('pngjs').PNG;
const pixelmatch = require('pixelmatch');
const img1 = PNG.sync.read(fs.readFileSync('img1.png'));
const img2 = PNG.sync.read(fs.readFileSync('img2.png'));
const {width, height} = img1;
const diff = new PNG({width, height});
const difference = pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1});
fs.writeFileSync('diff.png', PNG.sync.write(diff)); // see diff.png for the difference
const compatibility = 100 - dif * 100 / (width * height);
console.log(`${difference} pixels differents`);
console.log(`Compatibility: ${compatibility}%`);
Find a demo here: https://observablehq.com/@mourner/pixelmatch-demo
在此处查找演示:https: //observablehq.com/@mourner/pixelmatch-demo

