I wanted to automatically calculate the difference number from the slump graph on the pachislot data site.
↑ The graph looks like this.
Since the y-coordinate of the final point of the graph (red circle part of the image) was required for the calculation, I wrote the code to read the graph image and acquire the coordinates using OpenCV.
import cv2
import numpy as np
import time
start = time.time()
file_path = 'C:\\Users\\Pictures\\sample_graph.png'
#Graph image reading
img = cv2.imread(file_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #Contents of img:[y coordinate,x coordinate, [R, G, B]]
#Get the number of elements on the x-axis and y-axis
i_range = img.shape[0]
j_range = img.shape[1]
#Array for storing coordinates
graph_coordinate = [[0 for i in range(3)] for j in range(i_range * j_range)]
x_list = []
#y-axis loop
for i in range(i_range):
#x-axis loop
for j in range(j_range):
#Store each RGB value
R, G, B = img[i, j]
#Get only the coordinates that match the graph color
if (230 <= R <= 255) and (0 <= G <= 30) and (130 <= B <= 160):
graph_coordinate.append([i, j, img[i, j]])
x_list.append(j)
coordinate = np.asarray(graph_coordinate)
#Find the y coordinate from the maximum value on the x axis (x coordinate at the end of the graph)
target = np.where(coordinate[:, 1] == max(x_list))
y_axis_target = coordinate[target]
y_axis = y_axis_target[0][0]
print(f'y_axis:{y_axis}')
print('elapsed_time:{time} sec'.format(time=time.time() - start))
The processing when there is no graph and the processing when there are multiple y-coordinate targets are also in the continuation of this, but they will be long, so they are omitted.
First, read the graph and get all pixel information with numpy.ndarray type. (Data is stored in img in the form of [y coordinate, x coordinate, [R, G, B]]) ↓ Search all pixels from pixel information and store all coordinates of pixels that match the graph color in the list. (The RGB value of the graph color is set by checking the image in advance) ↓ Finally, get the target y-coordinate from the maximum value on the x-axis (end point of the graph) and complete.
It is a flow.
This meets the requirements, but the processing speed is slow due to the double loop of the for statement.
Execution result
y_axis:36
elapsed_time:2.9471683502197266 sec
For the time being, I wanted to make only the shape, so I wrote it with the above code that came up immediately, but since I am reading the elements of the graph with the numpy type, I rewrote it to a shape that can make use of it.
import cv2
import numpy as np
import time
start = time.time()
file_path = 'C:\\Users\\Pictures\\sample_graph.png'
#Graph image reading
img = cv2.imread(file_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Get RGB value of graph start point pixel (img[y coordinate][x coordinate])
color_pattern = img[180][45]
#Binarization(0 or 255)
mask = cv2.inRange(img, color_pattern, color_pattern)
#Get coordinates of only the graph part
coordinate = np.stack(np.where(mask == 255), axis=1)
#Maximum value on the x-axis (end point of graph)
max_x_axis = coordinate[:, 1].max()
#Find the y coordinate from the maximum value on the x-axis
y_axis_target = coordinate[np.where(coordinate[:, 1] == max_x_axis)]
y_axis = y_axis_target[0][0]
print(f'y_axis:{y_axis}')
print('elapsed_time:{time} sec'.format(time=time.time() - start))
Unlike before, first get the RGB value of the graph from the pixel at the start of the graph. ** * Prerequisite: Image size and coordinates of graph start point are always constant **
Next, binarize the pixels that match the RGB values in the graph and the pixels that do not. After that, use numpy.where to get the coordinates of only the graph part (255). At the end, as with the code before improvement, the y coordinate is calculated from the maximum value on the x-axis and completed.
Click here for the processing speed after rewriting.
Execution result
y_axis:36
elapsed_time:0.015000581741333008 sec
Explosive speed! !! !!
Image processing with Python: Differences and usage of Pillow, NumPy, OpenCV Image processing with Python, NumPy (read, calculate, save) Image processing with Python Let's understand image arrays and RGB! [Python] Numpy reference, extraction, combination How to output the coordinates of a specific color in python [Python / OpenCV] How to extract a specific color of an image