我有截图,我需要找到一些图像(总是相同的大小,旋转角度等)。我找到了一些PIL和numpy的解决方案,但它们只适用于非透明图像。我必须找到类似圆圈的东西,所以我必须在它后面使用透明背景。
示例图像如下所示:
我正在寻找像以下目标:
任何想法我怎样才能做到这一点?
答案 0 :(得分:3)
由于您要精确匹配图像,因此可以轻松创建滑块来查找目标。这不是最快的解决方案,但它很容易上班。
import numpy as np
from scipy.misc import imread
screen = imread("screen.png")
target = imread("cookie.png")
def find_match(screen, target):
dx,dy,_ = target.shape
for x in xrange(screen.shape[0]-dx):
for y in xrange(screen.shape[1]-dy):
diff = (screen[x:x+dx,y:y+dy,:]-target)
dz = np.abs(diff).sum()
if dz == 0: return x,y, dx, dy
return None, None, None, None
x,y,dx,dy = find_match(screen, target)
# Show the result
import pylab as plt
plt.subplot(121)
plt.imshow(screen)
screen_sample = np.copy(screen)
screen_sample[x:x+dx,y:y+dy,:] = 0
plt.subplot(122)
plt.imshow(screen_sample)
plt.tight_layout()
plt.show()
答案 1 :(得分:1)
感谢您的回复! 我再次分析了上面发布的主题并得到了一些想法。您的代码很简单,但执行需要大约40秒。我做到了这一点:
def search(screen, img):
sx, sy = screen.size
ix, iy = img.size
for xstart in range(sx - ix):
for ystart in range(sy - iy):
#search for the pixel on the screen that equals the pixel at img[0:0]
if img.getpixel((0,0)) == screen.getpixel((xstart, ystart)):
match = 1 #temporary
for x in range(ix): #check if first row of img is on this coords
if img.getpixel((x,0)) <> screen.getpixel((xstart+x, ystart)):
match = 0 #if there's any difference, exit the loop
break
if match == 1: #otherwise, if this coords matches the first row of img
for x in range(ix):
for y in range(iy):
#check every pixel of the img
if img.getpixel((x,y)) <> screen.getpixel((xstart+x, ystart+y)):
match = 0 #any difference - break
break
if match == 1: return (xstart, ystart) #return top-left corner coordinates
return (-1,-1) #or this, if not found
它使用了很慢的getpixel方法,但它在~4s内执行,我很高兴。感谢您定位我!
此致 mopsiok