【问题标题】:is there way to compare two images using jimp for node js有没有办法使用 jimp for node js 比较两个图像
【发布时间】:2020-10-22 23:34:43
【问题描述】:

我是 node js 的初学者,所以我正在为我的项目寻找一种使用 jimp 比较两个图像的方法。我想知道这是否可能,是否可能的代码和方法,或者是否有其他方法可以做到这一点。

【问题讨论】:

    标签: node.js image image-processing compare jimp


    【解决方案1】:

    这是一个比较图像的例子,我们可以使用像素距离或汉明距离进行比较。

    这与 Jimp 文档中的示例非常相似,尽管我们是从在线资源加载图像。

    您可以玩弄阈值,我们在这里使用的阈值与 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      相关资源
      最近更新 更多