Giter Site home page Giter Site logo

image-fuzzy-matching's Introduction

Image Fuzzy Matching

原理介绍

此项目是对imagehash这个Python库的延展 原本项目是对两张图片进行对比, 然后给出一个数字结果, 数字越小说明两张图片越相似 但原库的对比方法输入是两张图片, 在实际项目中使用效率比较低

经过阅读源码, 我们们发现imagehash图片对比的基本流程是:
1. 打开图片并通过算法转化成hash值
2. 把hash值转化成numpy array
3. 用numpy.count_nonzero()这个方法对两个numpy array进行对比得出结果

我们发现其实把图片通过imagehash转化为numpy array, 我们可以直接用numpy.count_nonzero()对两个numpy array进行对比也可以得到同样结果, 在实际项目中我们可以直接把这个numpy array可以转化成字符串并进行存储(字节长度小于100), 这样就可以提高使用效率, 具体方法在项目代码中

并且, 通过了解numpy.count_nonzero()这个方法的工作原理有可能实现图片的模糊搜索, 以后有空会做

代码讲解

  1. 把图片用PIL.Image打开
    image = Image.open(path1)
  2. 用本项目中的fuzzy_match.image_2_string()把图片转化成字符串, 这里的字符串就可以进行存储
    image_string = fuzzy_match.image_2_string(image)
  3. 需要对比时就把字符串用fuzzy_match.string_2_npArray()转化成numpy array
    np_array1 = string_2_npArray(image_string)
  4. 用两个numpy array对比得出一个数字结果, 数值越小两张图片越相似
    result = image_fuzzy_match(np_array1, np_array2)

测试用例

我们看下测试用例

Test case 1

1 2

第二张图是第一张图微信截图截出来的, 第一张图少截了一周, 并且把右下角的一个log截掉了, 结果是3, 可以说明两张图是相似的

Test case 2

1 2
这两张图除了裙子上的印花, 大小和其他都是一模一样的, 结果是4

Test case 3

1 2
这张图的大小和人物颜色都不一样, 但是用的是同一个模板, 结果是10

Test case 4

1 2
这组是拿第一组测试图片和第二组测试图片各一张图进行对比, 因为是完全不一样的图, 结果是41

结果可以看出来相似图片的对比数值基本在个位数左右

image-fuzzy-matching's People

Contributors

xleex avatar

Stargazers

 avatar  avatar

Watchers

Lee.420 avatar

Forkers

liqiujiong

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.