V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
18870715400
V2EX  ›  Python

请问一下 Python 有什么比较好的图片比对,

  •  
  •   18870715400 · 2019-12-05 19:28:09 +08:00 · 3549 次点击
    这是一个创建于 1574 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如有一张大截图, 还有一个小图标的图片,python 有什么算法或者库可以比较这张大截图当中有没有这个小图标

    10 条回复    2019-12-06 00:24:20 +08:00
    delectate
        1
    delectate  
       2019-12-05 19:33:27 +08:00   ❤️ 2
    ClericPy
        2
    ClericPy  
       2019-12-05 19:34:11 +08:00
    简单识别可以用 PIL / pillow 那边的, 带容错率的也有, 可以查查相关的, 以前用过基于它的以图找坐标的自动化库
    18870715400
        3
    18870715400  
    OP
       2019-12-05 19:42:14 +08:00
    非常感谢
    0x5f
        4
    0x5f  
       2019-12-05 19:49:11 +08:00
    用 sift 算法去做匹配
    imn1
        5
    imn1  
       2019-12-05 20:02:13 +08:00
    很简单,几行而已
    img_rgb = cv2imread(image)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = target
    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
    threshold = 0.9
    loc = numpy.where( res >= threshold)

    image 是待检查图片路径(截图)
    target 是参考图片路径(小图)
    loc 是结果,表示 target 在 image 内的位置(可以是 0 至多个),可以用 len 判断 T/F
    threshold 的大小,是判断临界值,准确度
    cv2imread 是个修改的函数,只是做了非 ASCII 路径兼容而已,其实就是 opencv 的 imread,这里就不贴了

    相关依赖的安装自理

    没有记下来源,某个专门写图像识别的洋人 blog
    aadebuger
        6
    aadebuger  
       2019-12-05 20:16:22 +08:00
    pip install airtest 就可以。 你的需求肯定是手机的截图
    imn1
        7
    imn1  
       2019-12-05 20:18:01 +08:00
    这里说一下,一张大图里面找小图,例如集体合照里面找某个人的人脸这种
    用 similar image 方式是不准确的,因为 similar image 是整图比较的——
    显然,除去小图的部分,其他的部分,全部都变成干扰(差异)因素了
    areless
        8
    areless  
       2019-12-05 23:17:20 +08:00
    hamming distance
    github 关键词 image hash
    https://github.com/search?q=images+hash
    imn1
        9
    imn1  
       2019-12-06 00:20:43 +08:00   ❤️ 1
    OK,我的错,copy 代码没留意灰度预处理的部分(我代码图像预处理是写在一个类里面,忘了)
    修正 #3

    应该是这个,简单测试了几幅图
    ------------------------------------------
    #!/usr/bin/env python3
    # -*-coding:utf-8 -*-

    import numpy
    from cv2 import cv2

    image = r'c:\temp\1.jpg'
    # target = r'c:\temp\2.jpg'
    # target = r'c:\temp\1142d03a4b21edd545812af46e7f84cc.jpg'
    target = r'c:\temp\Download-HD-Bamboo-Wallpapers.jpg'
    # target = r'c:\temp\Free-HD-Bamboo-Wallpapers-Download.jpg'
    img_gray = cv2.imread(target, cv2.IMREAD_GRAYSCALE)
    tImg = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
    h, w = tImg.shape[:2]
    h0 = h//4
    h1 = h-h//4
    w0 = w//4
    w1 = w-w//4
    template = tImg[h0:h1, w0:w1]
    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
    threshold = 0.9
    loc = numpy.where( res >= threshold)
    print(loc)
    if len(loc[0]): print("True")
    else: print("False")
    ------------------------------------------------
    注释同上
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5446 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:50 · PVG 16:50 · LAX 01:50 · JFK 04:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.