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

How to digitize (extract data from) a heat map image using Python?

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

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

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

你需要把问题分成两半。

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

import cv2    
# 1 - read color image (3 color channels)
image = cv2.imread('test.jpg',1)

然后,您将遍历单元格并读取其中的颜色。如果需要,您可以将结果归一化。我们引入一些偏移量的原因是因为热图不是从原始图像的左上角 (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

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

因此,例如,我们可以推导出图例中的步骤(从 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

然后您想将它们形象化,看看是否一切都做对了。例如,使用 seaborn 就非常简单 [1]. If you want more control, over...anything, you can use scikit learn and matplotlib [2].