numpy:如何在给定测试条件的多维数组中使用np.where? [英] Numpy : how to use np.where in a multidimensional array with a given test condition?

查看:756
本文介绍了numpy:如何在给定测试条件的多维数组中使用np.where?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我将问题简化为一个最小的问题,因为我的第一个问题可能太乱了

Edit : I reduce to a minimal problem, since my first question was probably too messy

当我在标量单元格上使用np.where时,事情就可以了罚款:

when I use np.where on a condition on a scalar cell things work fine:

new_array = np.where(old_array==6, rempl_array, old_array)

但是如果我希望我的条件在数组的完整维度上起作用:

but if I want my condition to work on a full dimension of the array:

new_array = np.where((old_array == [1, 2, 3]).all(axis=-1), rempl_array, old_array)

由于尺寸不匹配,我不再使用

I does not any more, for dimension mismatch

但是我不知道列出了如何在合适的3D布尔值中转换2D布尔值(old_array == [1、2、3])。all(axis = -1)

But I can't figure out how to transform the 2D boolean (old_array == [1, 2, 3]).all(axis=-1) in a suitable 3D boolean for where

这是最初的帖子:

我有一个3D数组,我是根据图片创建的(因此尺寸保持高度) ,宽度和RGB值)。我想根据给定的条件更改颜色。

I have a 3D array, that I have created from a picture (so dimensions hold for height, width and RGB value). I want to change colors according to a given condition.

    submap = np.any([(carr == [pr["red"], pr["green"], pr["blue"]]).all(axis=-1) for pr in list_areas], axis=0)

条件正常,对于满足条件的像素,使用True返回2D数组,否则返回False。

The condition works fine, retruning a 2D array with True for pixels where the condition is met, and False otherwise.

但是,当我尝试构建一个新的3D数组时,我会根据该条件更改颜色:

However, when I try to build a new 3D array where I change colors according to that condition:

    new_carr = np.where(submap, new_color, carr)

我收到形状不匹配错误:

I get a shape mismatch error :

ValueError: operands could not be broadcast together with shapes (2048,5632) (3,) (2048,5632,3)

问题似乎不仅仅在于我的new_color具有形状(3,)的事实,因为问题仍然存在。我将其替换为形状数组(2048,5632,3),但事实是我的条件是2D,而我的初始数组是3D。但是根据定义,这种情况怎么可能不是二维的呢?我该如何做呢?

The problem seems not to be only the fact that my new_color has shape (3,), since the problem still holds when I replace it with an array of shape (2048,5632,3), but the fact that my condition is 2D while my initial array is 3D. But how could this condition not be 2D by definition, and how could I make this work?

感谢您的帮助

推荐答案

从帕丁顿的这张海报海报开始:

Starting with this posterised image of Paddington:

我想您想使用 np.where()如下,使所有红色区域变为品红色,所有其他区域变成黄色:

I think you want to use np.where() as follows to make all red areas into magenta and all other areas into yellow:

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Load PIL Image and ensure RGB rather than palette based, then make into Numpy array
pi = Image.open('paddington.png').convert('RGB')
na = np.array(pi)

# Now make 2 images same size, one magenta, one yellow
magenta = np.zeros_like(na) + [255,0,255]
yellow  = np.zeros_like(na) + [255,255,0]

# Anywhere paddington is red, make him magenta. Anywhere else, make him yellow.
result = np.where((na==[255,0,0]).all(axis=-1)[...,None], magenta, yellow) 

# Save result
Image.fromarray(result.astype(np.uint8)).save('result.png')

当然,不必制作全尺寸的洋红色图像和黄色,我只是这样做以匹配您的原始代码。您本可以使用单个像素并保存内存,使他像这样变成绿色和蓝色:

Of course, it was not necessary to make a full size image of magenta and yellow, I just did that to match your original code. You could have used a single pixel and saved memory, making him green and blue like this:

result = np.where((na==[255,0,0]).all(axis=-1)[...,None], [0,255,0], [0,0,255]) 

这篇关于numpy:如何在给定测试条件的多维数组中使用np.where?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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