有效地将一组 {coordinate+value} 绘制到(numpy 数组)位图 [英] Efficiently plot set of {coordinate+value}s to (numpy array) bitmap

查看:70
本文介绍了有效地将一组 {coordinate+value} 绘制到(numpy 数组)位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一组像素值,例如

Suppose I have a set of pixel values, e.g.

> S[42]
6, 2, (0.1, 0, 0)

^ 这里的第 42 个条目是用于像素位置 (6,2) 的暗红色.

^ here the 42nd entry is for pixel location (6,2) with a dull red color.

如何有效地将 S 绘制成一个新的 numpy 位图数组 bitmap = np.zeros((1024, 768, 3))?

How to efficiently plot S into a fresh numpy bitmap array bitmap = np.zeros((1024, 768, 3))?

是否有一些矢量化解决方案(而不是 for 循环)?

Is there some vectorized solution (rather than a for loop)?

如果有帮助,我可以按列将 S 拆分为 S_xS_yS_RGB.

I can split S by columns into S_x, S_y and S_RGB if that helps.

推荐答案

这就是你的方法,是的,拆分是有帮助的,并使用我在下面的相同数据类型

this is how you do it, yes splitting up is helpful, and use the same datatypes I have below

bitmap = np.zeros((10, 10, 3))
    
s_x = (1,2,3) ## tuple
s_y = (0,1,2) ## tuple
pixal_val = np.array([[0,0,1],[1,0,0],[0,1,0]]) ## np

bitmap[s_y, s_x] = pixal_val

plt.imshow(bitmap)

输出:

它确实可以使用 numpy 数组作为坐标,但请确保它们是 int 类型

it does work with using numpy arrays as coordinates but make sure they are type int

bitmap = np.zeros((10, 10, 3))

s_x = np.array([a for a in range(10)], dtype=int)
s_y = np.array([a for a in range(10)], dtype=int)
    
np.random.shuffle(s_x)
np.random.shuffle(s_y)

pixel_val = np.random.rand(10,3)

bitmap[s_y, s_x] = pixel_val

plt.imshow(bitmap)

最终 s_x ans s_y 我在上面修复了错误的方法

final edit: s_x ans s_y where the wrong way round I have fixed above

这篇关于有效地将一组 {coordinate+value} 绘制到(numpy 数组)位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆