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

2019-12-05 19:28:09 +08:00
 18870715400

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

3570 次点击
所在节点    Python
10 条回复
delectate
2019-12-05 19:33:27 +08:00
ClericPy
2019-12-05 19:34:11 +08:00
简单识别可以用 PIL / pillow 那边的, 带容错率的也有, 可以查查相关的, 以前用过基于它的以图找坐标的自动化库
18870715400
2019-12-05 19:42:14 +08:00
非常感谢
0x5f
2019-12-05 19:49:11 +08:00
用 sift 算法去做匹配
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
2019-12-05 20:16:22 +08:00
pip install airtest 就可以。 你的需求肯定是手机的截图
imn1
2019-12-05 20:18:01 +08:00
这里说一下,一张大图里面找小图,例如集体合照里面找某个人的人脸这种
用 similar image 方式是不准确的,因为 similar image 是整图比较的——
显然,除去小图的部分,其他的部分,全部都变成干扰(差异)因素了
areless
2019-12-05 23:17:20 +08:00
hamming distance
github 关键词 image hash
https://github.com/search?q=images+hash
imn1
2019-12-06 00:20:43 +08:00
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")
------------------------------------------------
注释同上
lovestudykid
2019-12-06 00:24:20 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/626314

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX