Solution for Extract the values from graph
is Given Below:
I want to extract data from plot.
But I can’t obtain original values, in some places extracted line (green) above original data (black line), in some places it is below. I assume I should use some rule and do something with data if line falling, if its rising, and if its stable.
How can I fit green line to the middle of original one?
import cv2 import matplotlib.pyplot as plt import numpy as np img = cv2.imread("1.bmp", 0) img = np.flip(img, axis=0) img_plot = img.copy() graph = 255 - img_plot h, w = graph.shape g_left = np.flip(graph.copy(), axis=0) g_left = g_left.argmax(axis=0) g_left = h - g_left g_left[g_left == h] = 0 g_right = graph.copy() g_right = g_right.argmax(axis=0) g_right[g_right == h] = 0 fig, ax = plt.subplots() ax.imshow(img, extent=[0, w, 0, h], cmap="gray", origin='lower') ax.scatter(np.arange(0, len(g_left)), g_left, color="red") ax.scatter(np.arange(0, len(g_left)), g_right, color="blue") ax.plot(np.arange(0, len(g_left)), (g_left + g_right) / 2.0, color="green") plt.show()
In the original image, the value of the line is assigned to a pixel with some width and height. In your code, you are extracting those values and you assign at the left border of the pixel, as you can see in the zoomed image:
If your image has a good resolution, you can approximate by moving to the right the green curve by half width of a pixel, as in the below image:
You can do so simply by:
ax.plot(np.arange(0 + 0.5, len(g_left) + 0.5), (g_left + g_right) / 2.0, color="green")
0.5 if half width of a pixel.