醋醋百科网

Good Luck To You!

游戏百解——利用Python图像识别玩连连看,手把手教你成为大师!

这是我自己用程序写的视频,利用Python图像识别算法玩转连连看。感兴趣可以自己看一下。

游戏百解——连连看(大神版)

前言:程序主要功能是先将练练看的整个大图切分成单个小图,然后进行循环遍历找出相似的图片,并在矩阵中进行记录,然后依据练练看两个图片连接的规则进行连接。直接放代码:

一、连连看连接逻辑程序:

import time
from pymouse import *
from LLKgame.baseFunctions import cutPicture, makeArray
class run_game():
	#初始截图左上右下坐标位置,初始截图总宽总高,初始单个图像宽高
	def __init__(self,left,top,right,botton,pWidth,pHeight,height,width):
		self.left = left
		self.top = top
		self.right = right
		self.botton = botton
		self.pWidth = pWidth
		self.pHeight = pHeight
		self.width = width
		self.height = height
		self.im2num_arr = []

	#点击事件
	def pClick(self, x1, y1, x2, y2):
		m = PyMouse()
		p1_x = int(self.left + (y1) * self.width - int((self.width / 2)))
		p1_y = int(self.top + (x1) * self.height - int((self.height / 2)))
		p2_x = int(self.left + (y2) * self.width - int((self.width / 2)))
		p2_y = int(self.top + (x2) * self.height - int((self.height / 2)))

		time.sleep(0.3)#沉睡时间
		m.click(int(p1_x / 1.25), int(p1_y / 1.25))
		time.sleep(0.3)
		m.click(int(p2_x / 1.25), int(p2_y / 1.25))
		# time.sleep(0.1)
		# 设置矩阵值为0
		self.im2num_arr[x1][y1] = 0
		self.im2num_arr[x2][y2] = 0
		print("消除:(%d, %d) (%d, %d)" % (x1, y1, x2, y2))

	# 是否为同行或同列且可连
	#X1 Y1 元素坐标x值以及y值 X2 Y2 元素坐标x值 y值
	def isReachable(self, x1, y1, x2, y2):
		# 1、先判断值是否相同
		if self.im2num_arr[x1][y1] != self.im2num_arr[x2][y2]:
			return False
		# 判断横向连通
		if self.isSameRow(x1, y1, x2, y2):
			return True
		# 判断纵向连通
		if self.isSameCol(x1, y1, x2, y2):
			return True
		# 判断一个拐点可连通
		if self.turnOnceCheck(x1, y1, x2, y2):
			return True
		# 判断两个拐点可连通
		if self.turnTwiceCheck(x1, y1, x2, y2):
			return True
		# 不可联通返回False
		return False

	#是否两个元素同行
	def isSameRow(self, x1, y1, x2, y2):
		if (abs(y1 - y2) == 1 and x1 == x2):  # 同行且相邻
			return True

		if (abs(y1 - y2) > 1 and x1 == x2):  # 同行不相邻
			# if (x1 == 0 or x1 == (self.pHeight - 1)):  # 最外行
			#     return True
			flag = 0
			for i in range(min(y1, y2) + 1, max(y1, y2)):
				if self.im2num_arr[x1][i] == 0:
					flag = flag + 0
				else:
					flag = flag + 1
			if (flag == 0):
				return True
		return False

	#是否同列
	def isSameCol(self, x1, y1, x2, y2):
		if (abs(x1 - x2) == 1 and y1 == y2):  # 同列且相邻
			return True

		if (abs(x1 - x2) > 1 and y1 == y2):  # 同列不相邻
			# if (y1 == 0 or y1 == (self.pWidth - 1)):  # 在最外列
			#     return True
			flag = 0
			for i in range(min(x1, x2) + 1, max(x1, x2)):
				if self.im2num_arr[i][y1] == 0:
					flag = flag + 0
				else:
					flag = flag + 1
			if (flag == 0):
				return True
		return False

	# 判断一个拐点可联通
	def turnOnceCheck(self, x1, y1, x2, y2):
		if x1 == x2 or y1 == y2:
			return False
		cx = x1
		cy = y2
		dx = x2
		dy = y1
		# 拐点为空,从第一个点到拐点并且从拐点到第二个点可通,则整条路可通。
		if self.im2num_arr[cx][cy] == 0:
			if self.isSameRow(x1, y1, cx, cy) and self.isSameCol(cx, cy, x2, y2):
				return True
		if self.im2num_arr[dx][dy] == 0:
			if self.isSameRow(x1, y1, dx, dy) and self.isSameCol(dx, dy, x2, y2):
				return True
		return False

	# 判断两个拐点可联通
	def turnTwiceCheck(self, x1, y1, x2, y2):
		if x1 == x2 and y1 == y2:
			return False
		# 遍历整个数组找合适的拐点
		for i in range(0, len(self.im2num_arr)):
			for j in range(0, len(self.im2num_arr[1])):
				# 不为空不能作为拐点
				if self.im2num_arr[i][j] != 0:
					continue
				# 不和被选方块在同一行列的不能作为拐点
				if i != x1 and i != x2 and j != y1 and j != y2:
					continue
				# 作为交点的方块不能作为拐点
				if (i == x1 and j == y2) or (i == x2 and j == y1):
					continue
				if self.turnOnceCheck(x1, y1, i, j) and (
						self.isSameRow(i, j, x2, y2) or self.isSameCol(i, j, x2, y2)):
					return True
				if self.turnOnceCheck(i, j, x2, y2) and (
						self.isSameRow(x1, y1, i, j) or self.isSameCol(x1, y1, i, j)):
					return True
		return False

	# 判断矩阵是否全为0
	def isAllZero(self, arr):
		for i in range(0, self.pWidth + 2):
			for j in range(0, self.pHeight + 2):
				if arr[i][j] != 0:
					return False
		return True

	def start(self):
		# 3、遍历查找可以相连的坐标
		global num1
		global num2
		num1 = 0#循环判断
		num2 = 0 #结束符
		print(self.im2num_arr)
		while not self.isAllZero(self.im2num_arr):
			if num1 == 10:
				for i in range(0, self.pWidth + 2):
					for j in range(0, self.pHeight + 2):
						self.im2num_arr[i][j] = 0
				num2 = 1
			for x1 in range(0, self.pWidth + 2):
				for y1 in range(0, self.pHeight + 2):
					if self.im2num_arr[x1][y1] == 0:
						continue
					for x2 in range(0, self.pWidth + 2):
						for y2 in range(0, self.pHeight + 2):
							if self.im2num_arr[x2][y2] == 0 or (x1 == x2 and y1 == y2):
								continue
							if self.im2num_arr[x1][y1] == self.im2num_arr[x2][y2]:
								if self.isReachable(x1, y1, x2, y2):
									self.pClick(x1, y1, x2, y2)
			num1 = num1 + 1
		return num2
if __name__ == '__main__':
	t = run_game(304, 271, 1226, 870,10,14,60.2,65.9)#设置元素坐标
	time.sleep(3)#等待3秒
	image = cutPicture(304, 271, 1226, 870,10,14,60.2,65.9)#截图
	arr = makeArray(10,14,image)#设置矩阵
	t.im2num_arr = arr
	num = t.start()
	print("游戏结束!")

二、选择一种图像识别算法,我这里写了四种,我是四种混着用,有两种算法通过则判断两个连连看图片一致,提高精准度。

import cv2
import numpy as np
import matplotlib
matplotlib.use('TkAgg')

# 均值哈希算法
def aHash(img):
    # 缩放为8*8
    # img = cv2.imread(img)
    img = np.asanyarray(img)
    img = cv2.resize(img, (8, 8))
    # 转换为灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # s为像素和初值为0,hash_str为hash值初值为''
    s = 0
    hash_str = ''
    # 遍历累加求像素和
    for i in range(8):
        for j in range(8):
            s = s+gray[i, j]
    # 求平均灰度
    avg = s/64
    # 灰度大于平均值为1相反为0生成图片的hash值
    for i in range(8):
        for j in range(8):
            if gray[i, j] > avg:
                hash_str = hash_str+'1'
            else:
                hash_str = hash_str+'0'
    return hash_str

# 差值哈希算法
def dHash(img):
    # 缩放8*8
    # img = cv2.imread(img) #此种方法只能用于本地图片读取
    img = np.asanyarray(img)
    img = cv2.resize(img, (9, 8))
    # 转换灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    hash_str = ''
    # 每行前一个像素大于后一个像素为1,相反为0,生成哈希
    for i in range(8):
        for j in range(8):
            if gray[i, j] > gray[i, j+1]:
                hash_str = hash_str+'1'
            else:
                hash_str = hash_str+'0'
    return hash_str

# 感知哈希算法
def pHash(img):
    # 缩放32*32
    # img = cv2.imread(img)
    img = np.asanyarray(img)
    img = cv2.resize(img, (32, 32))  # , interpolation=cv2.INTER_CUBIC
    # 转换为灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 将灰度图转为浮点型,再进行dct变换
    dct = cv2.dct(np.float32(gray))
    # opencv实现的掩码操作
    dct_roi = dct[0:8, 0:8]

    hash = []
    avreage = np.mean(dct_roi)
    for i in range(dct_roi.shape[0]):
        for j in range(dct_roi.shape[1]):
            if dct_roi[i, j] > avreage:
                hash.append(1)
            else:
                hash.append(0)
    return hash

# 灰度直方图算法
def calculate(image1, image2):
    # 计算单通道的直方图的相似值
    # 均值、差值、感知哈希算法三种算法值越小,则越相似,相同图片值为0
    # 三直方图算法和单通道的直方图 0-1之间,值越大,越相似。 相同图片为1
    # image1 = cv2.imread(image1)
    # image2 = cv2.imread(image2)
    image1 = np.asanyarray(image1)
    image2 = np.asanyarray(image2)
    hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
    hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
    # 计算直方图的重合度
    degree = 0
    for i in range(len(hist1)):
        if hist1[i] != hist2[i]:
            degree = degree + \
                (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
        else:
            degree = degree + 1
    degree = degree / len(hist1)
    return degree

# 三通道直方图算法
def classify_calculate(image1, image2):
    # 计算单通道的直方图的相似值
    # 均值、差值、感知哈希算法三种算法值越小,则越相似,相同图片值为0
    # 三直方图算法和单通道的直方图 0-1之间,值越大,越相似。 相同图片为1
    hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
    hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
    # 计算直方图的重合度
    degree = 0
    for i in range(len(hist1)):
        if hist1[i] != hist2[i]:
            degree = degree + \
                (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
        else:
            degree = degree + 1
    degree = degree / len(hist1)
    return degree

# RGB每个通道的直方图相似度
def classify_hist_with_split(image1, image2, size=(256, 256)):
    # 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
    image1 = np.asanyarray(image1)
    image2 = np.asanyarray(image2)
    image1 = cv2.resize(image1, size)
    image2 = cv2.resize(image2, size)
    sub_image1 = cv2.split(image1)
    sub_image2 = cv2.split(image2)
    sub_data = 0
    for im1, im2 in zip(sub_image1, sub_image2):
        sub_data += classify_calculate(im1, im2)
    sub_data = sub_data / 3
    return sub_data

# Hash值对比
def cmpHash(hash1, hash2):
    # 均值、差值、感知哈希算法三种算法值越小,则越相似,相同图片值为0
    # 三直方图算法和单通道的直方图 0-1之间,值越大,越相似。 相同图片为1
    # 算法中1和0顺序组合起来的即是图片的指纹hash。顺序不固定,但是比较的时候必须是相同的顺序。
    # 对比两幅图的指纹,计算汉明距离,即两个64位的hash值有多少是不一样的,不同的位数越小,图片越相似
    # 汉明距离:一组二进制数据变成另一组数据所需要的步骤,可以衡量两图的差异,汉明距离越小,则相似度越高。汉明距离为0,即两张图片完全一样
    n = 0
    # hash长度不同则返回-1代表传参出错
    if len(hash1) != len(hash2):
        return -1
    # 遍历判断
    for i in range(len(hash1)):
        # 不相等则n计数+1,n最终为相似度
        if hash1[i] != hash2[i]:
            n = n + 1
    return n


三、基本功能模块——截图、图片转换

from PIL import ImageGrab
import numpy as np

#截图方法
#传入图片像素位置以及所需比例宽度高度 #引入自己写的模块
from LLKgame.similarFunctinos import cmpHash, pHash, classify_hist_with_split, calculate

#传入截图的左上右下坐标,宽的小图数量,长的小图数量,小图的高度和宽度
#如cutPicture(304, 271, 1226, 870,10,14,60.2,65.9)在坐标(304,271)(1226,870)处截取140个小图,小图的高是60.2,宽是65.9

def cutPicture(left,top,right,botton,pWidth,pHeight,height,width):  # 截图
	size = (left, top, right, botton)
	img = ImageGrab.grab(size)
	# img.size(200,100)
	print(img.size)
	print(img)
	# img.save("D://cut.jpg")
	# img.show()
	# 2、分切小图
	image_list = {}
	for x in range(pWidth):
		image_list[x] = {}
		for y in range(pHeight):
			# print("show",x, y)
			# exit()
			top1 = x * height
			left1 = y * width
			right1 = (y + 1) * width
			botton1 = (x + 1) * height
			# 用crop函数切割成小图标,参数为图标的左上角和右下角左边
			im = img.crop((left1, top1, right1, botton1))
			# im.show()
			# time.sleep(1)
			# 将切割好的图标存入对应的位置
			image_list[x][y] = im
	print(image_list)
	return image_list

#创造数字矩阵
#传入矩阵的宽高和初始矩阵
def makeArray(pWidth,pHeight,image_list):
	image_type_list = []
	arr = np.zeros((pWidth + 2,pHeight + 2), dtype=np.int32)  # 创建矩阵以数字代替图片
	for i in range(len(image_list)):
		for j in range(len(image_list[0])):
			im = image_list[i][j]
			# 验证当前图标是否已存入
			index = getIndex(10,0.65,im, image_type_list)
			# 不存在image_type_list
			if index < 0:
				image_type_list.append(im)
				arr[i + 1][j + 1] = len(image_type_list)
			else:
				arr[i + 1][j + 1] = index + 1
	print("图标数:", len(image_type_list))
	# self.im2num_arr = arr
	return arr

# 检查数组中是否有图标,如果有则返回索引下表
#传入标准相似度similar1 similar2 和需要验证图片
def getIndex(similar1,similar2,im, im_list):
	global flag
	for i in range(len(im_list)):
		flag = 0
		# if self.compare_image_with_hash(im, im_list[i],6):
		#     return i
		val1 = calculate(im,im_list[i])
		val2 = classify_hist_with_split(im,im_list[i])
		val3 = cmpHash(pHash(im), pHash(im_list[i]))
		if val2 >= similar2:
			flag = 1
		if val1 >= similar2:
			flag = flag + 1
		if val3 <= similar1:
			flag = flag + 1
		if flag >= 2:
			return i
	return -1

以上就是程序所有的代码,程序难点在于两个图片之间相似度识别的精确性,不过对于练练看这个游戏来说,这个程序所使用的计算算法是够用的。喜欢的朋友可以一键三连!在评论区进行评论!

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言