[PYTHON] Read the graph image with OpenCV and get the coordinates of the final point of the graph

Background

I wanted to automatically calculate the difference number from the slump graph on the pachislot data site.

graph_sample.png

↑ 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.

code

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.

Explanation of the general flow of processing

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.

Improve processing speed

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! !! !!

Reference link

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

Recommended Posts

Read the graph image with OpenCV and get the coordinates of the final point of the graph
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the stock price of a Japanese company with Python and make a graph
[Python] Get the numbers in the graph image with OCR
Read image coordinates with Python-matplotlib
Get image features with OpenCV
Read the csv file with jupyter notebook and write the graph on top of it
I tried "gamma correction" of the image with Python + OpenCV
Try blurring the image with opencv2
[Statistics] Grasp the image of the central limit theorem with a graph
Read the image of the puzzle game and output the sequence of each block
How to get the pixel value of the point from the satellite image by specifying the latitude and longitude
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
How to crop the lower right part of the image with Python OpenCV
[Image recognition] How to read the result of automatic annotation with VoTT
Get the number of articles accessed and likes with Qiita API + Python
Try to separate the background and moving object of the video with OpenCV
What I did when I couldn't find the feature point with the optical flow of opencv and when I lost it
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
Extract the color of the object in the image with Mask R-CNN and K-Means clustering
The basis of graph theory with matplotlib animation
Wavelet transform of images with PyWavelets and OpenCV
Data cleansing 3 Use of OpenCV and preprocessing of image data
I read and implemented the Variants of UKR
I tried using the image filter of OpenCV
[Python + OpenCV] Whiten the transparent part of the image
Get the title of yahoo news and analyze sentiment
Extract the table of image files with OneDrive & Python
Coordinates of the right end of Label made with tkinter
Get the image of "Suzu Hirose" by Google image search.
Omit the decimal point of the graph scale in matplotlib
[OpenCV / Python] I tried image analysis of cells with OpenCV
Image capture / OpenCV speed comparison with and without GPU
Visualize the range of interpolation and extrapolation with python
Read the power of the smart meter with M5StickC (BP35C0-J11-T01)
Rotate and scale the image before cropping [python] [OpenCV]
POST the image with json and receive it with flask
[Django 2.2] Sort and get the value of the relation destination
Get the operation status of JR West with Python
Visualize the appreciation status of art works with OpenCV
Read the OpenCV documentation
A server that returns the number of people in front of the camera with bottle.py and OpenCV
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
Return the image data with Flask of Python and draw it to the canvas element of HTML
Get the trading price of virtual currency and create a chart with API of Zaif exchange
[Python] Read the csv file and display the figure with matplotlib
See the power of speeding up with NumPy and SciPy
[pyqtgraph] Add region to the graph and link it with the graph region
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
Get rid of dirty data with Python and regular expressions
The story of displaying images with OpenCV or PIL (only)
100 language processing knock-29: Get the URL of the national flag image
Read the VTK file and display the color map with jupyter.
Sample of HTTP GET and JSON parsing with python of pepper
Estimate the attitude of AR markers with Python + OpenCV + drone
Make a BLE thermometer and get the temperature with Pythonista3
Play with the password mechanism of GitHub Webhook and Python
[Python] Easy reading of serial number image files with OpenCV