通过较小的第一个图像找到正确的第二个图像 [英] Finding correct second image, by smaller representation of first

查看:58
本文介绍了通过较小的第一个图像找到正确的第二个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看看这个棋盘(

解决方案

如果您是图像处理的新手,我将向您展示一些简单的入门知识.我将您的图像加载为PIL/枕头图像,并将其转换为Numpy数组-您应该能够从 pyautogui 获得这样的图像或数组.

 从PIL导入图像将numpy导入为np#加载图像并制作成Numpy数组进行处理im = Image.open('chess.png').convert('RGB')na = np.array(im) 

接下来,我用一个滴管对g8和f6上的绿色进行采样以获取其RGB值

  greenA = [246,246,130]greenB = [186,202,68] 

现在,无论您的图像是哪种颜色,我都可以制作一个 True 的蒙版:

  maskA =(na [:] == greenA).all(2)maskB =(na [:] == greenB).all(2) 

如果要可视化蒙版,可以将它们制成图像并显示:

  Image.fromarray((maskA * 255).astype(np.uint8)).show() 

或将它们另存为文件:

  Image.fromarray((maskA * 255).astype(np.uint8)).save('a.png') 

如果您现在像这样对行中的像素求和:

  rowTotals = np.sum(maskA,axis = 1)数组([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、65、65、4、4、4、59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59、59、59、59、59、59、59、59、59、59、59、59、52、51、53、53、5351、52、56、4、4、4、4、65、65]) 

您会看到所有全黑的行总计为零,并且只有最后几行大于零-因此您可以找到彩色正方形的坐标.

同样,如果您像这样对各列的像素进行求和:

  colTotals = np.sum(maskA,axis = 0)数组([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0,0,0,0,0,0,0,0,0,0,0,0,0,65,65,4,4,4,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59、59、59、59、59、59、59、59、59、59、59、59、59、59、54、52、52,55、55、51、51、51、4、4、4、4、65、65、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0,0,0,0,0,0,0,0,0]) 

您可以看到前几列为空(黑色或零),然后是彩色像素,其次是更多黑色像素.

基本上,我正在这样做:

Let's take a look at this chessboard (https://prnt.sc/r8vjth) As you can see, there are two greenish squares (g8 and f6), which indicate the enemy's move. Square g8 is empty and is pretty easy to find by locateOnScreen function. Problems begin when I am trying to find f6 because it returns again the g8's position.

For example, in screenshot's position, it finds the g8. First part is correct, and it comes from the h parameter in the code below. But the second g8 is coming from this function, which is not correct.

def pos_loop(h): # h is the first position (Second square's position) which is struct consisting of top, left, width, height # I assume that h is correct because it always gives the right position values
    global previous_green
    global previous_white

    for p1 in pyautogui.locateAllOnScreen("C:\\Users\\Admin\\Desktop\\chesspic\\small_on_white.png"):
        if p1.height != h.height and p1.width != h.width and p1 != previous_white and p1 != previous_green:
            previous_white = p1
            print("\n Returned P1")
            return p1
    for p2 in pyautogui.locateAllOnScreen("C:\\Users\\Admin\\Desktop\\chesspic\\small_on_green.png"):
        if p2.height != h.height and p2.width != h.width and p2 != previous_green and p2 != previous_white:
            previous_green = p2
            print("\n Returned P2")
            return p2

    return None

previous_green and previous_white are just global variables, which are set to None at the start of the program. Even though I implemented a few checks, to make sure, that second position is not equal to the first, it is missing it randomly. Most strange thing is that this error is random. Sometimes it finds the right move, sometimes not.

Maybe you know how to solve that finding correct second image (f6) problem.

解决方案

If you are new to image processing, I'll show you something simple that might get you started. I'll load your image as a PIL/Pillow Image, and convert it to a Numpy array - you should be able to get such an Image or array from pyautogui.

from PIL import Image
import numpy as np

# Load your image and make into Numpy array for processing
im = Image.open('chess.png').convert('RGB')
na = np.array(im)

Next, I used a colour dropper to sample the green colours on g8 and f6 to get their RGB values

greenA = [246,246,130]
greenB = [186,202,68]

Now I can make a mask that is True wherever your image is that colour:

maskA = (na[:] == greenA).all(2)
maskB = (na[:] == greenB).all(2)

If you want to visualise the masks, you can make them into images and display them:

Image.fromarray((maskA*255).astype(np.uint8)).show()

Or save them as files:

Image.fromarray((maskA*255).astype(np.uint8)).save('a.png')

If you now sum the pixels across the rows like this:

rowTotals = np.sum(maskA,axis=1) 

array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 65, 65,  4,  4,  4, 59,
       59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59,
       59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59,
       59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 52, 51, 53, 53, 53,
       51, 52, 56,  4,  4,  4, 65, 65])

You can see that all the rows that are fully black total up to zero, and only the last few rows are greater than zero - so you can find the coordinates of your coloured square.

Likewise, if you sum the pixels down the columns like this:

colTotals = np.sum(maskA,axis=0)

array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 65, 65,  4,  4,
        4, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59,
       59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59,
       59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 54, 52, 52,
       55, 55, 51, 51, 51,  4,  4,  4, 65, 65,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0])

You can see the first few columns are empty (black or zero) and then there are your coloured pixels, followed by lots more black ones.

Essentially, I am doing this:

这篇关于通过较小的第一个图像找到正确的第二个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆