代码下载:GitHub - sunriver2000/LinkGameAss

问题

1、如何提高图标对比识别率?

思路

1、截取图标时不保留外围的正方形黑框,上下左右都少截取2个像素,如下图。

2、增加数值分析函数。

python游戏脚本 python游戏脚本制作_开发语言

关键步骤

1、调整图标截取区域。

# 分割图标
    def screenshot(self):
        image = ImageGrab.grab((self.x0, self.y0, self.x1, self.y1))

        save_im(image, 'image')

        image_list = {}
        offset = self.im_width

        for x in range(8):
            image_list[x] = {}
            for y in range(12):
                top = round(x * offset) + 2
                left = round(y * offset) + 2
                bottom = round((x + 1) * offset) - 2
                right = round((y + 1) * offset) - 2

                im = image.crop((left, top, right, bottom))
                image_list[x][y] = im

        return image_list

2、增加分析函数,用于分析二值化后图像对比效果。

# 分析图像识别算法
    def analysis(self, image_list):
        
        f = open('log.txt','w')
        for z in range(len(self.im_type_list)):

            for i in range(len(image_list)):
                for j in range(len(image_list[0])):
                    if self.im2num_arr[i+1][j+1] == z + 1:
                        
                        self.isMatch(self.im_type_list[z], image_list[i][j])
                        f.write(str(self.result).zfill(3) + ' ' + str(self.hash2) + '\n')
            f.write('\n')
        f.close()

 图标识别函数做了稍许改动,主要是把变量改为全局的,方便后续保存数据。

# 汉明距离判断两个图标是否一样
    def isMatch(self, im1, im2):
        # 缩小图标,转成灰度
        image1 = im1.resize((20, 20), Image.ANTIALIAS).convert("L")
        image2 = im2.resize((20, 20), Image.ANTIALIAS).convert("L")

        # 降灰度图标转成01串,即二进制数据
        pixels1 = list(image1.getdata())
        pixels2 = list(image2.getdata())

        avg1 = sum(pixels1) / len(pixels1)    
        avg2 = sum(pixels2) / len(pixels2)    
        self.hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pixels1))
        self.hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pixels2))

        # 统计两个01串不同数据的个数
        match = sum(map(operator.ne, self.hash1, self.hash2))

        self.result = match

        # 阀值设为10
        return match < 40

分析结果,保存至log.txt中。下面是同一种图标的6个二值化样本。

000 0111111111111111111101111111001111111111011111000000111111110111000000000111111101100000000000111111010000010000100011110111001000110100111101110101110010001111011101111000000001110111011110100010011101110111100000000111011100111000000011110111100110000000111101111001110001001111011110000100111011110111110000000000011101111110000000111111011111111111111111110111111111111111111100000000000000000000
022 0111111111111111111001111110001111111111011111000000111111110111000000000111111101000000000000011111010000010000100111110111001101110100111101110101110110001111011101111000000011110111011110010010111101110111100100001111011100111001000011110111100110000001111101111001100001001111011110001100011011110111110000000000111101111111000001111111011111111111111111110111111111111111111100000000000000000000
021 0111111111111111111101111110001111111111011111000000111111110111000000000011111101000000000000011111010000010000100111110111001001110100111101110101110110001111011101111000000011110111011110010010111101110111100100001111011100111001000011110111100110000001111101111001110001001111011111000100001011110111110000000000111101111111000001111111011111111111111111110111111111111111111100000000000000000000
011 0111111111111111111101111111001111111111011110000000111111110111000000000111111101100000000000111111010000010000100011110111001000100100111101110101110110001111011101111000000011110111011110100010111101110111100100001111011100111001000011110111100110000000111101111001100001001111011110000100011011110111110000000000111101111110000000111111011111111111111111110111111111111111111100000000000000000000
035 0000000011000000000001111111111111111111011111000000111111110111000000000111111101000000000000111111010000010000100011110111001100100000111101110101110110001111011101111000100011110111011110100010111101110111100100001111011100111001000011110111100110000000111101111001100001001111011110000100001011110111110000000000111101111111000000111111011111111111111111110111111111111111111100000000000000000000
023 0111111111111111111101111110001111111111011110000000111111110111000000000111111101000000000000011111010000110000100111110111001001100100111101110101110110001111011001111000000011110110011110100000111101110111100100001111011100111001000011110111100110000001111101111001100001011111011110000100011011110111110000000000111101111111000000111111011111111111111111110111111111111111111100000000000000000000