从图中提取值

Extract the values from graph

我想从绘图中提取数据。

原剧情:

但我无法获取原始值,有些地方提取的线(绿色)在原始数据(黑线)上方,有些地方在下方。我想我应该使用一些规则,如果线下降,如果它上升,如果它稳定,我应该对数据做一些事情。

如何将绿线调整到原始线的中间?

提取的情节:

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()

在原始图像中,线条的值被分配给具有一定宽度和高度的像素。在您的代码中,您正在提取这些值并在像素的左边界分配,正如您在缩放图像中看到的那样:

如果你的图片分辨率不错,你可以通过将绿色曲线向右移动半个像素宽度来近似,如下图所示:

您可以通过以下方式轻松完成:

ax.plot(np.arange(0 + 0.5, len(g_left) + 0.5), (g_left + g_right) / 2.0, color='green')

其中 0.5 如果像素的半宽。