在4399上玩了一个寻找不一样的色彩小游戏,如下图所示:
目的就是找到颜色不同的方块,点一次升一次级,限定时间25秒,点的快时间可以不变。最开始2x2,3x3,4x4,5x5等等逐渐增多,最后9x9不变。我的思路首先是截一张图,这里我用的是pyscreenshot模块,让用户用鼠标点击游戏总方块的左上顶角和右下顶角,这样确定了目标区域。以9x9的时候为例,代码如下:
import cv2 import numpy as np import os import pyscreenshot as ss import time def onMouse(event, x, y, flags, param): global flag, pLeftTop, pRightBottom if event == cv2.EVENT_LBUTTONDOWN: if flag: pLeftTop = (x,y) else: pRightBottom = (x,y) flag = False flag = True pLeftTop = None pRightBottom = None cv2.namedWindow('frame') cv2.setMouseCallback('frame', onMouse) img = ss.grab(bbox=(0,0,1300,768)) img = np.array(img) cv2.imshow('frame', img) if cv2.waitKey(0) & 0xff == ord(' '): cv2.destroyWindow('frame')
while True: img = ss.grab(bbox=(0,0,1300,768)) img = np.array(img) target = img[pLeftTop[1]:pRightBottom[1],pLeftTop[0]:pRightBottom[0]] gray = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY_INV)
接下来二值化处理,我这里设的阈值是220,取反处理,结果如下:
im, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) centers = [] centersGray = [] #d = 0 for c in contours: (x,y), radius = cv2.minEnclosingCircle(c) x, y = int(x), int(y) centers.append((x,y)) centersGray.append(gray[y,x]) #d+=1 #cv2.circle(target, (x,y), 2, (0,255,0), 2) #cv2.putText(target, str(d), (x,y), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0))
sets = set(centersGray) index = None for s in sets: if centersGray.count(s) == 1: index = centersGray.index(s) break接下来就是找到不同的那个颜色块,可以通过list的自带count函数快速找到,在图上显示就是:
realX = centers[index][0] + pLeftTop[0] realY = centers[index][1] + pLeftTop[1] os.system('xdotool mousemove '+str(realX)+' '+str(realY)) os.system('xdotool click 1')
演示视频:http://www.bilibili.com/video/av5023951/
完整代码: https://github.com/littlethunder/autoFindColorGame
转载请注明: 转自http://blog.csdn.net/littlethunder/article/details/51713234