Python/numpy 点列表到 black/white 图片区域

Python/numpy points list to black/white image area

我正在尝试将连续列表点(介于 0 和 1 之间)转换为黑白图像,代表区域 under/over 列表点。

plt.plot(points)
plt.ylabel('True val')
plt.show()
print("Points shape-->", points.shape)

我可以保存由 matplotlib 生成的图像,但我认为这可能是一个糟糕的解决方法

最后我想获得形状为 (224,224) 的图像,其中白色区域表示线下区域,黑色区域表示线上方...

image_area = np.zeros((points.shape[0],points.shape[0],))
# ¿?

欢迎提出任何想法或建议!感谢专家

这是一个基本示例,说明您可以如何做到这一点。由于切片需要整数,您可能必须先缩放原始数据。

import numpy as np
import matplotlib.pyplot as plt

# your 2D image
image_data = np.zeros((224, 224))

# your points. Here I am just using a random list of points
points = np.random.choice(224, size=224)

# loop over each column in the image and set the values
# under "points" equal to 1
for col in range(len(image_data[0])):
    image_data[:points[col], col] = 1

# show the final image
plt.imshow(image_data, cmap='Greys')
plt.show()

谢谢Eric,这里是你建议的解决方案,非常感谢!

def to_img(points):
    shape = points.shape[0]
# your 2D image
    image_data = np.zeros((shape, shape))

# your points. Here I am just using a random list of points
# points = np.random.choice(224, size=224)
    def minmax_norm_img(data, xmax, xmin):
        return (data - xmin) / (xmax - xmin)

    points_max = np.max(points)
    points_min = np.min(points)
    points_norm = minmax_norm_img(points,points_max , points_min)

# loop over each column in the image and set the values
# over "points" equal to 1
    for col in range(len(image_data[0])):
        image_data[shape-int(points_norm[col]*shape):, col] = 1

    return image_data