numpy操作:将绿色转换为红色(numpy operation: convert green to red)

编程入门 行业动态 更新时间:2024-10-27 15:27:28
numpy操作:将绿色转换为红色(numpy operation: convert green to red)

我有想要转换为红色的图像,以防有绿色。 如果有红色,我想保留它。

图像处于numpy数组中,如下所示:

x.shape(50,15,3)

在第一个实例中,我想取第三维(R和G)的前两个元素的最大值,并将相应的值设置为R(第一个元素)。 然后我想设置第三个维度零的第二个元素(G)。

我怎样才能做到这一点? 基本上它需要是这样的:

x[:,:,0] = max(x[:,:,0],x[:,:,1]) x[:,:,1] = 0

I have images that I would like to convert to red in case there is any green. If there's red, I'd like to keep it.

The images are in numpy arrays as follows:

x.shape (50, 15, 3)

In a fist instance I would like to take the max value of the first two elements of the third dimension (R and G) and set the corresponding value to R (the first element). Then I would like to set the second element (G) of the third dimension zo zero.

How can I do this? Essentially it woul need to be something like that:

x[:,:,0] = max(x[:,:,0],x[:,:,1]) x[:,:,1] = 0

最满意答案

看来你已经想出了第二步。 这是迈出第一步的一种方法 -

x[...,0] = x[...,:2].max(axis=-1)

或者,我们也可以使用np.maximum进行元素方式的最大计算 -

x[...,0] = np.maximum(x[...,0], x[...,1])

或者,我们也可以使用masking -

mask = x[...,0] < x[...,1] x[mask,0] = x[mask,1]

It seems you have already figured out the second step. Here's one way to do the first step -

x[...,0] = x[...,:2].max(axis=-1)

Alternatively, we can also use np.maximum for the element-wise max computation -

x[...,0] = np.maximum(x[...,0], x[...,1])

Alternatively, we can also use masking -

mask = x[...,0] < x[...,1] x[mask,0] = x[mask,1]

更多推荐

本文发布于:2023-08-05 15:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   红色   操作   numpy   red

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!