[英]Finding transparent (png) image inside another image
I have a screenshot and I need to find some image (always the same size, rotation angle etc.) on it. 我有截图,我需要找到一些图像(总是相同的大小,旋转角度等)。 I've found some solutions with PIL and numpy, but they only work for non-transparent images. 我找到了一些PIL和numpy的解决方案,但它们只适用于非透明图像。 I have to find something like a circle, so I have to use transparent background behind it. 我必须找到类似圆圈的东西,所以我必须在它后面使用透明背景。
The sample image looks like: 示例图像如下所示:
and I'm looking for a target like: 我正在寻找一个目标:
Any ideas how can I achieve this? 任何想法我怎样才能做到这一点?
Since you are matching the image exactly, it's easy to create a sliding block to find the target. 由于您要精确匹配图像,因此可以轻松创建滑块以查找目标。 This isn't the fastest solution, but it's easy to get working. 这不是最快的解决方案,但它很容易上班。
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()
Thanks for reply! 谢谢你的答复! I analysed the topic I posted above once again and got some idea. 我再次分析了上面发布的主题并得到了一些想法。 Your code is simple, but it takes ~40s to execute. 您的代码很简单,但执行需要大约40秒。 I've done this: 我这样做了:
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
It uses getpixel method which is quite slow, but it executes in ~4s and I'm pretty glad of this. 它使用了很慢的getpixel方法,但它在~4s内执行,我很高兴。 Thank you for targeting me! 谢谢你瞄准我!
Regards mopsiok 关心mopsiok
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.