Read the image of the puzzle game and output the sequence of each block

Purpose

For example, I want to read the image below and determine the arrangement of each block. Pazudora01.png

problem

I tried to get the value by Template Matching using OpenCV, but it takes time to match each image, and all the images with high correlation within the threshold value are output, so select them. The cost was high because I had to write a program.

policy

Using the RGB data in the PIL library, the RGB data of the image cut out for each block is correlated and recognized as the color closest to the reference value.

Implementation

Use the PIL Image object.

import numpy
from PIL import Image

def get_rgb(pic, box=""):
    if box == "":
        box = (0, 0, pic.width, pic.height)
    rgbimg = pic.crop(box).convert("RGB")
    rgb = np.array(rgbimg.getdata())
    return [__round(rgb[:,0]),
            __round(rgb[:,1]),
            __round(rgb[:,2])]

def color(array):
    col = {}
    col["red"] = [100, 20, 20] #suitable
    #During this time, embed the standard value of color
    col["heart"] = [100, 70, 70] #suitable
    
    max = 0
    result = ""
    for k, c in col.items:
       tmp = numpy.corrcoef(numpy.array(array), numpy.array(c))[0][1]
       if max < tmp:
           result = k
           max = tmp
    return result

def __round(array):
    return int(round(np.average(array)))

if __name__ == "__main__":
    '''
        xa :start point, xs :Block width, xb :end point
     ya :start point, ys :Block height, yb :end point
    '''
    pic = Image.open("/path/to/Image.png ", 'r')
    for i in xrange(6):
        for j in xrange(5):
           box = (xa + xs*i,
                  ya + ys*j,
                  xb + xs*i,
                  yb + ys*j)
           rgb = get_rgb(pic, box)
           print color(rgb)

In this case, there is no particular problem in terms of speed, and the detection rate is also reasonable. Occasionally, it drops like Zero division.

Conclusion

Isn't Puzzle & Dragons interesting? I've never done it.

Recommended Posts

Read the image of the puzzle game and output the sequence of each block
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Read the output of subprocess.Popen in real time
I read and implemented the Variants of UKR
Coordination of each process in MPI and buffering of standard output
Filter the output of tracemalloc
[EC2] How to install chrome and the contents of each command
Read the standard output of a subprocess line by line in Python
The story of Python and the story of NaN
[Python] The stumbling block of import
Run Pylint and read the results
Have python read the command output
Clash of Clans and image analysis (3)
Judge the extension and download the image
[Image recognition] How to read the result of automatic annotation with VoTT
Find the general terms of the Tribonacci sequence with linear algebra and Python
About the main tasks of image processing (computer vision) and the architecture used