我尝试在python中执行以下操作:捕捉屏幕
如果屏幕截图包含给定的参考图像(可以是jpg或pgn),则获取该图像在屏幕上的坐标
更多信息:参考图像不会太大(5x5像素就足够了)
它应该尽可能快,因为它应该不断地扫描屏幕
如果可能:在Windows和Linux上工作
在python中实现这一点的最佳方法是什么?在
编辑:
感谢利奥·安图内斯,我使以下解决方案发挥了作用:def bitmap2brg(bmp):
w = bmp.width;
h = bmp.height;
a = np.empty((h, w, 3), dtype=np.uint8);
for r in xrange(h):
for c in xrange(w):
v = bmp.get_color(c, r);
a[r, c, 2] = (v >> 16) & 0xFF;
a[r, c, 1] = (v >> 8) & 0xFF;
a[r, c, 0] = v & 0xFF;
return a;
def grabScreen():
THRESHOLD = 1
# reference image
needle = cv2.imread('img_top_left.png')
needle_height, needle_width, needle_channels = needle.shape
# Grabbing with autopy
screen = autopy.bitmap.capture_screen()
haystack = bitmap2brg(screen)
# work through the frame looking for matches above a certain THRESHOLD
# and mark them with a green circle
matches = 0
for pt in np.transpose(np.where(cv2.matchTemplate(haystack, needle, cv2.TM_CCOEFF_NORMED) >= THRESHOLD)):
cv2.circle(haystack, (pt[1] + needle_width/2, pt[0] + needle_height/2), 10, (0,255,0))
matches += 1
# display the altered frame
print "Number of matches: {}".format(matches)
cv2.imshow('matches', haystack)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()