python屏幕找图_如何使用python在另一个图像中查找图像

1586010002-jmsa.png

I'm trying to use python to determine if one (small) image is within another (large) image.

Any suggestions before I take myself completely down the wrong path?

/edit: Ok, some ideas: I'm using PIL, and I'm converting each image to the 'P' mode so I can compare each pixel as an integer. I'm trying to implement something like a Boyer–Moore string search or the Knuth–Morris–Pratt algorithm, but in 2 dimensions.

Maybe this will help: instead of searching for ABC in XXXABCXXX (answer=4) we are searching for

ABC

DEF

GHI

in

XXXXX

XABCX

XDEFX

XGHIX

XXXXX

(answer=(2,2))

解决方案

EDIT: Ok, here is the naive way to do this:

import Image, numpy

def subimg(img1,img2):

img1=numpy.asarray(img1)

img2=numpy.asarray(img2)

#img1=numpy.array([[1,2,3],[4,5,6],[7,8,9]])

#img2=numpy.array([[0,0,0,0,0],[0,1,2,3,0],[0,4,5,6,0],[0,7,8,9,0],[0,0,0,0,0]])

img1y=img1.shape[0]

img1x=img1.shape[1]

img2y=img2.shape[0]

img2x=img2.shape[1]

stopy=img2y-img1y+1

stopx=img2x-img1x+1

for x1 in range(0,stopx):

for y1 in range(0,stopy):

x2=x1+img1x

y2=y1+img1y

pic=img2[y1:y2,x1:x2]

test=pic==img1

if test.all():

return x1, y1

return False

small=Image.open('small.tif')

big=Image.open('big.tif')

print subimg(small, big)

It works just fine, but I want to SPEED IT UP. I think the key is in the array 'test' which we might be able to use to skip some positions in the image.

Edit 2: Make sure you use images in a loss-less format to test this.

On Mac, install Pillow and from PIL import Image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值