Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

python - numpy矩阵的有效着色值?(Efficient colorization of numpy matrix by value?)

I have a numpy ndarray that looks something like:

(我有一个看起来像的numpy ndarray:)

[[0, 0.25, 1,  ...., 0.5, 0.23 ],
 [0.3, 0.75, 1, ..., 0.5, 0.37 ],
  ...,
  ...,
 [0, 0.25, 1,  ...., 0.5, 0.23 ],
 [0.3, 0.75, 1, ..., 0.5, 0.37 ]]

Basically every value is in the range 0 - 1.0

(基本上每个值都在0-1.0的范围内)

I would like to visualize this as a bitmap and currently I have a very slow loop which basically does this:

(我想将其可视化为位图,目前我有一个很慢的循环,基本上可以这样做:)

for i, row in enumerate(data):
    for j, val in enumerate(row):
        yield val_to_rgb(val)

It then will take the 3-tuple of rgb components and do a PIL putdata on it and create a PNG.

(然后它将使用三元组的rgb组件,并对其进行PIL putdata并创建PNG。)

I need to do this many times and this ghetto method is slow, and the colorization is very ugly.

(我需要做很多次,而且这种贫民窟的方法很慢,而且着色很丑陋。)

My question is this:

(我的问题是这样的:)

Is there a series of matrix operations I can apply which will yield a colorized matrix containing the raw RGB values?

(我可以应用一系列矩阵运算,以产生包含原始RGB值的彩色矩阵吗?)

Which really consists of two questions:

(其中实际上包含两个问题:)

  1. What is the most efficient transformation I can apply to get RGB tuples from the above matrix

    (我可以应用最有效的变换来从上述矩阵中获取RGB元组)

  2. Is there a "nice" way to convert (0, 1.0) values into a colorized representation?

    (有没有一种“不错”的方法可以将(0,1.0)值转换为彩色表示形式?)

Edit: Clarification- I'm looking to SAVE this as PNG, not just view it in real time.

(编辑:澄清-我希望将其另存为PNG,而不仅仅是实时查看。)

The reason being that a lot of this is getting executed on a headless machine which I then inspect after the fact.

(原因是很多这样的事情都是在无头机器上执行的,后来我检查了一下。)

The output of the current algo looks pretty nasty:

(当前算法的输出看起来很讨厌:)

彩色矩阵

  ask by amirpc translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

matplotlib has imshow function you can use out-of-the-box.

(matplotlib具有imshow功能,您可以直接使用。)

What you're doing is usually done via "vectorization".

(您正在执行的操作通常是通过“矢量化”完成的。)

You define a function and let numpy do the iteration:

(您定义一个函数,然后让numpy进行迭代:)

vec = np.vectorize(val_to_rgb)
rgb_data = vec(data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...