如何使用 Python 数字化(从中提取数据)热图图像?

Nee*_*raj 3 python heatmap data-extraction

热图示例

有几个软件包可用于将折线图数字化,例如GetData Graph Digitizer
但是,对于热图的数字化,我找不到任何软件包或程序。

我想使用 Python 数字化热图(来自 png 或 jpg 格式的图像)。怎么做?
我需要从头开始编写整个代码吗?
或者有没有可用的包?

mar*_*i20 5

有多种方法可以做到这一点,许多机器学习库提供自定义可视化功能……更容易或更难。

您需要将问题一分为二。

首先,将 OpenCV 用于 python 或 scikit-image,您首先必须将图像加载为矩阵。您可以设置一些偏移量以从单元格的开头开始。

import cv2    
# 1 - read color image (3 color channels)
image = cv2.imread('test.jpg',1)
Run Code Online (Sandbox Code Playgroud)

然后,您将遍历单元格并读取里面的颜色。如果需要,您可以对结果进行标准化。我们引入一些偏移量的原因是因为热图不是从原始图像的左上角 (0,0) 开始。offset_x 和 offset_y 将是每个列表,其中包含 2 个值。

  • offset_x[0] : 从图像左侧到热图开头的偏移量(即 start_of_heatmap_x)
  • offset_x[1] : 从图像右侧到热图结尾的偏移量(即 image_width - end_of_heatmap_x)
  • offset_y[0] : 从图像顶部到热图开始的偏移量(即 start_of_heatmap_y)
  • offset_y[1] : 从图像底部到热图结尾的偏移量(即 image_height - end_of_heatmap_y)

热图偏移

此外,我们不会迭代到最后一列。那是因为我们从“第 0”列开始,在每个基局部坐标上添加 cell_size/2 以获得单元格的中心值。

def read_as_digital(image, cell_size, offset_x, offset_y):
    # grab the image dimensions
    h = image.shape[0]
    w = image.shape[1]
    results = []
    # loop over the image, cell by cell 
    for y in range(offset_y[0], h-offset_y[1]-cell_size, cell_size):
       row = []
       for x in range(offset_x[0], w-offset_x[0]-cell_size, cell_size):
            # append heatmap cell color to row
            row.append(image[x+int(cell_size/2),y+int(cell_size/2)])
       results.append(row)

    # return the thresholded image
    return results
Run Code Online (Sandbox Code Playgroud)

提取图例信息并不难,因为我们可以通过限制来推导出值(尽管这适用于线性尺度)。

例如,我们可以推导出图例上的步骤(从 x 和 y)。

def generate_legend(length, offset, cell_size, legend_start, legend_end):
    nr_of_cells = (length- offset[0] - offset[1])/cell_size
    step_size = (legend_end - legend_start)/nr_of_cells
    i=legend_start+step_size/2  # a little offset to center on the cell

    values = []
    while(i<legend_end):
        values.append(i)
        i = i+step_size
    return values
Run Code Online (Sandbox Code Playgroud)

然后你想把它们形象化,看看是否一切都做对了。例如,使用 seaborn 就非常简单[1]。如果你想要更多的控制,对......任何事情,你可以使用 scikit learn 和 matplotlib [2]