For example, I want to read the image below and determine the arrangement of each block.
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.
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.
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.
Isn't Puzzle & Dragons interesting? I've never done it.
Recommended Posts