简体   繁体   English

有没有办法使用 jimp for node js 比较两个图像

[英]is there way to compare two images using jimp for node js

I am a beginner in node js, so I am looking for a way to compare two images using jimp for my project.我是 node js 的初学者,所以我正在寻找一种方法来使用 jimp 为我的项目比较两个图像。 I want to know is that possible, if it is possible the code and method of doing it, or is there another way to do that.我想知道这是否可能,是否有可能的代码和方法,或者是否有其他方法可以做到这一点。

Here's an example of comparing images, we can compare using pixel distance or hamming distance.这是比较图像的示例,我们可以使用像素距离或汉明距离进行比较。

This is very similar to an example from the Jimp docs, though we're loading the images from an online source.这与 Jimp 文档中的示例非常相似,尽管我们是从在线资源加载图像。

You could play around with the thresholds, we're using the same here as Jimp uses in its demo.您可以使用阈值,我们在这里使用与 Jimp 在其演示中使用的相同。

const Jimp = require('jimp');

async function compareImages(image1Url, image2Url) {

    const image1 = await Jimp.read(image1Url);
    const image2 = await Jimp.read(image2Url);
    // Perceived distance
    const distance = Jimp.distance(image1, image2);
    // Pixel difference
    const diff = Jimp.diff(image1, image2);
    
    console.log(`compareImages: distance: ${distance.toFixed(3)}, diff.percent: ${diff.percent.toFixed(3)}`);
    if (distance < 0.15 || diff.percent < 0.15) {
        console.log("compareImages: Images match!");
        return true;
    } else {
        console.log("compareImages: Images do NOT match!");
        return false;
    }
}

const usFlag = "https://flaglane.com/download/american-flag/american-flag-small.jpg";
const canadianFlagJpg = "https://flaglane.com/download/canadian-flag/canadian-flag-small.jpg";
const canadianFlagPng = "https://flaglane.com/download/canadian-flag/canadian-flag-small.png";

// These should not match.
compareImages(usFlag, canadianFlagJpg);

// These should match.
compareImages(canadianFlagJpg, canadianFlagPng);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM