Skip to content

Instantly share code, notes, and snippets.

@nibral
Created March 5, 2017 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nibral/931cfbbd01d051e507770d6ee23d2871 to your computer and use it in GitHub Desktop.
Save nibral/931cfbbd01d051e507770d6ee23d2871 to your computer and use it in GitHub Desktop.
eAmuのDDRスコア画像を読み取るやつ
import sys
import cv2
# pattern matching
def find(image, template):
ret_i = 0
ret_val = 0
for i in range(len(template)):
res = cv2.matchTemplate(image, template[i], cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val > ret_val:
ret_val = max_val
ret_i = i
return ret_i
# load template number image (12x20)
# (208,264)+---+
# | |
# | |
# | |
# +---+(226,284)
num_template = []
num_width = 12
num_height = 20
for i in range(10):
num_i = cv2.imread('./num/' + str(i) + '.jpg')
# convert to gray scale
num_template.append(cv2.cvtColor(num_i, cv2.COLOR_RGB2GRAY))
# load score image from argument
score_img = cv2.imread(sys.argv[1])
# score(7digits)
s_x_origins = [224, 250, 267, 284, 310, 327, 344]
s_y_origin = 165
s_width = 17
s_height = 26
s_col = len(s_x_origins)
score = 0
weight = 10 ** (s_col - 1)
for d in range(s_col):
# set ROI
x = s_x_origins[d]
y = s_y_origin
clip = score_img[y:y+s_height, x:x+s_width]
# resize (same as num template)
clip = cv2.resize(clip, (num_width, num_height))
# convert to gray scale
clip = cv2.cvtColor(clip, cv2.COLOR_RGB2GRAY)
# pattarn matching
ret = find(clip, num_template)
score += ret * weight
weight /= 10
print('Score:' + str(int(score)))
# max combo(4 digits)
m_x_origin = 321
m_y_origin = 207
m_col = 4
combo = 0
weight = 10 ** (m_col - 1)
for d in range(m_col):
# set ROI
x = m_x_origin + num_width * d
y = m_y_origin
clip = score_img[y:y+num_height, x:x+num_width]
# convert to gray scale
clip = cv2.cvtColor(clip, cv2.COLOR_RGB2GRAY)
# pattarn matching
ret = find(clip, num_template)
combo += ret * weight
weight /= 10
print('Max Combo:' + str(int(combo)))
# ex score
e_x_origin = 321
e_y_origin = 227
e_col = 4
e_score = 0
weight = 10 ** (e_col - 1)
for d in range(e_col):
# set ROI
x = e_x_origin + num_width * d
y = e_y_origin
clip = score_img[y:y+num_height, x:x+num_width]
# convert to gray scale
clip = cv2.cvtColor(clip, cv2.COLOR_RGB2GRAY)
# pattarn matching
ret = find(clip, num_template)
e_score += ret * weight
weight /= 10
print('Ex Score:' + str(int(e_score)))
# judgement
j_x_origin = 154
j_y_origin = 264
j_row = 6
j_col = 4
j_title = ['Marvelous', 'Perfect', 'Great', 'Good', 'OK', 'Miss']
for j in range(j_row):
# digit
count_j = 0
weight = 10 ** (j_col - 1)
for d in range(j_col):
# set ROI
x = j_x_origin + num_width * d
y = j_y_origin + num_height * j
clip = score_img[y:y+num_height, x:x+num_width]
# convert to gray scale
clip = cv2.cvtColor(clip, cv2.COLOR_RGB2GRAY)
# pattarn matching
ret = find(clip, num_template)
count_j += ret * weight
weight /= 10
print(j_title[j] + ':' + str(int(count_j)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment