侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python实现连连看辅助(图像识别)

Python  /  管理员 发布于 7年前   409

个人兴趣,用python实现连连看的辅助程序,总结实现过程及知识点。

总体思路

1、获取连连看程序的窗口并前置
2、游戏界面截图,将每个一小图标切图,并形成由小图标组成的二维列表
3、对图片的二维列表遍历,将二维列表转换成由数字组成的二维数组,图片相同的数值相同。
4、遍历二维数组,找到可消除的对象,实现算法:

  • 两个图标相邻。(一条线连接)
  • 两个图标同行,同列,且中间的图标全部为空(数值为0)(一条线连接)
  • 两条线连接,转弯一次,路径上所有图标为空。(二条线连接)
  • 三条线连接,转弯二次,路径上所有图标为空。(三条线连接)
  • 分别点击两个图标,并将对应的二维数据值置为0

实现过程中遇到的问题

图片切割

im = image.crop((left,top,right,bottom))//image.crop参数为一个列表或元组,顺序为(left,top,right,bottom)

找到游戏运行窗口

hdwd = win32gui.FindWindow(0,wdname)# 设置为最前显示win32gui.SetForegroundWindow(hdwd)

窗口不要点击最小化,点击后无法弹出来。

  • 图片缩放并转为灰度

img1 = im1.resize((20, 20), Image.ANTIALIAS).convert('L')

Image.ANTIALIAS 为抗锯齿的选项,图片无毛边。

  • 获取图片每个点的RGB值

pi1 = list(img1.getdata())

列表每个元素为一个三位数的值,分别代表该点的RGB值。列表pi1共400个元素。(因为图片为20*20)

  • 鼠标点击消除

PyMouse.click()该方法默认双击,改为PyMouse.press() 或 PyMouse.release()

  • 判断图片相似
  • 汉明距离,平均哈希
def compare_img(self,im1,im2): img1 = im1.resize((20, 20), Image.ANTIALIAS).convert('L') img2 = im2.resize((20, 20), Image.ANTIALIAS).convert('L') pi1 = list(img1.getdata()) pi2 = list(img2.getdata()) avg1 = sum(pi1) / len(pi1) avg2 = sum(pi2) / len(pi2) hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pi1)) hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pi2)) match = 0 for i in range(len(hash1)): if hash1[i] != hash2[i]: match += 1 # match = sum(map(operator.ne, hash1, hash2)) # match 值越小,相似度越高 return match
  • 计算直方图
from PIL import Image# 将图片转化为RGBdef make_regalur_image(img, size=(8, 8)): gray_image = img.resize(size).convert('RGB') return gray_image# 计算直方图def hist_similar(lh, rh): assert len(lh) == len(rh) hist = sum(1 - (0 if l == r else float(abs(l - r)) / max(l, r)) for l, r in zip(lh, rh)) / len(lh) return hist# 计算相似度def calc_similar(li, ri): calc_sim = hist_similar(li.histogram(), ri.histogram()) return calc_simif __name__ == '__main__': image1 = Image.open('1-10.jpg') image1 = make_regalur_image(image1) image2 = Image.open('2-11.jpg') image2 = make_regalur_image(image2) print("图片间的相似度为", calc_similar(image1, image2)) # 值在[0,1]之间,数值越大,相似度越高
  • 图片余弦相似度
from PIL import Imagefrom numpy import average, dot, linalg# 对图片进行统一化处理def get_thum(image, size=(64, 64), greyscale=False): # 利用image对图像大小重新设置, Image.ANTIALIAS为高质量的 image = image.resize(size, Image.ANTIALIAS) if greyscale: # 将图片转换为L模式,其为灰度图,其每个像素用8个bit表示 image = image.convert('L') return image# 计算图片的余弦距离def image_similarity_vectors_via_numpy(image1, image2): image1 = get_thum(image1) image2 = get_thum(image2) images = [image1, image2] vectors = [] norms = [] for image in images: vector = [] for pixel_tuple in image.getdata(): vector.append(average(pixel_tuple)) vectors.append(vector) # linalg=linear(线性)+algebra(代数),norm则表示范数 # 求图片的范数?? norms.append(linalg.norm(vector, 2)) a, b = vectors a_norm, b_norm = norms # dot返回的是点积,对二维数组(矩阵)进行计算 res = dot(a / a_norm, b / b_norm) return resif __name__ == '__main__': image1 = Image.open('1-9.jpg') image2 = Image.open('8-6.jpg') cosin = image_similarity_vectors_via_numpy(image1, image2) print('图片余弦相似度', cosin) # 值在[0,1]之间,数值越大,相似度越高,计算量较大,效率较低

完整代码

import win32guiimport timefrom PIL import ImageGrab , Imageimport numpy as npfrom pymouse import PyMouseclass GameAuxiliaries(object): def __init__(self): self.wdname = r'宠物连连看经典版2,宠物连连看经典版2小游戏,4399小游戏 www.4399.com - Google Chrome' # self.wdname = r'main.swf - PotPlayer' self.image_list = {} self.m = PyMouse() def find_game_wd(self,wdname): # 取得窗口句柄 hdwd = win32gui.FindWindow(0,wdname) # 设置为最前显示 win32gui.SetForegroundWindow(hdwd) time.sleep(1) def get_img(self): image = ImageGrab.grab((417, 289, 884, 600)) # image = ImageGrab.grab((417, 257, 885, 569)) image.save('1.jpg','JPEG') for x in range(1,9): self.image_list[x] = {} for y in range(1,13): top = (x - 1) * 38 + (x-2) left =(y - 1) * 38 +(y-2) right = y * 38 + (y-1) bottom = x * 38 +(x -1) if top < 0: top = 0 if left < 0 : left = 0 im_temp = image.crop((left,top,right,bottom)) im = im_temp.crop((1,1,37,37)) im.save('{}-{}.jpg'.format(x,y)) self.image_list[x][y]=im # 判断两个图片是否相同。汉明距离,平均哈希 def compare_img(self,im1,im2): img1 = im1.resize((20, 20), Image.ANTIALIAS).convert('L') img2 = im2.resize((20, 20), Image.ANTIALIAS).convert('L') pi1 = list(img1.getdata()) pi2 = list(img2.getdata()) avg1 = sum(pi1) / len(pi1) avg2 = sum(pi2) / len(pi2) hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pi1)) hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pi2)) match = 0 for i in range(len(hash1)): if hash1[i] != hash2[i]: match += 1 # match = sum(map(operator.ne, hash1, hash2)) # match 值越小,相似度越高 return match # 将图片矩阵转换成数字矩阵 def create_array(self): array = np.zeros((10,14),dtype=np.int32) img_type_list = [] for row in range(1,len(self.image_list)+1): for col in range(1,len(self.image_list[1])+1): # im = Image.open('{}-{}.jpg'.format(row,col)) im = self.image_list[row][col] for img in img_type_list: match = self.compare_img(im,img) # match = test2.image_similarity_vectors_via_numpy(im,img) if match <15: array[row][col] = img_type_list.index(img) +1 break else: img_type_list.append(im) array[row][col] = len(img_type_list) return array def row_zero(self,x1,y1,x2,y2,array): '''相同的图片中间图标全为空''' if x1 == x2: min_y = min(y1,y2) max_y = max(y1,y2) if max_y - min_y == 1: return True for y in range(min_y+1,max_y): if array[x1][y] != 0 : return False return True else: return False def col_zero(self,x1,y1,x2,y2,array): '''相同的图片同列''' if y1 == y2: min_x = min(x1,x2) max_x = max(x1,x2) if max_x - min_x == 1: return True for x in range(min_x+1,max_x): if array[x][y1] != 0 : return False return True else: return False def two_line(self,x1,y1,x2,y2,array): '''两条线相连,转弯一次''' for row in range(1,9): for col in range(1,13): if row == x1 and col == y2 and array[row][col]==0 and self.row_zero(x1,y1,row,col,array) and self.col_zero(x2,y2,row,col,array): return True if row == x2 and col == y1 and array[row][col]==0 and self.row_zero(x2,y2,row,col,array) and self.col_zero(x1,y1,row,col,array): return True return False def three_line(self,x1,y1,x2,y2,array): '''三条线相连,转弯两次''' for row1 in range(10): for col1 in range(14): for row2 in range(10): for col2 in range(14): if array[row1][col1] == array[row2][col2] == 0 and self.row_zero(x1,y1,row1,col1,array) and self.row_zero(x2,y2,row2,col2,array) and self.col_zero(row1,col1,row2,col2,array): return True if array[row1][col1] == array[row2][col2] == 0 and self.col_zero(x1,y1,row1,col1,array) and self.col_zero(x2,y2,row2,col2,array) and self.row_zero(row1,col1,row2,col2,array): return True if array[row1][col1] == array[row2][col2] == 0 and self.row_zero(x2,y2,row1,col1,array) and self.row_zero(x1,y1,row2,col2,array) and self.col_zero(row1,col1,row2,col2,array): return True if array[row1][col1] == array[row2][col2] == 0 and self.col_zero(x2,y2,row1,col1,array) and self.col_zero(x1,y1,row2,col2,array) and self.row_zero(row1,col1,row2,col2,array): return True return False def mouse_click(self,x,y): top = (x - 1) * 38 + (x - 2) left = (y - 1) * 38 + (y - 2) right = y * 38 + (y - 1) bottom = x * 38 + (x - 1) if top < 0: top = 0 if left < 0: left = 0 self.m.press(int(417+(left+right)/2) ,int(289+(top+bottom)/2) ) def find_same_img(self,array): for x1 in range(1,9): for y1 in range(1,13): if array[x1][y1] == 0: continue for x2 in range(1,9): for y2 in range(1,13): if x1==x2 and y1 == y2: continue if array[x2][y2] == 0 : continue if array[x1][y1] != array[x2][y2] : continue if array[x1][y1] ==array[x2][y2] and (self.row_zero(x1,y1,x2,y2,array) or self.col_zero(x1,y1,x2,y2,array) or self.two_line(x1,y1,x2,y2,array) or self.three_line(x1,y1,x2,y2,array)): print("可消除!x{}y{} 和 x{}y{}".format(x1,y1,x2,y2)) self.mouse_click(x1,y1) time.sleep(0.1) self.mouse_click(x2,y2) time.sleep(0.1) array[x1][y1]=array[x2][y2]=0 def run(self): #找到游戏运行窗口 self.find_game_wd(self.wdname) # 截图,切割成小图标 self.get_img() # 将图片矩阵转换成数字矩阵 array = self.create_array() print(array) # 遍历矩阵,找到可消除项,点击消除 for i in range(10): self.find_same_img(array) print(array)if __name__ == '__main__': ga = GameAuxiliaries() ga.run()

总结

该程序其实未能完全实现辅助功能,主要是因为图片切割时未找到更好的规则,造成图片识别困难,缩放比例和判断阀值未找到一个平衡点,阀值太大,则将不同的图标识别为相同,阀值太小,相同的图标又判断为不一样。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    详解python实现数据归一化处理的方式:(0,1)标准化
    下一条:
    200行python代码实现2048游戏
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客