首页 > 解决方案 > Python/numpy 点列表到黑白图像区域

问题描述

我正在尝试将连续列表点(0 到 1 之间)转换为黑白图像,表示列表点下方/上方的区域。

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],))
# ¿?

欢迎任何想法或建议如何处理它!感谢专家

标签: pythonnumpyopencvimage-processingscikit-image

解决方案


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

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

在此处输入图像描述


推荐阅读