IndexError:使用np.where时用作索引阵列必须是整数(或布尔)型的()

问题描述 投票:0回答:2

当我试图带着面具融合两个图像,我用np.where():

a,b和掩模是ndarrays的列表和D型细胞是float64。 tempImg是相同的D型一ndarray。

```
a = [[179.52181224 196.11236657 199.25513578 200.81821174 198.7369237
  188.63429662 192.30241342 207.88677914 222.15657045 229.51498031
  231.98006343 231.87414046 230.46600773 218.49685369 162.22483198
  100.36016141 121.1080895  135.74811817 130.99244378 110.68895168
  118.30273168 126.86982489 129.77672984 124.48464581 113.252353  ]
 [196.82448164 214.76750918 217.8666702  220.43924199 218.29167364
  206.96139307 210.62221148 226.93273178 242.81860388 250.77400119
  252.75942764 252.58055613 251.7060296  244.7889392  205.30175274
  140.06253874 130.66796303 134.60075016 144.20452322 134.04340699
  134.67761061 134.7536771  134.06737521 132.02019221 125.54434286]
 [199.35308577 217.40897714 220.85336669 223.59548903 221.36787333
  210.40169753 213.24258599 228.92592981 244.90159636 252.59280128
  252.9813501  248.71485061 237.89496352 225.45499552 211.15977205
  167.02392375 125.05120764 110.35189406 137.84965955 135.69712767
  133.58192482 132.36280398 132.31858306 134.45862906 132.1907518 ]]

//b is quite similar to a, same size but different value.
//mask is also the same size but has float values between 0 and 1:
[[0, 0, 0...1, 1, 0.56, 0.94, 1]...]
```
    for i in range (0, len(a)):
        tempImg = tempImg[np.where(mask[i] == 1, a[i], b[i])]
        tempImg = tempImg[np.where(mask[i] > 0 and mask[i] < 1,
                          a[i] * mask[i] + b[i] * (1 - mask[i]), tempImg)]
        img.append(tempImg)

预期的结果是新ndarray列表(IMG),但我得到了以下错误:

in blend
    tempImg = tempImg(np.where(mask[i] == 1, a[i], b[i]))
IndexError: arrays used as indices must be of integer (or boolean) type

谁能帮我找出这个问题?非常感谢!

python numpy-ndarray
2个回答
0
投票

相反,使用数组列表,我以为是代表了图像的行,使用二维数组因为这是图像的方式通常被解释。如果您的图片已定义为阵列的列表,你可以做以下,使之成为二维数组:

import numpy as np
list_of_arrays = [np.asarray([0.1, 0.2]), np.asarray([0.1, 0.2])]
my_image = np.asarray(list_of_arrays)

如果你的面具已经被定义,你可以使用的OpenCV的bitwise_and:看看在documentation关于使用细节,但this answer看起来似乎对你非常有用。


0
投票

感谢您更新您的问题。根据您的样本数据,这里是我的设置:

a = np.array([[179.52181224, 196.11236657, 199.25513578, 200.81821174, 198.7369237,
  188.63429662, 192.30241342, 207.88677914, 222.15657045, 229.51498031,
  231.98006343, 231.87414046, 230.46600773, 218.49685369, 162.22483198,
  100.36016141, 121.1080895 , 135.74811817, 130.99244378, 110.68895168,
  118.30273168, 126.86982489, 129.77672984, 124.48464581, 113.252353  ],
 [196.82448164, 214.76750918, 217.8666702 , 220.43924199, 218.29167364,
  206.96139307, 210.62221148, 226.93273178, 242.81860388, 250.77400119,
  252.75942764, 252.58055613, 251.7060296 , 244.7889392 , 205.30175274,
  140.06253874, 130.66796303, 134.60075016, 144.20452322, 134.04340699,
  134.67761061, 134.7536771 , 134.06737521, 132.02019221, 125.54434286],
 [199.35308577, 217.40897714, 220.85336669, 223.59548903, 221.36787333,
  210.40169753, 213.24258599, 228.92592981, 244.90159636, 252.59280128,
  252.9813501 , 248.71485061, 237.89496352, 225.45499552, 211.15977205,
  167.02392375, 125.05120764, 110.35189406, 137.84965955, 135.69712767,
  133.58192482, 132.36280398, 132.31858306, 134.45862906, 132.1907518]])

b =a**2
mask=np.array([[0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 1, 1, 0.56],
              [0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 1, 1, 0.56],
              [0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 1, 1, 0.56]])
tempImg=np.zeros_like(a)

您遇到的问题是双重的。首先,你试图索引tempImg与您其他阵列(tempImg=tempImg[])浮点指数。

解决方案如下:

for i in range (0, len(a)):
    tempImg[i] = np.where(mask[i] == 1, a[i], b[i])

其次,也是最重要的,当你没有必要这么做,在所有你是通过循环!

矢量化的解决方案杠杆numpy的:

tempImg = np.where(mask == 1, a, b)
© www.soinside.com 2019 - 2024. All rights reserved.